Small refactor of the forground color code

modified:   README.md
	modified:   src/main.c
	modified:   src/nms.c
	modified:   src/nms.h
This commit is contained in:
Brian Barto 2016-05-03 10:30:11 -04:00
parent eef49d2ce6
commit 9ab70d1948
4 changed files with 21 additions and 17 deletions

View File

@ -70,7 +70,8 @@ eliminating the need for the user to press a key to start it.
ls -l / | nms -a ls -l / | nms -a
``` ```
Use the `-f` option to set foreground color to either white, yellow, black, magenta, blue, green, or red - this is blue by default. Use the `-f` option to set foreground color to either white, yellow, black, magenta, blue, green, or
red - this is blue by default.
``` ```
ls -l / | nms -f green ls -l / | nms -f green
``` ```
@ -129,19 +130,18 @@ Here is how the structure is defined:
``` ```
typedef struct { typedef struct {
char *src; char *src;
char *foreground_color;
char *return_opts; char *return_opts;
int input_cursor_x; int input_cursor_x;
int input_cursor_y; int input_cursor_y;
bool show_cursor; bool show_cursor;
bool auto_decrypt; bool auto_decrypt;
int foreground_color;
} NmsArgs; } NmsArgs;
``` ```
* `char *src` * `char *src`
* Pointer to the string of characters on which to perform the effect. * Pointer to the string of characters on which to perform the effect.
* `char *foreground_color`
Useful for displaying menus: * Pointer to a string containing the desired foreground color: white, yellow, black, magenta, blue, green, red.
* `char *return_opts` * `char *return_opts`
* String pointer containing only the character options that the user must choose from once the src characters are revealed. For example, if you are showing a menu with six options, this string might be "123456". The user will have to choose one of these characters before execution is handed back to the calling function. Note that the character selected is returned by `nms_exec()`; * String pointer containing only the character options that the user must choose from once the src characters are revealed. For example, if you are showing a menu with six options, this string might be "123456". The user will have to choose one of these characters before execution is handed back to the calling function. Note that the character selected is returned by `nms_exec()`;
* `int input_cursor_x` and `int input_cursor_y` * `int input_cursor_x` and `int input_cursor_y`
@ -150,8 +150,6 @@ Useful for displaying menus:
* Set to `true` if you want the cursor to be visible during the text decryption effect. It is set to `false` by default. * Set to `true` if you want the cursor to be visible during the text decryption effect. It is set to `false` by default.
* `bool auto_decrypt` * `bool auto_decrypt`
* Set to `true` to automatically start the decryption effect, eliminating the need for the user to press a key to start it. * Set to `true` to automatically start the decryption effect, eliminating the need for the user to press a key to start it.
* `int foreground_color`
* Use this to assign a default (ncurses) color to the foreground
Assign values to the structure members as needed. Then simply pass a pointer to the structure to the Assign values to the structure members as needed. Then simply pass a pointer to the structure to the
nms_exec() function: nms_exec() function:

View File

@ -16,8 +16,8 @@ int main(int argc, char *argv[]) {
while ((o = getopt(argc, argv, "f:av")) != -1) { while ((o = getopt(argc, argv, "f:av")) != -1) {
switch (o) { switch (o) {
case 'f': case 'f':
args.foreground_color = getColorByName(optarg, args.foreground_color); args.foreground_color = optarg;
break; break;
case 'a': case 'a':
args.auto_decrypt = true; args.auto_decrypt = true;
break; break;

View File

@ -39,6 +39,7 @@ struct winpos {
// Function prototypes (internal) // Function prototypes (internal)
char getMaskChar(void); char getMaskChar(void);
int getColorByName(char *);
/* /*
* void nms_exec(NmsArgs *) * void nms_exec(NmsArgs *)
@ -86,7 +87,7 @@ char nms_exec(NmsArgs *args) {
// Setting up and starting colors if terminal supports them // Setting up and starting colors if terminal supports them
if (has_colors()) { if (has_colors()) {
start_color(); start_color();
init_pair(1, args->foreground_color, COLOR_BLACK); init_pair(1, getColorByName(args->foreground_color), COLOR_BLACK);
} }
// Get terminal window size // Get terminal window size
@ -288,11 +289,19 @@ char getMaskChar(void) {
/* /*
* char getColorByName(char *string, int fallback) * char getColorByName(char *string, int fallback)
* *
* ARGS:
*
* char *string - new color setting (white, yellow, black, magenta, blue, green, red)
*
* DESCR: * DESCR:
* Returns an ncurses color by its name. * Returns an ncurses color by its name.
* *
*/ */
int getColorByName(char *string, int fallback) { int getColorByName(char *string) {
if(string == NULL) {
return COLOR_BLUE;
}
if(strcmp("white", string) == 0) { if(strcmp("white", string) == 0) {
return COLOR_WHITE; return COLOR_WHITE;
@ -322,5 +331,5 @@ int getColorByName(char *string, int fallback) {
return COLOR_RED; return COLOR_RED;
} }
return fallback; return COLOR_BLUE;
} }

View File

@ -3,25 +3,22 @@
#include <stddef.h> #include <stddef.h>
#include <stdbool.h> #include <stdbool.h>
#include <ncurses.h>
// Default arguments for nms_exec() // Default arguments for nms_exec()
#define INIT_NMSARGS { NULL, NULL, -1, -1, false, false , COLOR_BLUE } #define INIT_NMSARGS { NULL, NULL, NULL, -1, -1, false, false }
// Argument structure for nms_exec() // Argument structure for nms_exec()
typedef struct { typedef struct {
char *src; char *src;
char* foreground_color;
char *return_opts; char *return_opts;
int input_cursor_x; int input_cursor_x;
int input_cursor_y; int input_cursor_y;
bool show_cursor; bool show_cursor;
bool auto_decrypt; bool auto_decrypt;
int foreground_color;
} NmsArgs; } NmsArgs;
// Display the characters stored in the display queue // Display the characters stored in the display queue
char nms_exec(NmsArgs *); char nms_exec(NmsArgs *);
int getColorByName(char *string, int fallback);
#endif #endif