Merge branch 'anthonysterling-feature/colour'
This commit is contained in:
commit
5927e52444
12
README.md
12
README.md
@ -70,6 +70,12 @@ 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.
|
||||||
|
```
|
||||||
|
ls -l / | nms -f green
|
||||||
|
```
|
||||||
|
|
||||||
Using the Module in Your Program
|
Using the Module in Your Program
|
||||||
---------------------------------
|
---------------------------------
|
||||||
|
|
||||||
@ -124,6 +130,7 @@ 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;
|
||||||
@ -133,9 +140,8 @@ typedef struct {
|
|||||||
```
|
```
|
||||||
* `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`
|
||||||
|
@ -13,8 +13,11 @@ int main(int argc, char *argv[]) {
|
|||||||
NmsArgs args = INIT_NMSARGS;
|
NmsArgs args = INIT_NMSARGS;
|
||||||
|
|
||||||
// Processing command arguments
|
// Processing command arguments
|
||||||
while ((o = getopt(argc, argv, "av")) != -1) {
|
while ((o = getopt(argc, argv, "f:av")) != -1) {
|
||||||
switch (o) {
|
switch (o) {
|
||||||
|
case 'f':
|
||||||
|
args.foreground_color = optarg;
|
||||||
|
break;
|
||||||
case 'a':
|
case 'a':
|
||||||
args.auto_decrypt = true;
|
args.auto_decrypt = true;
|
||||||
break;
|
break;
|
||||||
|
51
src/nms.c
51
src/nms.c
@ -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, COLOR_BLUE, COLOR_BLACK);
|
init_pair(1, getColorByName(args->foreground_color), COLOR_BLACK);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get terminal window size
|
// Get terminal window size
|
||||||
@ -284,3 +285,51 @@ char getMaskChar(void) {
|
|||||||
|
|
||||||
return maskChars[rand() % strlen(maskChars)];
|
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;
|
||||||
|
}
|
||||||
|
@ -5,11 +5,12 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
// Default arguments for nms_exec()
|
// 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()
|
// 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;
|
||||||
|
Loading…
Reference in New Issue
Block a user