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) {
switch (o) {
case 'f':
nms_set_foreground_color(optarg);
nms_set_foregroundcolor(optarg);
break;
case 'a':
nms_set_auto_decrypt(1);
break;
case 'c':
nms_set_clear_scr(1);
nms_set_clearscr(1);
break;
case 'v':
printf("nms version " VERSION "\n");

View File

@ -25,16 +25,6 @@
#include "nms.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
#define TYPE_EFFECT_SPEED 4 // miliseconds per char
#define JUMBLE_SECONDS 2 // number of seconds for jumble effect
@ -57,10 +47,8 @@ struct charAttr {
static void nms_sleep(int);
// NMS settings
static int foregroundColor = COLOR_BLUE; // Foreground color setting
static char *returnOpts = NULL; // Return option setting
static int autoDecrypt = 0; // Auto-decrypt flag
static int clearScr = 0; // clearScr flag
static int colorOn = 1; // Terminal color flag
static int inputPositionX = -1; // X 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, "");
// Initialize terminal
origRow = nmsterm_init_terminal(foregroundColor, clearScr);
origRow = nmsterm_init_terminal();
// Get terminal window rows/cols
maxRows = nmsterm_get_rows();
@ -306,7 +294,7 @@ char nms_exec(char *string) {
} else {
// 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
* passed an invalid color. No value is returned.
*/
void nms_set_foreground_color(char *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;
void nms_set_foregroundcolor(char *color) {
nmsterm_set_foregroundcolor(color);
}
/*
@ -409,11 +379,8 @@ void nms_set_auto_decrypt(int setting) {
* nms_set_clear_scr() sets the clearScr flag according to the
* true/false value of the 'setting' argument.
*/
void nms_set_clear_scr(int setting) {
if (setting)
clearScr = 1;
else
clearScr = 0;
void nms_set_clearscr(int s) {
nmsterm_set_clearscr(s);
}
/*

View File

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

View File

@ -12,9 +12,6 @@
#include <unistd.h>
#include <sys/ioctl.h>
// Static settings
static int clearScr = 0; // clearScr flag
// Macros for VT100 codes
#define CLEAR_SCR() printf("\033[2J") // Clear Screen
#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_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
static void nmsterm_set_terminal(int);
static int nmsterm_get_cursor_row(void);
// Initialize terminal window
int nmsterm_init_terminal(int foregroundColor, int c) {
(void) foregroundColor;
int nmsterm_init_terminal(void) {
int origRow = 0;
// Set clearScr flag
clearScr = c;
// Turn off line buffering and echo
nmsterm_set_terminal(0);
@ -138,7 +145,7 @@ char nmsterm_get_char(void) {
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
BOLD();
@ -165,6 +172,46 @@ int nmsterm_get_clearscr(void) {
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
* passed an integer value that evaluates to true. It restores the

View File

@ -9,7 +9,7 @@
#define NMSTERM_H 1
// Function prototypes
int nmsterm_init_terminal(int, int);
int nmsterm_init_terminal(void);
void nmsterm_restore_terminal(void);
int nmsterm_get_rows(void);
int nmsterm_get_cols(void);
@ -19,10 +19,12 @@ void nmsterm_print_string(char *);
void nmsterm_refresh(void);
void nmsterm_clear_input(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_beep(void);
int nmsterm_get_clearscr(void);
void nmsterm_set_clearscr(int);
void nmsterm_set_foregroundcolor(char *);
#endif

View File

@ -8,12 +8,12 @@
#include <ncurses.h>
// 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
int nmsterm_init_terminal(int foregroundColor, int c) {
(void) c;
int nmsterm_init_terminal(void) {
initscr();
cbreak();
noecho();
@ -75,7 +75,7 @@ void nmsterm_get_char(void) {
getch();
}
void nmsterm_print_reveal_string(char *s, int foregroundColor, int colorOn) {
void nmsterm_print_reveal_string(char *s, int colorOn) {
(void) colorOn;
(void) foregroundColor;
@ -105,3 +105,43 @@ void nmsterm_beep(void) {
int nmsterm_get_clearscr(void) {
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
nms_set_input_position(((termCols - strlen(foot2Center)) / 2) + 2, 18);
nms_set_return_opts("123456");
nms_set_clear_scr(1);
nms_set_clearscr(1);
// Execut effect
input = nms_exec(display);