feature implemented user-specified theme colors (TODO: make cmdline arguments work (segfault))

This commit is contained in:
bePrivate
2023-11-02 20:46:28 +01:00
parent d5b863b6ca
commit e67ec7d3fa
2 changed files with 337 additions and 44 deletions
BIN
View File
Binary file not shown.
+337 -44
View File
@@ -13,15 +13,22 @@
#include <unistd.h> #include <unistd.h>
#include <time.h> #include <time.h>
char *RESET = "\x1b[0m"; char *RESET = "\x1b[0m";
char *INCORRECT = "\x1b[31m"; char *INCORRECT = "\x1b[31m";
char *CORRECT = "\x1b[32m"; char *CORRECT = "\x1b[32m";
char *OUTOFPLACE = "\x1b[33m"; char *OUTOFPLACE = "\x1b[33m";
char *ASCII = "\x1b[7m"; char *ASCII = "\x1b[7m";
char *RESET_USER = "\x1b[0m";
char *INCORRECT_USER = "\x1b[31m";
char *CORRECT_USER = "\x1b[32m";
char *OUTOFPLACE_USER = "\x1b[33m";
char *ASCII_USER = "\x1b[7m";
int flag_verbose = 0;
int flag_worldlist = 0;
int flag_light_theme = 0; int flag_light_theme = 0;
int flag_user_theme = 0;
int flag_verbose = 0;
int flag_wordlist = 0;
int len_wordlist = 0; int len_wordlist = 0;
int lose = 0; int lose = 0;
char pf_props[6][5][2] = { char pf_props[6][5][2] = {
@@ -80,6 +87,7 @@ void put_word_into_playfield(char word[]);
int search_word(char word[], char target); int search_word(char word[], char target);
int search_wordlist(char wordlist[][6], char target[]); int search_wordlist(char wordlist[][6], char target[]);
void set_theme(); void set_theme();
void set_user_theme();
void show(); void show();
void show_win_lose_message(); void show_win_lose_message();
@@ -92,23 +100,38 @@ int argparse(int argc, char *argv[]) {
/* /*
look for invalid arguments look for invalid arguments
*/ */
if (strcmp(argv[i], argv[0]) != 0 && if (strcmp(argv[i], argv[0]) != 0 &&
strcmp(argv[i], "-h") != 0 && strcmp(argv[i], "-h") != 0 &&
strcmp(argv[i], "--help") != 0 && strcmp(argv[i], "--help") != 0 &&
strcmp(argv[i], "-l") != 0 && strcmp(argv[i], "-l") != 0 &&
strcmp(argv[i], "--light-theme") != 0 && strcmp(argv[i], "--light-theme") != 0 &&
strcmp(argv[i], "-t") != 0 && strcmp(argv[i], "-t") != 0 &&
strcmp(argv[i], "--theme") != 0 strcmp(argv[i], "--theme") != 0 &&
strcmp(argv[i], "-v") != 0 && strcmp(argv[i-1], "-t") != 0 && // RESET color
strcmp(argv[i], "--verbose") != 0 && strcmp(argv[i-1], "--theme") != 0 &&
strcmp(argv[i], "-w") != 0 && strcmp(argv[i-2], "-t") != 0 && // INCORRECT color,
strcmp(argv[i-1], "-w") != 0 && strcmp(argv[i-2], "--theme") != 0 &&
strcmp(argv[i], "--wordlist") != 0 && strcmp(argv[i-3], "-t") != 0 && // CORRECT color
strcmp(argv[i-1], "--wordlist") != 0 && strcmp(argv[i-3], "--theme") != 0 &&
strcmp(argv[i-4], "-t") != 0 && // OUTOFPLACE color
strcmp(argv[i-4], "--theme") != 0 &&
strcmp(argv[i-5], "-t") != 0 && // ASCII color
strcmp(argv[i-5], "--theme") != 0 &&
strcmp(argv[i], "-v") != 0 &&
strcmp(argv[i], "--verbose") != 0 &&
) { ) {
fprintf(stderr, "Invalid argument %s\n", argv[i]); fprintf(stderr, "Invalid argument %s\n", argv[i]);
flag_error = 1; flag_error = 1;
} }
if (strcmp(argv[i], "-w") != 0 &&
strcmp(argv[i], "--wordlist") != 0
) {
if (strcmp(argv[i-1], "-w") != 0 &&
strcmp(argv[i-1], "--wordlist") != 0
) {
}
}
/* /*
print the help & usage print the help & usage
@@ -149,28 +172,43 @@ int argparse(int argc, char *argv[]) {
/* /*
getting the path to the wordlist getting the path to the wordlist
*/ */
if (strcmp(argv[i], "--wordlist") == 0 && flag_worldlist == 0) { if (strcmp(argv[i], "--wordlist") == 0 && flag_wordlist == 0) {
flag_worldlist = 1; flag_wordlist = 1;
if (argv[i+1] != NULL) { // if there is an argument present after that, read it as the wordlist path if (argv[i+1] != NULL) { // if there is an argument present after that, read it as the wordlist path
strcpy(wordlist_path, argv[i+1]); strcpy(wordlist_path, argv[i+1]);
} else { } else {
fprintf(stderr, "Missing argument after %s\nUsage: %s <path_to_wordlist_file>\n", argv[i], argv[i]); fprintf(stderr, "Missing argument after %s\nUsage: %s <path_to_wordlist_file>\n", argv[i], argv[i]);
flag_error = 1; flag_error = 1;
} }
} else if (strcmp(argv[i], "--wordlist") != 0 && flag_worldlist == 0) { } else if (strcmp(argv[i], "--wordlist") != 0 && flag_wordlist == 0) {
strcpy(wordlist_path, "lib/wordlist"); strcpy(wordlist_path, "lib/wordlist");
flag_wordlist = 1;
} }
// -------------------------------------------------------------- // --------------------------------------------------------------
if (strcmp(argv[i], "--wordlist") == 0 && flag_worldlist == 0) { if (strcmp(argv[i], "-w") == 0 && flag_wordlist == 0) {
flag_worldlist = 1; flag_wordlist = 1;
if (argv[i+1] != NULL) { // if there is an argument present after that, read it as the wordlist path if (argv[i+1] != NULL) { // if there is an argument present after that, read it as the wordlist path
strcpy(wordlist_path, argv[i+1]); strcpy(wordlist_path, argv[i+1]);
} else { } else {
fprintf(stderr, "Missing argument after %s\nUsage: %s <path_to_wordlist_file>\n", argv[i], argv[i]); fprintf(stderr, "Missing argument after %s\nUsage: %s <path_to_wordlist_file>\n", argv[i], argv[i]);
flag_error = 1; flag_error = 1;
} }
} else if (strcmp(argv[i], "--wordlist") != 0 && flag_worldlist == 0) { } else if (strcmp(argv[i], "-w") != 0 && flag_wordlist == 0) {
strcpy(wordlist_path, "lib/wordlist"); strcpy(wordlist_path, "lib/wordlist");
flag_wordlist = 1;
}
/*
getting user-specified theme colors
*/
if (strcmp(argv[i], "-t") != 0 && flag_user_theme == 0) {
flag_user_theme = 1;
set_user_theme(argv[i+1], argv[i+2], argv[i+3], argv[i+4], argv[i+5]);
}
// --------------------------------------------------------------
if (strcmp(argv[i], "--theme") != 0 && flag_user_theme == 0) {
flag_user_theme = 1;
set_user_theme(argv[i+1], argv[i+2], argv[i+3], argv[i+4], argv[i+5]);
} }
} }
@@ -324,7 +362,7 @@ int main(int argc, char *argv[]) {
} }
// set light/dark theme // set light/dark theme
set_theme(); if (flag_user_theme == 0) set_theme();
// prerequisite 1 // prerequisite 1
get_random_word(word_to_guess, wordlist_path); get_random_word(word_to_guess, wordlist_path);
@@ -353,13 +391,12 @@ void print_help_and_usage() {
" specify a custom wordlist\n" " specify a custom wordlist\n"
" RULES: five characters on each line (no empty lines),\n" " RULES: five characters on each line (no empty lines),\n"
" each new line is separated by '\\n' (Enter/Return)\n" " each new line is separated by '\\n' (Enter/Return)\n"
"-t/--theme <correct_color> <incorrect_color> <out_of_place_color> <ascii_art_color>\n" // TODO: make work "-t/--theme <default_color> <correct_color> <incorrect_color> <out_of_place_color> <ascii_art_color>\n" // TODO: make work
" specify the colors in which the playfield will be visible\n" " specify the colors in which the playfield will be visible\n"
" RULES: choose the color from this list:\n" " RULES: choose the color from this list:\n"
" [ black, red, green, yellow, blue, magenta, cyan, white ]\n" " [ 'x' (default/reset), 'b' (black), 'r' (red), 'g' (green), 'y' (yellow), 'l' (blue), 'm' (magenta), 'c' (cyan), 'w' (white) ]\n"
" append ':b' (background color) or ':f' (foreground color)\n" " the format is <foreground_color><background_color>\n"
" add a ';' between the background and foreground colors\n" " EXAMPLE: '-t xx gb bw bb wb'\n"
" EXAMPLE: '-t green:f;black:b cyan:b;white:f red:b;black:f white:b;black:f'\n"
"-l/--light-theme\n" "-l/--light-theme\n"
" activate light theme\n" " activate light theme\n"
"-v/--verbose\n" "-v/--verbose\n"
@@ -413,24 +450,280 @@ int search_wordlist(char wordlist[][6], char target[]) {
} }
/* /*
set the theme (light/dark) set the theme (light/dark/user-specified)
*/ */
void set_theme() { void set_theme() {
if (flag_light_theme == 1) { if (flag_user_theme != 1) {
RESET = "\x1b[0;0m"; // handle light/dark mode
INCORRECT = "\x1b[0;41m"; if (flag_light_theme == 1) {
CORRECT = "\x1b[0;42m"; RESET = "\x1b[0;0m";
OUTOFPLACE = "\x1b[0;43m"; INCORRECT = "\x1b[0;41m";
ASCII = "\x1b[0;7m"; CORRECT = "\x1b[0;42m";
OUTOFPLACE = "\x1b[0;43m";
ASCII = "\x1b[0;7m";
} else {
RESET = "\x1b[37;0m";
INCORRECT = "\x1b[37;41m";
CORRECT = "\x1b[37;42m";
OUTOFPLACE = "\x1b[37;43m";
ASCII = "\x1b[37;90m";
}
} else { } else {
RESET = "\x1b[37;0m"; // handle user-specified theme
INCORRECT = "\x1b[37;41m";
CORRECT = "\x1b[37;42m"; strcpy(RESET, RESET_USER);
OUTOFPLACE = "\x1b[37;43m"; strcpy(INCORRECT, INCORRECT_USER);
ASCII = "\x1b[37;90m"; strcpy(CORRECT, CORRECT_USER);
strcpy(OUTOFPLACE, OUTOFPLACE_USER);
strcpy(ASCII, ASCII_USER);
} }
} }
/*
set user-specified theme
*/
void set_user_theme(char reset[], char incorrect[], char correct[], char outofplace[], char ascii[]) {
/* x = reset
* b = black
* r = red
* g = green
* y = yellow
* l = blue
* m = magenta
* c = cyan
* w = white
*/
int reset_int[2];
int incorrect_int[2];
int correct_int[2];
int outofplace_int[2];
int ascii_int[2];
// converting the flags into usable numbers
for (int i = 0; i < 2; i++) {
switch (reset[i]) {
case 'x':
if (i == 0) reset_int[i] = 0;
if (i == 1) reset_int[i] = 0;
break;
case 'b':
if (i == 0) reset_int[i] = 30;
if (i == 1) reset_int[i] = 40;
break;
case 'r':
if (i == 0) reset_int[i] = 31;
if (i == 1) reset_int[i] = 41;
break;
case 'g':
if (i == 0) reset_int[i] = 32;
if (i == 1) reset_int[i] = 42;
break;
case 'y':
if (i == 0) reset_int[i] = 33;
if (i == 1) reset_int[i] = 43;
break;
case 'l':
if (i == 0) reset_int[i] = 34;
if (i == 1) reset_int[i] = 44;
break;
case 'm':
if (i == 0) reset_int[i] = 35;
if (i == 1) reset_int[i] = 45;
break;
case 'c':
if (i == 0) reset_int[i] = 36;
if (i == 1) reset_int[i] = 46;
break;
case 'w':
if (i == 0) reset_int[i] = 37;
if (i == 1) reset_int[i] = 47;
break;
}
}
for (int i = 0; i < 2; i++) {
switch (incorrect[i]) {
case 'x':
if (i == 0) incorrect_int[i] = 0;
if (i == 1) incorrect_int[i] = 0;
break;
case 'b':
if (i == 0) incorrect_int[i] = 30;
if (i == 1) incorrect_int[i] = 40;
break;
case 'r':
if (i == 0) incorrect_int[i] = 31;
if (i == 1) incorrect_int[i] = 41;
break;
case 'g':
if (i == 0) incorrect_int[i] = 32;
if (i == 1) incorrect_int[i] = 42;
break;
case 'y':
if (i == 0) incorrect_int[i] = 33;
if (i == 1) incorrect_int[i] = 43;
break;
case 'l':
if (i == 0) incorrect_int[i] = 34;
if (i == 1) incorrect_int[i] = 44;
break;
case 'm':
if (i == 0) incorrect_int[i] = 35;
if (i == 1) incorrect_int[i] = 45;
break;
case 'c':
if (i == 0) incorrect_int[i] = 36;
if (i == 1) incorrect_int[i] = 46;
break;
case 'w':
if (i == 0) incorrect_int[i] = 37;
if (i == 1) incorrect_int[i] = 47;
break;
}
}
for (int i = 0; i < 2; i++) {
switch (correct[i]) {
case 'x':
if (i == 0) correct_int[i] = 0;
if (i == 1) correct_int[i] = 0;
break;
case 'b':
if (i == 0) correct_int[i] = 30;
if (i == 1) correct_int[i] = 40;
break;
case 'r':
if (i == 0) correct_int[i] = 31;
if (i == 1) correct_int[i] = 41;
break;
case 'g':
if (i == 0) correct_int[i] = 32;
if (i == 1) correct_int[i] = 42;
break;
case 'y':
if (i == 0) correct_int[i] = 33;
if (i == 1) correct_int[i] = 43;
break;
case 'l':
if (i == 0) correct_int[i] = 34;
if (i == 1) correct_int[i] = 44;
break;
case 'm':
if (i == 0) correct_int[i] = 35;
if (i == 1) correct_int[i] = 45;
break;
case 'c':
if (i == 0) correct_int[i] = 36;
if (i == 1) correct_int[i] = 46;
break;
case 'w':
if (i == 0) correct_int[i] = 37;
if (i == 1) correct_int[i] = 47;
break;
}
}
for (int i = 0; i < 2; i++) {
switch (outofplace[i]) {
case 'x':
if (i == 0) outofplace_int[i] = 0;
if (i == 1) outofplace_int[i] = 0;
break;
case 'b':
if (i == 0) outofplace_int[i] = 30;
if (i == 1) outofplace_int[i] = 40;
break;
case 'r':
if (i == 0) outofplace_int[i] = 31;
if (i == 1) outofplace_int[i] = 41;
break;
case 'g':
if (i == 0) outofplace_int[i] = 32;
if (i == 1) outofplace_int[i] = 42;
break;
case 'y':
if (i == 0) outofplace_int[i] = 33;
if (i == 1) outofplace_int[i] = 43;
break;
case 'l':
if (i == 0) outofplace_int[i] = 34;
if (i == 1) outofplace_int[i] = 44;
break;
case 'm':
if (i == 0) outofplace_int[i] = 35;
if (i == 1) outofplace_int[i] = 45;
break;
case 'c':
if (i == 0) outofplace_int[i] = 36;
if (i == 1) outofplace_int[i] = 46;
break;
case 'w':
if (i == 0) outofplace_int[i] = 37;
if (i == 1) outofplace_int[i] = 47;
break;
}
}
for (int i = 0; i < 2; i++) {
switch (ascii[i]) {
case 'x':
if (i == 0) ascii_int[i] = 0;
if (i == 1) ascii_int[i] = 0;
break;
case 'b':
if (i == 0) ascii_int[i] = 30;
if (i == 1) ascii_int[i] = 40;
break;
case 'r':
if (i == 0) ascii_int[i] = 31;
if (i == 1) ascii_int[i] = 41;
break;
case 'g':
if (i == 0) ascii_int[i] = 32;
if (i == 1) ascii_int[i] = 42;
break;
case 'y':
if (i == 0) ascii_int[i] = 33;
if (i == 1) ascii_int[i] = 43;
break;
case 'l':
if (i == 0) ascii_int[i] = 34;
if (i == 1) ascii_int[i] = 44;
break;
case 'm':
if (i == 0) ascii_int[i] = 35;
if (i == 1) ascii_int[i] = 45;
break;
case 'c':
if (i == 0) ascii_int[i] = 36;
if (i == 1) ascii_int[i] = 46;
break;
case 'w':
if (i == 0) ascii_int[i] = 37;
if (i == 1) ascii_int[i] = 47;
break;
}
}
int reset_f = reset_int[0];
int reset_b = reset_int[1];
int incorrect_f = incorrect_int[0];
int incorrect_b = incorrect_int[1];
int correct_f = correct_int[0];
int correct_b = correct_int[1];
int outofplace_f = outofplace_int[0];
int outofplace_b = outofplace_int[1];
int ascii_f = ascii_int[0];
int ascii_b = ascii_int[1];
sprintf(RESET_USER, "\x1b[%d;%dm", reset_f, reset_b);
sprintf(INCORRECT_USER, "\x1b[%d;%dm", incorrect_f, incorrect_b);
sprintf(CORRECT_USER, "\x1b[%d;%dm", correct_f, correct_b);
sprintf(OUTOFPLACE_USER, "\x1b[%d;%dm", outofplace_f, outofplace_b);
sprintf(ASCII_USER, "\x1b[%d;%dm", ascii_f, ascii_b);
}
/* /*
prints the current state of the game prints the current state of the game
shows shows