Move static vars foregroundColor and clearScr to nmsterm implementations. Make setters and getters as necessary.

modified:   src/main.c
	modified:   src/nms.c
	modified:   src/nms.h
	modified:   src/nmsterm.c
	modified:   src/nmsterm.h
	modified:   src/nmsterm_ncurses.c
	modified:   src/sneakers.c
This commit is contained in:
Brian Barto 2017-01-18 19:19:33 -05:00
parent b4fd33a43b
commit 089a089bc4
7 changed files with 116 additions and 60 deletions

View File

@ -22,13 +22,13 @@ int main(int argc, char *argv[]) {
while ((o = getopt(argc, argv, "f:acv")) != -1) { while ((o = getopt(argc, argv, "f:acv")) != -1) {
switch (o) { switch (o) {
case 'f': case 'f':
nms_set_foreground_color(optarg); nms_set_foregroundcolor(optarg);
break; break;
case 'a': case 'a':
nms_set_auto_decrypt(1); nms_set_auto_decrypt(1);
break; break;
case 'c': case 'c':
nms_set_clear_scr(1); nms_set_clearscr(1);
break; break;
case 'v': case 'v':
printf("nms version " VERSION "\n"); printf("nms version " VERSION "\n");

View File

@ -25,16 +25,6 @@
#include "nms.h" #include "nms.h"
#include "nmsterm.h" #include "nmsterm.h"
// Color identifiers
#define COLOR_BLACK 0
#define COLOR_RED 1
#define COLOR_GREEN 2
#define COLOR_YELLOW 3
#define COLOR_BLUE 4
#define COLOR_MAGENTA 5
#define COLOR_CYAN 6
#define COLOR_WHITE 7
// Program settings // Program settings
#define TYPE_EFFECT_SPEED 4 // miliseconds per char #define TYPE_EFFECT_SPEED 4 // miliseconds per char
#define JUMBLE_SECONDS 2 // number of seconds for jumble effect #define JUMBLE_SECONDS 2 // number of seconds for jumble effect
@ -57,10 +47,8 @@ struct charAttr {
static void nms_sleep(int); static void nms_sleep(int);
// NMS settings // NMS settings
static int foregroundColor = COLOR_BLUE; // Foreground color setting
static char *returnOpts = NULL; // Return option setting static char *returnOpts = NULL; // Return option setting
static int autoDecrypt = 0; // Auto-decrypt flag static int autoDecrypt = 0; // Auto-decrypt flag
static int clearScr = 0; // clearScr flag
static int colorOn = 1; // Terminal color flag static int colorOn = 1; // Terminal color flag
static int inputPositionX = -1; // X coordinate for input position static int inputPositionX = -1; // X coordinate for input position
static int inputPositionY = -1; // Y coordinate for input position static int inputPositionY = -1; // Y coordinate for input position
@ -134,7 +122,7 @@ char nms_exec(char *string) {
setlocale(LC_ALL, ""); setlocale(LC_ALL, "");
// Initialize terminal // Initialize terminal
origRow = nmsterm_init_terminal(foregroundColor, clearScr); origRow = nmsterm_init_terminal();
// Get terminal window rows/cols // Get terminal window rows/cols
maxRows = nmsterm_get_rows(); maxRows = nmsterm_get_rows();
@ -306,7 +294,7 @@ char nms_exec(char *string) {
} else { } else {
// print source character // print source character
nmsterm_print_reveal_string(list_pointer->source, foregroundColor, colorOn); nmsterm_print_reveal_string(list_pointer->source, colorOn);
} }
} }
@ -363,26 +351,8 @@ char nms_exec(char *string) {
* "green", "red", and "cyan". This function will default to blue if * "green", "red", and "cyan". This function will default to blue if
* passed an invalid color. No value is returned. * passed an invalid color. No value is returned.
*/ */
void nms_set_foreground_color(char *color) { void nms_set_foregroundcolor(char *color) {
nmsterm_set_foregroundcolor(color);
if(strcmp("white", color) == 0)
foregroundColor = COLOR_WHITE;
else if(strcmp("yellow", color) == 0)
foregroundColor = COLOR_YELLOW;
else if(strcmp("black", color) == 0)
foregroundColor = COLOR_BLACK;
else if(strcmp("magenta", color) == 0)
foregroundColor = COLOR_MAGENTA;
else if(strcmp("blue", color) == 0)
foregroundColor = COLOR_BLUE;
else if(strcmp("green", color) == 0)
foregroundColor = COLOR_GREEN;
else if(strcmp("red", color) == 0)
foregroundColor = COLOR_RED;
else if(strcmp("cyan", color) == 0)
foregroundColor = COLOR_CYAN;
else
foregroundColor = COLOR_BLUE;
} }
/* /*
@ -409,11 +379,8 @@ void nms_set_auto_decrypt(int setting) {
* nms_set_clear_scr() sets the clearScr flag according to the * nms_set_clear_scr() sets the clearScr flag according to the
* true/false value of the 'setting' argument. * true/false value of the 'setting' argument.
*/ */
void nms_set_clear_scr(int setting) { void nms_set_clearscr(int s) {
if (setting) nmsterm_set_clearscr(s);
clearScr = 1;
else
clearScr = 0;
} }
/* /*

View File

@ -10,10 +10,10 @@
// Function prototypes // Function prototypes
char nms_exec(char *); char nms_exec(char *);
void nms_set_foreground_color(char *); void nms_set_foregroundcolor(char *);
void nms_set_return_opts(char *); void nms_set_return_opts(char *);
void nms_set_auto_decrypt(int); void nms_set_auto_decrypt(int);
void nms_set_clear_scr(int); void nms_set_clearscr(int);
void nms_use_color(int); void nms_use_color(int);
void nms_set_input_position(int, int); void nms_set_input_position(int, int);

View File

@ -12,9 +12,6 @@
#include <unistd.h> #include <unistd.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
// Static settings
static int clearScr = 0; // clearScr flag
// Macros for VT100 codes // Macros for VT100 codes
#define CLEAR_SCR() printf("\033[2J") // Clear Screen #define CLEAR_SCR() printf("\033[2J") // Clear Screen
#define CURSOR_HOME() printf("\033[H") // Move cursor to home position (0,0) #define CURSOR_HOME() printf("\033[H") // Move cursor to home position (0,0)
@ -30,17 +27,27 @@ static int clearScr = 0; // clearScr flag
#define CURSOR_HIDE() printf("\033[?25l") // Hide cursor #define CURSOR_HIDE() printf("\033[?25l") // Hide cursor
#define CURSOR_SHOW() printf("\033[?25h") // Unhide cursor #define CURSOR_SHOW() printf("\033[?25h") // Unhide cursor
// Color identifiers
#define COLOR_BLACK 0
#define COLOR_RED 1
#define COLOR_GREEN 2
#define COLOR_YELLOW 3
#define COLOR_BLUE 4
#define COLOR_MAGENTA 5
#define COLOR_CYAN 6
#define COLOR_WHITE 7
// Static settings
static int clearScr = 0; // clearScr flag
static int foregroundColor = COLOR_BLUE; // Foreground color setting
// Function prototypes // Function prototypes
static void nmsterm_set_terminal(int); static void nmsterm_set_terminal(int);
static int nmsterm_get_cursor_row(void); static int nmsterm_get_cursor_row(void);
// Initialize terminal window // Initialize terminal window
int nmsterm_init_terminal(int foregroundColor, int c) { int nmsterm_init_terminal(void) {
(void) foregroundColor;
int origRow = 0; int origRow = 0;
// Set clearScr flag
clearScr = c;
// Turn off line buffering and echo // Turn off line buffering and echo
nmsterm_set_terminal(0); nmsterm_set_terminal(0);
@ -138,7 +145,7 @@ char nmsterm_get_char(void) {
return c; return c;
} }
void nmsterm_print_reveal_string(char *s, int foregroundColor, int colorOn) { void nmsterm_print_reveal_string(char *s, int colorOn) {
// Set bold and foreground color // Set bold and foreground color
BOLD(); BOLD();
@ -165,6 +172,46 @@ int nmsterm_get_clearscr(void) {
return clearScr; return clearScr;
} }
/*
* nmsterm_set_clearscr() sets the clearScr flag according to the
* true/false value of the 's' argument.
*/
void nmsterm_set_clearscr(int s) {
if (s)
clearScr = 1;
else
clearScr = 0;
}
/*
* nms_set_foreground_color() sets the foreground color of the unencrypted
* characters as they are revealed to the color indicated by the 'color'
* argument. Valid arguments are "white", "yellow", "magenta", "blue",
* "green", "red", and "cyan". This function will default to blue if
* passed an invalid color. No value is returned.
*/
void nmsterm_set_foregroundcolor(char *c) {
if(strcmp("white", c) == 0)
foregroundColor = COLOR_WHITE;
else if(strcmp("yellow", c) == 0)
foregroundColor = COLOR_YELLOW;
else if(strcmp("black", c) == 0)
foregroundColor = COLOR_BLACK;
else if(strcmp("magenta", c) == 0)
foregroundColor = COLOR_MAGENTA;
else if(strcmp("blue", c) == 0)
foregroundColor = COLOR_BLUE;
else if(strcmp("green", c) == 0)
foregroundColor = COLOR_GREEN;
else if(strcmp("red", c) == 0)
foregroundColor = COLOR_RED;
else if(strcmp("cyan", c) == 0)
foregroundColor = COLOR_CYAN;
else
foregroundColor = COLOR_BLUE;
}
/* /*
* nmsterm_set_terminal() turns off terminal echo and line buffering when * nmsterm_set_terminal() turns off terminal echo and line buffering when
* passed an integer value that evaluates to true. It restores the * passed an integer value that evaluates to true. It restores the

View File

@ -9,7 +9,7 @@
#define NMSTERM_H 1 #define NMSTERM_H 1
// Function prototypes // Function prototypes
int nmsterm_init_terminal(int, int); int nmsterm_init_terminal(void);
void nmsterm_restore_terminal(void); void nmsterm_restore_terminal(void);
int nmsterm_get_rows(void); int nmsterm_get_rows(void);
int nmsterm_get_cols(void); int nmsterm_get_cols(void);
@ -19,10 +19,12 @@ void nmsterm_print_string(char *);
void nmsterm_refresh(void); void nmsterm_refresh(void);
void nmsterm_clear_input(void); void nmsterm_clear_input(void);
char nmsterm_get_char(void); char nmsterm_get_char(void);
void nmsterm_print_reveal_string(char *, int, int); void nmsterm_print_reveal_string(char *, int);
void nmsterm_show_cursor(void); void nmsterm_show_cursor(void);
void nmsterm_beep(void); void nmsterm_beep(void);
int nmsterm_get_clearscr(void); int nmsterm_get_clearscr(void);
void nmsterm_set_clearscr(int);
void nmsterm_set_foregroundcolor(char *);
#endif #endif

View File

@ -8,12 +8,12 @@
#include <ncurses.h> #include <ncurses.h>
// Static settings // Static settings
static int clearScr = 1; // clearScr flag // Static settings
static int clearScr = 1; // clearScr flag
static int foregroundColor = COLOR_BLUE; // Foreground color setting
// Initialize terminal window // Initialize terminal window
int nmsterm_init_terminal(int foregroundColor, int c) { int nmsterm_init_terminal(void) {
(void) c;
initscr(); initscr();
cbreak(); cbreak();
noecho(); noecho();
@ -75,7 +75,7 @@ void nmsterm_get_char(void) {
getch(); getch();
} }
void nmsterm_print_reveal_string(char *s, int foregroundColor, int colorOn) { void nmsterm_print_reveal_string(char *s, int colorOn) {
(void) colorOn; (void) colorOn;
(void) foregroundColor; (void) foregroundColor;
@ -105,3 +105,43 @@ void nmsterm_beep(void) {
int nmsterm_get_clearscr(void) { int nmsterm_get_clearscr(void) {
return clearScr; return clearScr;
} }
/*
* nmsterm_set_clearscr() sets the clearScr flag according to the
* true/false value of the 's' argument.
*/
void nmsterm_set_clearscr(int s) {
if (s)
clearScr = 1;
else
clearScr = 0;
}
/*
* nms_set_foreground_color() sets the foreground color of the unencrypted
* characters as they are revealed to the color indicated by the 'color'
* argument. Valid arguments are "white", "yellow", "magenta", "blue",
* "green", "red", and "cyan". This function will default to blue if
* passed an invalid color. No value is returned.
*/
void nmsterm_set_foregroundcolor(char *c) {
if(strcmp("white", c) == 0)
foregroundColor = COLOR_WHITE;
else if(strcmp("yellow", c) == 0)
foregroundColor = COLOR_YELLOW;
else if(strcmp("black", c) == 0)
foregroundColor = COLOR_BLACK;
else if(strcmp("magenta", c) == 0)
foregroundColor = COLOR_MAGENTA;
else if(strcmp("blue", c) == 0)
foregroundColor = COLOR_BLUE;
else if(strcmp("green", c) == 0)
foregroundColor = COLOR_GREEN;
else if(strcmp("red", c) == 0)
foregroundColor = COLOR_RED;
else if(strcmp("cyan", c) == 0)
foregroundColor = COLOR_CYAN;
else
foregroundColor = COLOR_BLUE;
}

View File

@ -162,7 +162,7 @@ int main(void) {
// Settings // Settings
nms_set_input_position(((termCols - strlen(foot2Center)) / 2) + 2, 18); nms_set_input_position(((termCols - strlen(foot2Center)) / 2) + 2, 18);
nms_set_return_opts("123456"); nms_set_return_opts("123456");
nms_set_clear_scr(1); nms_set_clearscr(1);
// Execut effect // Execut effect
input = nms_exec(display); input = nms_exec(display);