From 469a24b85da60871d717e09e7599c044edec4f25 Mon Sep 17 00:00:00 2001 From: Anthony Sterling Date: Mon, 2 May 2016 13:33:19 +0100 Subject: [PATCH 1/3] Added the `-f` flag to change the foreground color The `-f` flag enabling the selection of the following colors:- - white - yellow - black - magenta - blue - green - red By default the foreground is blue, as per the current implementation. --- README.md | 5 +++++ src/main.c | 5 ++++- src/nms.c | 42 +++++++++++++++++++++++++++++++++++++++++- src/nms.h | 6 +++++- 4 files changed, 55 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d1a14f2..83f6879 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,11 @@ 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 --------------------------------- diff --git a/src/main.c b/src/main.c index f472021..2101e12 100644 --- a/src/main.c +++ b/src/main.c @@ -12,8 +12,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 = getColorByName(optarg, args.foreground_color); + break; case 'a': args.auto_decrypt = true; break; diff --git a/src/nms.c b/src/nms.c index 4de277c..7bfbd05 100644 --- a/src/nms.c +++ b/src/nms.c @@ -86,7 +86,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, args->foreground_color, COLOR_BLACK); } // Get terminal window size @@ -284,3 +284,43 @@ char getMaskChar(void) { return maskChars[rand() % strlen(maskChars)]; } + +/* + * char getColorByName(char *string, int fallback) + * + * DESCR: + * Returns an ncurses color by its name. + * + */ +int getColorByName(char *string, int fallback) { + + 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 fallback; +} diff --git a/src/nms.h b/src/nms.h index e614aff..d8bec85 100644 --- a/src/nms.h +++ b/src/nms.h @@ -3,9 +3,10 @@ #include #include +#include // Default arguments for nms_exec() -#define INIT_NMSARGS { NULL, NULL, -1, -1, false, false } +#define INIT_NMSARGS { NULL, NULL, -1, -1, false, false , COLOR_BLUE } // Argument structure for nms_exec() typedef struct { @@ -15,9 +16,12 @@ typedef struct { int input_cursor_y; bool show_cursor; bool auto_decrypt; + int foreground_color; } NmsArgs; // Display the characters stored in the display queue char nms_exec(NmsArgs *); +int getColorByName(char *string, int fallback); + #endif From 62db60a1d93d9357b7958b9c5c905c8d0c1cffb1 Mon Sep 17 00:00:00 2001 From: Anthony Sterling Date: Mon, 2 May 2016 20:13:15 +0100 Subject: [PATCH 2/3] Added documenation update --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 83f6879..c02d404 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,7 @@ typedef struct { int input_cursor_y; bool show_cursor; bool auto_decrypt; + int foreground_color; } NmsArgs; ``` * `char *src` @@ -149,6 +150,8 @@ 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. * `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. + * `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 nms_exec() function: From 9ab70d1948ae058bc2956b6fdd5b6b641313e5c2 Mon Sep 17 00:00:00 2001 From: Brian Barto Date: Tue, 3 May 2016 10:30:11 -0400 Subject: [PATCH 3/3] Small refactor of the forground color code modified: README.md modified: src/main.c modified: src/nms.c modified: src/nms.h --- README.md | 12 +++++------- src/main.c | 4 ++-- src/nms.c | 15 ++++++++++++--- src/nms.h | 7 ++----- 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index c02d404..3e22a32 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,8 @@ 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. +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 ``` @@ -129,19 +130,18 @@ 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; bool show_cursor; bool auto_decrypt; - int foreground_color; } NmsArgs; ``` * `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` @@ -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. * `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. - * `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 nms_exec() function: diff --git a/src/main.c b/src/main.c index 119d554..ce80972 100644 --- a/src/main.c +++ b/src/main.c @@ -16,8 +16,8 @@ int main(int argc, char *argv[]) { while ((o = getopt(argc, argv, "f:av")) != -1) { switch (o) { case 'f': - args.foreground_color = getColorByName(optarg, args.foreground_color); - break; + args.foreground_color = optarg; + break; case 'a': args.auto_decrypt = true; break; diff --git a/src/nms.c b/src/nms.c index 7bfbd05..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, args->foreground_color, COLOR_BLACK); + init_pair(1, getColorByName(args->foreground_color), COLOR_BLACK); } // Get terminal window size @@ -288,11 +289,19 @@ char getMaskChar(void) { /* * 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, int fallback) { +int getColorByName(char *string) { + + if(string == NULL) { + return COLOR_BLUE; + } if(strcmp("white", string) == 0) { return COLOR_WHITE; @@ -322,5 +331,5 @@ int getColorByName(char *string, int fallback) { return COLOR_RED; } - return fallback; + return COLOR_BLUE; } diff --git a/src/nms.h b/src/nms.h index d8bec85..0b9d5aa 100644 --- a/src/nms.h +++ b/src/nms.h @@ -3,25 +3,22 @@ #include #include -#include // 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() typedef struct { char *src; + char* foreground_color; char *return_opts; int input_cursor_x; int input_cursor_y; bool show_cursor; bool auto_decrypt; - int foreground_color; } NmsArgs; // Display the characters stored in the display queue char nms_exec(NmsArgs *); -int getColorByName(char *string, int fallback); - #endif