From ba047c76e31c5c8070939767b91a9abd26aa823a Mon Sep 17 00:00:00 2001 From: Dr-Noob Date: Thu, 2 Jul 2020 18:53:28 +0200 Subject: [PATCH] Add two different styles. The old one is now called retro, and the new one, which is the default, is called fancy --- src/args.c | 43 +++++++++++-- src/args.h | 17 ++--- src/ascii.h | 6 +- src/main.c | 9 ++- src/printer.c | 174 +++++++++++++++++++++++++++++++------------------- src/printer.h | 11 +++- 6 files changed, 176 insertions(+), 84 deletions(-) diff --git a/src/args.c b/src/args.c index ceadd1b..287505b 100644 --- a/src/args.c +++ b/src/args.c @@ -5,25 +5,37 @@ #include "args.h" #include "global.h" +#define ARG_STR_STYLE "style" #define ARG_STR_COLOR "color" #define ARG_STR_HELP "help" #define ARG_STR_LEVELS "levels" #define ARG_STR_VERSION "version" -#define ARG_CHAR_COLOR 'c' -#define ARG_CHAR_HELP 'h' -#define ARG_CHAR_LEVELS 'l' -#define ARG_CHAR_VERSION 'v' + +#define ARG_CHAR_STYLE 's' +#define ARG_CHAR_COLOR 'c' +#define ARG_CHAR_HELP 'h' +#define ARG_CHAR_LEVELS 'l' +#define ARG_CHAR_VERSION 'v' + +#define STYLE_STR_1 "fancy" +#define STYLE_STR_2 "retro" struct args_struct { bool levels_flag; bool help_flag; bool version_flag; + STYLE style; struct color* color1; struct color* color2; }; +static const char* SYTLES_STR_LIST[STYLES_COUNT] = { STYLE_STR_1, STYLE_STR_2 }; static struct args_struct args; +STYLE get_style() { + return args.style; +} + struct color* get_color1() { return args.color1; } @@ -48,6 +60,16 @@ bool verbose_enabled() { return false; } +STYLE parse_style(char* style) { + int i = 0; + while(i != STYLES_COUNT && strcmp(SYTLES_STR_LIST[i],style) != 0) + i++; + + if(i == STYLES_COUNT) + return STYLE_INVALID; + return i; +} + bool parse_color(char* optarg, struct color** c1, struct color** c2) { *c1 = malloc(sizeof(struct color)); *c2 = malloc(sizeof(struct color)); @@ -97,8 +119,10 @@ bool parse_args(int argc, char* argv[]) { bool color_flag = false; args.levels_flag = false; args.help_flag = false; + args.style = STYLE_EMPTY; static struct option long_options[] = { + {ARG_STR_STYLE, required_argument, 0, ARG_CHAR_STYLE }, {ARG_STR_COLOR, required_argument, 0, ARG_CHAR_COLOR }, {ARG_STR_HELP, no_argument, 0, ARG_CHAR_HELP }, {ARG_STR_LEVELS, no_argument, 0, ARG_CHAR_LEVELS }, @@ -120,6 +144,17 @@ bool parse_args(int argc, char* argv[]) { return false; } } + else if(c == ARG_CHAR_STYLE) { + if(args.style != STYLE_EMPTY) { + printErr("Style option specified more than once"); + return false; + } + args.style = parse_style(optarg); + if(args.style == STYLE_INVALID) { + printErr("Invalid style '%s'\n",optarg); + return false; + } + } else if(c == ARG_CHAR_HELP) { if(args.help_flag) { printErr("Help option specified more than once"); diff --git a/src/args.h b/src/args.h index 439d27a..cf49697 100644 --- a/src/args.h +++ b/src/args.h @@ -4,14 +4,6 @@ #include #include -bool parse_args(int argc, char* argv[]); -bool show_help(); -bool show_levels(); -bool show_version(); -bool verbose_enabled(); -struct color* get_color1(); -struct color* get_color2(); - struct color { int32_t R; int32_t G; @@ -20,4 +12,13 @@ struct color { #include "printer.h" +bool parse_args(int argc, char* argv[]); +bool show_help(); +bool show_levels(); +bool show_version(); +bool verbose_enabled(); +struct color* get_color1(); +struct color* get_color2(); +STYLE get_style(); + #endif diff --git a/src/ascii.h b/src/ascii.h index ff026fa..62ea390 100644 --- a/src/ascii.h +++ b/src/ascii.h @@ -1,7 +1,7 @@ #ifndef __ASCII__ #define __ASCII__ -#define NUMBER_OF_LINES 20 +#define NUMBER_OF_LINES 19 #define LINE_SIZE 62 #define AMD_ASCII \ @@ -23,7 +23,6 @@ \ \ \ - \ " #define INTEL_ASCII \ @@ -45,7 +44,6 @@ #### #### \ ##### ########## \ ########## ################ \ - ############################### \ - " + ############################### " #endif diff --git a/src/main.c b/src/main.c index 78b357a..ba0255f 100644 --- a/src/main.c +++ b/src/main.c @@ -6,12 +6,15 @@ #include "cpuid.h" #include "global.h" -static const char* VERSION = "0.51"; +static const char* VERSION = "0.52"; void print_help(char *argv[]) { - printf("Usage: %s [--version] [--help] [--style STYLE]\n\ + printf("Usage: %s [--version] [--help] [--levels] [--style STYLE] [--color 'R,G,B:R,G,B']\n\ Options: \n\ --color Set text color. Two colors (in RGB format) must be specified in the form: R,G,B:R,G,B\n\ + --style Set the style of the ASCII art:\n\ + * fancy \n\ + * retro \n\ --help Prints this help and exit\n\ --levels Prints CPU model and cpuid levels (debug purposes)\n\ --version Prints cpufetch version and exit\n", @@ -60,7 +63,7 @@ int main(int argc, char* argv[]) { if(topo == NULL) return EXIT_FAILURE; - if(print_cpufetch(cpu, cach, freq, topo, get_color1(), get_color2())) + if(print_cpufetch(cpu, cach, freq, topo, get_style(), get_color1(), get_color2())) return EXIT_SUCCESS; else return EXIT_FAILURE; diff --git a/src/printer.c b/src/printer.c index 641280b..d3aa0c8 100644 --- a/src/printer.c +++ b/src/printer.c @@ -7,23 +7,20 @@ #include "ascii.h" #include "global.h" -/* style: retro (#) - fancy ( ) - default -> fancy - color: default -> amd / intel - blue - blue_dark -*/ - -#define COL_NONE "" -#define COL_INTEL_DEFAULT_1 "\x1b[36;1m" -#define COL_INTEL_DEFAULT_2 "\x1b[37;1m" -#define COL_AMD_DEFAULT_1 "\x1b[37;1m" -#define COL_AMD_DEFAULT_2 "\x1b[31;1m" -#define RESET "\x1b[m" - -#define COL_BACK_1 "\x1b[32;44m" -#define COL_BACK_2 "\x1b[32;47m" +#define COL_NONE "" +#define COL_INTEL_FANCY_1 "\x1b[46;1m" +#define COL_INTEL_FANCY_2 "\x1b[47;1m" +#define COL_INTEL_FANCY_3 "\x1b[36;1m" +#define COL_INTEL_FANCY_4 "\x1b[37;1m" +#define COL_INTEL_RETRO_1 "\x1b[36;1m" +#define COL_INTEL_RETRO_2 "\x1b[37;1m" +#define COL_AMD_FANCY_1 "\x1b[37;1m" +#define COL_AMD_FANCY_2 "\x1b[31;1m" +#define COL_AMD_FANCY_3 "\x1b[37;1m" +#define COL_AMD_FANCY_4 "\x1b[31;1m" +#define COL_AMD_RETRO_1 "\x1b[37;1m" +#define COL_AMD_RETRO_2 "\x1b[31;1m" +#define RESET "\x1b[m" #define TITLE_NAME "Name:" #define TITLE_FREQUENCY "Frequency:" @@ -74,8 +71,11 @@ static const int ATTRIBUTE_LIST[MAX_ATTRIBUTE_COUNT] = { ATTRIBUTE_NAME, ATTRIB struct ascii { char art[NUMBER_OF_LINES][LINE_SIZE]; - char color1[100]; - char color2[100]; + char color1_ascii[100]; + char color2_ascii[100]; + char color1_text[100]; + char color2_text[100]; + char ascii_chars[2]; char reset[100]; char* attributes[MAX_ATTRIBUTE_COUNT]; uint32_t n_attributes_set; @@ -87,17 +87,22 @@ void setAttribute(struct ascii* art, int type, char* value) { art->n_attributes_set++; } -char* rgb_to_ansi(struct color* c, bool bold) { +char* rgb_to_ansi(struct color* c, bool background, bool bold) { char* str = malloc(sizeof(char) * 100); - if(bold) - snprintf(str, 48, "\x1b[1m\x1b[38;2;%.3d;%.3d;%.3dm", c->R, c->G, c->B); - else - snprintf(str, 44, "\x1b[38;2;%.3d;%.3d;%.3dm", c->R, c->G, c->B); + if(background) { + snprintf(str, 44, "\x1b[48;2;%.3d;%.3d;%.3dm", c->R, c->G, c->B); + } + else { + if(bold) + snprintf(str, 48, "\x1b[1m\x1b[38;2;%.3d;%.3d;%.3dm", c->R, c->G, c->B); + else + snprintf(str, 44, "\x1b[38;2;%.3d;%.3d;%.3dm", c->R, c->G, c->B); + } return str; } -struct ascii* set_ascii(VENDOR cpuVendor, struct color* c1, struct color* c2) { +struct ascii* set_ascii(VENDOR cpuVendor, STYLE style, struct color* c1, struct color* c2) { // Sanity checks // for(int i=0; i < MAX_ATTRIBUTE_COUNT; i++) { if(ATTRIBUTE_FIELDS[i] == NULL) { @@ -110,7 +115,7 @@ struct ascii* set_ascii(VENDOR cpuVendor, struct color* c1, struct color* c2) { } } - char *COL_1, *COL_2; + char *COL_FANCY_1, *COL_FANCY_2, *COL_FANCY_3, *COL_FANCY_4, *COL_RETRO_1, *COL_RETRO_2; struct ascii* art = malloc(sizeof(struct ascii)); art->n_attributes_set = 0; art->vendor = cpuVendor; @@ -118,33 +123,74 @@ struct ascii* set_ascii(VENDOR cpuVendor, struct color* c1, struct color* c2) { art->attributes[i] = NULL; strcpy(art->reset,RESET); - if(c1 != NULL && c2 != NULL) { - COL_1 = rgb_to_ansi(c1, true); - COL_2 = rgb_to_ansi(c2, false); + if(cpuVendor == VENDOR_INTEL) { + COL_FANCY_1 = COL_INTEL_FANCY_1; + COL_FANCY_2 = COL_INTEL_FANCY_2; + COL_FANCY_3 = COL_INTEL_FANCY_3; + COL_FANCY_4 = COL_INTEL_FANCY_4; + COL_RETRO_1 = COL_INTEL_RETRO_1; + COL_RETRO_2 = COL_INTEL_RETRO_2; + art->ascii_chars[0] = '#'; } else { - if(cpuVendor == VENDOR_INTEL) { - COL_1 = COL_INTEL_DEFAULT_1; - COL_2 = COL_INTEL_DEFAULT_2; - } - else { - COL_1 = COL_AMD_DEFAULT_1; - COL_2 = COL_AMD_DEFAULT_2; - } + COL_FANCY_1 = COL_AMD_FANCY_1; + COL_FANCY_2 = COL_AMD_FANCY_2; + COL_FANCY_3 = COL_AMD_FANCY_3; + COL_FANCY_4 = COL_AMD_FANCY_4; + COL_RETRO_1 = COL_AMD_RETRO_1; + COL_RETRO_2 = COL_AMD_RETRO_2; + art->ascii_chars[0] = '@'; } + art->ascii_chars[1] = '#'; - #ifdef _WIN32 - strcpy(art->color1,COL_NONE); - strcpy(art->color2,COL_NONE); - art->reset[0] = '\0'; - #else - strcpy(art->color1,COL_1); - strcpy(art->color2,COL_2); - #endif - - if(c1 != NULL && c2 != NULL) { - free(COL_1); - free(COL_2); + switch(style) { + case STYLE_EMPTY: + #ifdef _WIN32 + strcpy(art->color1_ascii,COL_NONE); + strcpy(art->color2_ascii,COL_NONE); + strcpy(art->color1_text,COL_NONE); + strcpy(art->color2_text,COL_NONE); + art->reset[0] = '\0'; + break; + #endif + case STYLE_FANCY: + if(c1 != NULL && c2 != NULL) { + COL_FANCY_1 = rgb_to_ansi(c1, true, true); + COL_FANCY_2 = rgb_to_ansi(c2, true, true); + COL_FANCY_3 = rgb_to_ansi(c1, false, true); + COL_FANCY_4 = rgb_to_ansi(c2, false, true); + } + art->ascii_chars[0] = ' '; + art->ascii_chars[1] = ' '; + strcpy(art->color1_ascii,COL_FANCY_1); + strcpy(art->color2_ascii,COL_FANCY_2); + strcpy(art->color1_text,COL_FANCY_3); + strcpy(art->color2_text,COL_FANCY_4); + if(c1 != NULL && c2 != NULL) { + free(COL_FANCY_1); + free(COL_FANCY_2); + free(COL_FANCY_3); + free(COL_FANCY_4); + } + break; + case STYLE_RETRO: + if(c1 != NULL && c2 != NULL) { + COL_RETRO_1 = rgb_to_ansi(c1, false, true); + COL_RETRO_2 = rgb_to_ansi(c2, false, true); + } + strcpy(art->color1_ascii,COL_RETRO_1); + strcpy(art->color2_ascii,COL_RETRO_2); + strcpy(art->color1_text,COL_RETRO_1); + strcpy(art->color2_text,COL_RETRO_2); + if(c1 != NULL && c2 != NULL) { + free(COL_RETRO_1); + free(COL_RETRO_2); + } + break; + case STYLE_INVALID: + default: + printBug("Found invalid style (%d)",style); + return NULL; } char tmp[NUMBER_OF_LINES*LINE_SIZE]; @@ -162,7 +208,7 @@ uint32_t get_next_attribute(struct ascii* art, uint32_t last_attr) { return last_attr; } -void print_ascii_intel(struct ascii* art, uint32_t la) { +void print_ascii_intel(struct ascii* art, STYLE s, uint32_t la) { bool flag = false; int attr_to_print = -1; uint32_t space_right; @@ -175,15 +221,15 @@ void print_ascii_intel(struct ascii* art, uint32_t la) { if(flag) { if(art->art[n][i] == ' ') { flag = false; - printf("%c",' '); + printf("%s%c%s", art->color2_ascii, art->ascii_chars[1], art->reset); } else - printf("%s%c%s", art->color1, '#', art->reset); + printf("%s%c%s", art->color1_ascii, art->ascii_chars[0], art->reset); } else { if(art->art[n][i] != ' ' && art->art[n][i] != '\0') { flag = true; - printf("%s%c%s", art->color2, '#', art->reset); + printf("%c",' '); } else printf("%c",' '); @@ -193,13 +239,13 @@ void print_ascii_intel(struct ascii* art, uint32_t la) { if(n > space_up-1 && n < NUMBER_OF_LINES-space_down) { attr_to_print = get_next_attribute(art, attr_to_print); space_right = 1 + (la - strlen(ATTRIBUTE_FIELDS[attr_to_print])); - printf("%s%s%*s%s%s%s\n",art->color1, ATTRIBUTE_FIELDS[attr_to_print], space_right, "", art->color2, art->attributes[attr_to_print], art->reset); + printf("%s%s%s%*s%s%s%s\n",art->color1_text, ATTRIBUTE_FIELDS[attr_to_print], art->reset, space_right, "", art->color2_text, art->attributes[attr_to_print], art->reset); } else printf("\n"); } } -void print_ascii_amd(struct ascii* art, uint32_t la) { +void print_ascii_amd(struct ascii* art, STYLE s, uint32_t la) { int attr_to_print = -1; uint32_t space_right; uint32_t space_up = (NUMBER_OF_LINES - art->n_attributes_set)/2; @@ -208,9 +254,9 @@ void print_ascii_amd(struct ascii* art, uint32_t la) { for(uint32_t n=0;nart[n][i] == '@') - printf("%s%c%s", art->color1, art->art[n][i], art->reset); + printf("%s%c%s", art->color1_ascii, art->ascii_chars[0], art->reset); else if(art->art[n][i] == '#') - printf("%s%c%s", art->color2, art->art[n][i], art->reset); + printf("%s%c%s", art->color2_ascii, art->ascii_chars[1], art->reset); else printf("%c",art->art[n][i]); } @@ -218,7 +264,7 @@ void print_ascii_amd(struct ascii* art, uint32_t la) { if(n > space_up-1 && n < NUMBER_OF_LINES-space_down) { attr_to_print = get_next_attribute(art, attr_to_print); space_right = 1 + (la - strlen(ATTRIBUTE_FIELDS[attr_to_print])); - printf("%s%s%*s%s%s%s\n",art->color1, ATTRIBUTE_FIELDS[attr_to_print], space_right, "", art->color2, art->attributes[attr_to_print], art->reset); + printf("%s%s%s%*s%s%s%s\n",art->color1_text, ATTRIBUTE_FIELDS[attr_to_print], art->reset, space_right, "", art->color2_text, art->attributes[attr_to_print], art->reset); } else printf("\n"); } @@ -239,16 +285,16 @@ uint32_t longest_attribute_length(struct ascii* art) { return max; } -void print_ascii(struct ascii* art) { +void print_ascii(struct ascii* art, STYLE s) { uint32_t longest_attribute = longest_attribute_length(art); if(art->vendor == VENDOR_INTEL) - print_ascii_intel(art, longest_attribute); + print_ascii_intel(art, s, longest_attribute); else - print_ascii_amd(art, longest_attribute); + print_ascii_amd(art, s, longest_attribute); } -bool print_cpufetch(struct cpuInfo* cpu, struct cache* cach, struct frequency* freq, struct topology* topo, struct color* c1, struct color* c2) { - struct ascii* art = set_ascii(get_cpu_vendor(cpu), c1, c2); +bool print_cpufetch(struct cpuInfo* cpu, struct cache* cach, struct frequency* freq, struct topology* topo, STYLE s, struct color* c1, struct color* c2) { + struct ascii* art = set_ascii(get_cpu_vendor(cpu), s, c1, c2); if(art == NULL) return false; @@ -293,7 +339,7 @@ bool print_cpufetch(struct cpuInfo* cpu, struct cache* cach, struct frequency* f return false; } - print_ascii(art); + print_ascii(art, s); free(cpu_name); free(max_frequency); diff --git a/src/printer.h b/src/printer.h index 36d6ad4..2767628 100644 --- a/src/printer.h +++ b/src/printer.h @@ -1,9 +1,18 @@ #ifndef __PRINTER__ #define __PRINTER__ +typedef int STYLE; + #include "args.h" #include "cpuid.h" -bool print_cpufetch(struct cpuInfo* cpu, struct cache* cach, struct frequency* freq, struct topology* topo, struct color* c1, struct color* c2); +#define STYLES_COUNT 2 + +#define STYLE_INVALID -2 +#define STYLE_EMPTY -1 +#define STYLE_FANCY 0 +#define STYLE_RETRO 1 + +bool print_cpufetch(struct cpuInfo* cpu, struct cache* cach, struct frequency* freq, struct topology* topo, STYLE s, struct color* c1, struct color* c2); #endif