From 0cba7b434843ec99d7bea333d6e0df49a083e25d Mon Sep 17 00:00:00 2001 From: Dr-Noob Date: Sun, 21 Jun 2020 11:07:35 +0200 Subject: [PATCH] Add "none" style (useful for non colored terminals). Refactoring in printer --- src/args.c | 27 +++++++------- src/args.h | 2 +- src/ascii.h | 84 ++++++++++++++++++++++---------------------- src/main.c | 1 + src/printer.c | 97 +++++++++++++++++++-------------------------------- src/printer.h | 16 ++++----- 6 files changed, 101 insertions(+), 126 deletions(-) diff --git a/src/args.c b/src/args.c index 6000fcd..857c6a5 100644 --- a/src/args.c +++ b/src/args.c @@ -11,6 +11,7 @@ #define ARG_CHAR_VERSION 'v' #define STYLE_STR_1 "default" #define STYLE_STR_2 "dark" +#define STYLE_STR_3 "none" struct args_struct { int help_flag; @@ -18,7 +19,7 @@ struct args_struct { STYLE style; }; -static const char* SYTLES_STR_LIST[STYLES_COUNT] = { STYLE_STR_1, STYLE_STR_2 }; +static const char* SYTLES_STR_LIST[STYLES_COUNT] = { STYLE_STR_1, STYLE_STR_2, STYLE_STR_3 }; static struct args_struct args; STYLE parseStyle(char* style) { @@ -47,13 +48,13 @@ bool verbose_enabled() { return false; } -int parseArgs(int argc, char* argv[]) { +bool parseArgs(int argc, char* argv[]) { int c; int digit_optind = 0; int option_index = 0; opterr = 0; - args.help_flag = BOOLEAN_FALSE; + args.help_flag = false; args.style = STYLE_EMPTY; static struct option long_options[] = { @@ -69,31 +70,31 @@ int parseArgs(int argc, char* argv[]) { if(c == ARG_CHAR_STYLE) { if(args.style != STYLE_EMPTY) { printf("ERROR: Style option specified more than once\n"); - return BOOLEAN_FALSE; + return false; } args.style = parseStyle(optarg); if(args.style == STYLE_INVALID) { printf("ERROR: Invalid style '%s'\n",optarg); - return BOOLEAN_FALSE; + return false; } } else if(c == ARG_CHAR_HELP) { if(args.help_flag) { printf("ERROR: Help option specified more than once\n"); - return BOOLEAN_FALSE; + return false; } - args.help_flag = BOOLEAN_TRUE; + args.help_flag = true; } else if (c == ARG_CHAR_VERSION) { if(args.version_flag) { printf("ERROR: Version option specified more than once\n"); - return BOOLEAN_FALSE; + return false; } - args.version_flag = BOOLEAN_TRUE; + args.version_flag = true; } else if(c == '?') { printf("WARNING: Invalid options\n"); - args.help_flag = BOOLEAN_TRUE; + args.help_flag = true; break; } else @@ -105,13 +106,13 @@ int parseArgs(int argc, char* argv[]) { if (optind < argc) { printf("WARNING: Invalid options\n"); - args.help_flag = BOOLEAN_TRUE; + args.help_flag = true; } if((args.help_flag + args.version_flag + (args.style != STYLE_EMPTY)) > 1) { printf("WARNING: You should specify just one option\n"); - args.help_flag = BOOLEAN_TRUE; + args.help_flag = true; } - return BOOLEAN_TRUE; + return true; } diff --git a/src/args.h b/src/args.h index b945042..688fc48 100644 --- a/src/args.h +++ b/src/args.h @@ -4,7 +4,7 @@ #include #include "printer.h" -int parseArgs(int argc, char* argv[]); +bool parseArgs(int argc, char* argv[]); STYLE getStyle(); int showHelp(); int showVersion(); diff --git a/src/ascii.h b/src/ascii.h index 5e857da..604cf0a 100644 --- a/src/ascii.h +++ b/src/ascii.h @@ -2,48 +2,50 @@ #define __ASCII__ #define NUMBER_OF_LINES 20 -#define LINE_SIZE 63 +#define LINE_SIZE 62 -#define AMD1 " \0" -#define AMD2 " \0" -#define AMD3 " \0" -#define AMD4 " \0" -#define AMD5 " \0" -#define AMD6 " \0" -#define AMD7 " @@@@ @@@ @@@ @@@@@@@@ ############ \0" -#define AMD8 " @@@@@@ @@@@@ @@@@ @@@ @@@@ ########## \0" -#define AMD9 " @@@ @@@ @@@@@@@@@@@@@ @@@ @@ # #### \0" -#define AMD10 " @@@ @@@ @@@ @@@ @@@ @@@ @@@ ### #### \0" -#define AMD11 " @@@@@@@@@@@@ @@@ @@@ @@@ @@@ #### ## ### \0" -#define AMD12 " @@@ @@@ @@@ @@@ @@@@@@@@@ ######## ## \0" -#define AMD13 " \0" -#define AMD14 " \0" -#define AMD15 " \0" -#define AMD16 " \0" -#define AMD17 " \0" -#define AMD18 " \0" -#define AMD19 " \0" -#define AMD20 " \0" +#define AMD_ASCII \ +" \ + \ + \ + \ + \ + \ + @@@@ @@@ @@@ @@@@@@@@ ############ \ + @@@@@@ @@@@@ @@@@ @@@ @@@@ ########## \ + @@@ @@@ @@@@@@@@@@@@@ @@@ @@ # #### \ + @@@ @@@ @@@ @@@ @@@ @@@ @@@ ### #### \ + @@@@@@@@@@@@ @@@ @@@ @@@ @@@ #### ## ### \ + @@@ @@@ @@@ @@@ @@@@@@@@@ ######## ## \ + \ + \ + \ + \ + \ + \ + \ + " -#define INTEL1 " ################ \0" -#define INTEL2 " ####### ####### \0" -#define INTEL3 " #### #### \0" -#define INTEL4 " ### #### \0" -#define INTEL5 " ### ### \0" -#define INTEL6 " ### ### \0" -#define INTEL7 " # ### ### ### \0" -#define INTEL8 " ## ### ######### ###### ###### ### ### \0" -#define INTEL9 " ## ### ### ### ### #### #### ### ### \0" -#define INTEL10 " ## ### ### ### ### ### ### ### ### \0" -#define INTEL11 "## ### ### ### ### ########## ### #### \0" -#define INTEL12 "## ### ### ### ### ### ### ##### \0" -#define INTEL13 "## ## ### ### ##### ######### ## ### \0" -#define INTEL14 "### \0" -#define INTEL15 " ### \0" -#define INTEL16 " #### #### \0" -#define INTEL17 " ##### ########## \0" -#define INTEL18 " ########## ################ \0" -#define INTEL19 " ############################### \0" -#define INTEL20 " \0" +#define INTEL_ASCII \ +" ################ \ + ####### ####### \ + #### #### \ + ### #### \ + ### ### \ + ### ### \ + # ### ### ### \ + ## ### ######### ###### ###### ### ### \ + ## ### ### ### ### #### #### ### ### \ + ## ### ### ### ### ### ### ### ### \ +## ### ### ### ### ########## ### #### \ +## ### ### ### ### ### ### ##### \ +## ## ### ### ##### ######### ## ### \ +### \ + ### \ + #### #### \ + ##### ########## \ + ########## ################ \ + ############################### \ + " #endif diff --git a/src/main.c b/src/main.c index fb3f682..7058dc3 100644 --- a/src/main.c +++ b/src/main.c @@ -32,6 +32,7 @@ Options: \n\ --style Set logo style color\n\ default: Default style color\n\ dark: Dark style color\n\ + none: Don't use colors\n\ --help Print this help and exit\n\ --version Print cpufetch version and exit\n", argv[0]); diff --git a/src/printer.c b/src/printer.c index b69fea1..1ca92dc 100644 --- a/src/printer.c +++ b/src/printer.c @@ -1,20 +1,22 @@ #include #include #include +#include #include "printer.h" #include "ascii.h" #include "global.h" +#define COL_NONE "" #define COL_INTEL_DEFAULT_1 "\x1b[36;1m" #define COL_INTEL_DEFAULT_2 "\x1b[37;1m" -#define COL_INTEL_DARK_1 "\x1b[34;1m" -#define COL_INTEL_DARK_2 "\x1b[30m" -#define COL_AMD_DEFAULT_1 "\x1b[37;1m" -#define COL_AMD_DEFAULT_2 "\x1b[31;1m" -#define COL_AMD_DARK_1 "\x1b[30;1m" -#define COL_AMD_DARK_2 "\x1b[32;1m" -#define RESET "\x1b[0m" +#define COL_INTEL_DARK_1 "\x1b[34;1m" +#define COL_INTEL_DARK_2 "\x1b[30m" +#define COL_AMD_DEFAULT_1 "\x1b[37;1m" +#define COL_AMD_DEFAULT_2 "\x1b[31;1m" +#define COL_AMD_DARK_1 "\x1b[30;1m" +#define COL_AMD_DARK_2 "\x1b[32;1m" +#define RESET "\x1b[0m" #define TITLE_NAME "Name: " #define TITLE_FREQUENCY "Frequency: " @@ -53,14 +55,14 @@ struct ascii { VENDOR vendor; }; -int setAttribute(struct ascii* art, int type, char* value) { +void setAttribute(struct ascii* art, int type, char* value) { int i = 0; while(i < ATTRIBUTE_COUNT && type != ATTRIBUTE_LIST[i]) i++; - if(i == ATTRIBUTE_COUNT) - return BOOLEAN_FALSE; - art->atributes[i] = value; - return BOOLEAN_TRUE; + if(i != ATTRIBUTE_COUNT) + art->atributes[i] = value; + else + printBug("Setting attribute failed because it was not found"); } struct ascii* set_ascii(VENDOR cpuVendor, STYLE style) { @@ -73,9 +75,9 @@ struct ascii* set_ascii(VENDOR cpuVendor, STYLE style) { struct ascii* art = malloc(sizeof(struct ascii)); art->vendor = cpuVendor; + if(cpuVendor == VENDOR_INTEL) { - /*** CHECK STYLE ***/ - switch (style) { + switch (style) { case STYLE_EMPTY: case STYLE_DEFAULT: strcpy(art->color1,COL_INTEL_DEFAULT_1); @@ -85,35 +87,16 @@ struct ascii* set_ascii(VENDOR cpuVendor, STYLE style) { strcpy(art->color1,COL_INTEL_DARK_1); strcpy(art->color2,COL_INTEL_DARK_2); break; + case STYLE_NONE: + strcpy(art->color1,COL_NONE); + strcpy(art->color2,COL_NONE); + break; default: printBug("Found invalid style (%d)",style); return NULL; - } - - /*** COPY ASCII-ART ***/ - strcpy(art->art[0],INTEL1); - strcpy(art->art[1],INTEL2); - strcpy(art->art[2],INTEL3); - strcpy(art->art[3],INTEL4); - strcpy(art->art[4],INTEL5); - strcpy(art->art[5],INTEL6); - strcpy(art->art[6],INTEL7); - strcpy(art->art[7],INTEL8); - strcpy(art->art[8],INTEL9); - strcpy(art->art[9],INTEL10); - strcpy(art->art[10],INTEL11); - strcpy(art->art[11],INTEL12); - strcpy(art->art[12],INTEL13); - strcpy(art->art[13],INTEL14); - strcpy(art->art[14],INTEL15); - strcpy(art->art[15],INTEL16); - strcpy(art->art[16],INTEL17); - strcpy(art->art[17],INTEL18); - strcpy(art->art[18],INTEL19); - strcpy(art->art[19],INTEL20); + } } else { - /*** CHECK STYLE ***/ switch (style) { case STYLE_EMPTY: case STYLE_DEFAULT: @@ -124,37 +107,27 @@ struct ascii* set_ascii(VENDOR cpuVendor, STYLE style) { strcpy(art->color1,COL_AMD_DARK_1); strcpy(art->color2,COL_AMD_DARK_2); break; + case STYLE_NONE: + strcpy(art->color1,COL_NONE); + strcpy(art->color2,COL_NONE); + break; default: printBug("Found invalid style (%d)",style); return NULL; } - - strcpy(art->art[0],AMD1); - strcpy(art->art[1],AMD2); - strcpy(art->art[2],AMD3); - strcpy(art->art[3],AMD4); - strcpy(art->art[4],AMD5); - strcpy(art->art[5],AMD6); - strcpy(art->art[6],AMD7); - strcpy(art->art[7],AMD8); - strcpy(art->art[8],AMD9); - strcpy(art->art[9],AMD10); - strcpy(art->art[10],AMD11); - strcpy(art->art[11],AMD12); - strcpy(art->art[12],AMD13); - strcpy(art->art[13],AMD14); - strcpy(art->art[14],AMD15); - strcpy(art->art[15],AMD16); - strcpy(art->art[16],AMD17); - strcpy(art->art[17],AMD18); - strcpy(art->art[18],AMD19); - strcpy(art->art[19],AMD20); } + + char tmp[NUMBER_OF_LINES*LINE_SIZE]; + if(cpuVendor == VENDOR_INTEL) strcpy(tmp, INTEL_ASCII); + else strcpy(tmp, AMD_ASCII); + for(int i=0; i < NUMBER_OF_LINES; i++) + strncpy(art->art[i], tmp + i*LINE_SIZE, LINE_SIZE); + return art; } void print_ascii_intel(struct ascii* art) { - int flag = BOOLEAN_FALSE; + bool flag = false; for(int n=0;nart[n][i] == ' ') { - flag = BOOLEAN_FALSE; + flag = false; printf("%c",art->art[n][i]); } else @@ -170,7 +143,7 @@ void print_ascii_intel(struct ascii* art) { } else { if(art->art[n][i] != ' ') { - flag = BOOLEAN_TRUE; + flag = true; printf("%s%c" RESET,art->color2,art->art[n][i]); } else diff --git a/src/printer.h b/src/printer.h index 8667f68..220899c 100644 --- a/src/printer.h +++ b/src/printer.h @@ -4,10 +4,7 @@ #include "standart.h" #include "ascii.h" -#define BOOLEAN_TRUE 1 -#define BOOLEAN_FALSE 0 - -#define ATTRIBUTE_COUNT 12 +#define ATTRIBUTE_COUNT 12 #define ATTRIBUTE_NAME 0 #define ATTRIBUTE_FREQUENCY 1 #define ATTRIBUTE_NCORES 2 @@ -22,18 +19,19 @@ #define ATTRIBUTE_PEAK 11 typedef int STYLE; -#define STYLES_COUNT 2 +#define STYLES_COUNT 3 -#define STYLE_EMPTY -2 +#define STYLE_EMPTY -2 #define STYLE_INVALID -1 -#define STYLE_DEFAULT 0 -#define STYLE_DARK 1 +#define STYLE_DEFAULT 0 +#define STYLE_DARK 1 +#define STYLE_NONE 2 struct ascii; static const int STYLES_CODE_LIST [STYLES_COUNT] = {STYLE_DEFAULT, STYLE_DARK}; struct ascii* set_ascii(VENDOR cpuVendor, STYLE style); void print_ascii(struct ascii* art); -int setAttribute(struct ascii* art, int type, char* value); +void setAttribute(struct ascii* art, int type, char* value); #endif