diff --git a/README.md b/README.md index d1a14f2..3e22a32 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,12 @@ eliminating the need for the user to press a key to start it. 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. +``` +ls -l / | nms -f green +``` + Using the Module in Your Program --------------------------------- @@ -124,6 +130,7 @@ Here is how the structure is defined: ``` typedef struct { char *src; + char *foreground_color; char *return_opts; int input_cursor_x; int input_cursor_y; @@ -133,9 +140,8 @@ typedef struct { ``` * `char *src` * Pointer to the string of characters on which to perform the effect. - -Useful for displaying menus: - +* `char *foreground_color` + * Pointer to a string containing the desired foreground color: white, yellow, black, magenta, blue, green, red. * `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()`; * `int input_cursor_x` and `int input_cursor_y` diff --git a/src/main.c b/src/main.c index 77dfc8d..ce80972 100644 --- a/src/main.c +++ b/src/main.c @@ -13,8 +13,11 @@ int main(int argc, char *argv[]) { NmsArgs args = INIT_NMSARGS; // Processing command arguments - while ((o = getopt(argc, argv, "av")) != -1) { + while ((o = getopt(argc, argv, "f:av")) != -1) { switch (o) { + case 'f': + args.foreground_color = optarg; + break; case 'a': args.auto_decrypt = true; break; diff --git a/src/nms.c b/src/nms.c index 4de277c..b3c0aef 100644 --- a/src/nms.c +++ b/src/nms.c @@ -39,6 +39,7 @@ struct winpos { // Function prototypes (internal) char getMaskChar(void); +int getColorByName(char *); /* * void nms_exec(NmsArgs *) @@ -86,7 +87,7 @@ char nms_exec(NmsArgs *args) { // Setting up and starting colors if terminal supports them if (has_colors()) { start_color(); - init_pair(1, COLOR_BLUE, COLOR_BLACK); + init_pair(1, getColorByName(args->foreground_color), COLOR_BLACK); } // Get terminal window size @@ -284,3 +285,51 @@ char getMaskChar(void) { return maskChars[rand() % strlen(maskChars)]; } + +/* + * char getColorByName(char *string, int fallback) + * + * ARGS: + * + * char *string - new color setting (white, yellow, black, magenta, blue, green, red) + * + * DESCR: + * Returns an ncurses color by its name. + * + */ +int getColorByName(char *string) { + + if(string == NULL) { + return COLOR_BLUE; + } + + if(strcmp("white", string) == 0) { + return COLOR_WHITE; + } + + if(strcmp("yellow", string) == 0) { + return COLOR_YELLOW; + } + + if(strcmp("black", string) == 0) { + return COLOR_BLACK; + } + + if(strcmp("magenta", string) == 0) { + return COLOR_MAGENTA; + } + + if(strcmp("blue", string) == 0) { + return COLOR_BLUE; + } + + if(strcmp("green", string) == 0) { + return COLOR_GREEN; + } + + if(strcmp("red", string) == 0) { + return COLOR_RED; + } + + return COLOR_BLUE; +} diff --git a/src/nms.h b/src/nms.h index e614aff..0b9d5aa 100644 --- a/src/nms.h +++ b/src/nms.h @@ -5,11 +5,12 @@ #include // Default arguments for nms_exec() -#define INIT_NMSARGS { NULL, NULL, -1, -1, false, false } +#define INIT_NMSARGS { NULL, NULL, NULL, -1, -1, false, false } // Argument structure for nms_exec() typedef struct { char *src; + char* foreground_color; char *return_opts; int input_cursor_x; int input_cursor_y;