Remove styles and add option to specify custom color output in RGB format

This commit is contained in:
Dr-Noob
2020-07-02 16:14:37 +02:00
parent ea338a68c8
commit 942a86c04f
6 changed files with 143 additions and 107 deletions

View File

@@ -1,42 +1,35 @@
#include <getopt.h> #include <getopt.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h>
#include "args.h" #include "args.h"
#include "global.h"
#define ARG_STR_STYLE "style" #define ARG_STR_COLOR "color"
#define ARG_STR_HELP "help" #define ARG_STR_HELP "help"
#define ARG_STR_LEVELS "levels" #define ARG_STR_LEVELS "levels"
#define ARG_STR_VERSION "version" #define ARG_STR_VERSION "version"
#define ARG_CHAR_STYLE 's' #define ARG_CHAR_COLOR 'c'
#define ARG_CHAR_HELP 'h' #define ARG_CHAR_HELP 'h'
#define ARG_CHAR_LEVELS 'l' #define ARG_CHAR_LEVELS 'l'
#define ARG_CHAR_VERSION 'v' #define ARG_CHAR_VERSION 'v'
#define STYLE_STR_1 "default"
#define STYLE_STR_2 "dark"
#define STYLE_STR_3 "none"
struct args_struct { struct args_struct {
bool levels_flag; bool levels_flag;
bool help_flag; bool help_flag;
bool version_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, STYLE_STR_3 };
static struct args_struct args; static struct args_struct args;
STYLE parse_style(char* style) { struct color* get_color1() {
int i = 0; return args.color1;
while(i != STYLES_COUNT && strcmp(SYTLES_STR_LIST[i],style) != 0)
i++;
if(i == STYLES_COUNT)
return STYLE_INVALID;
return i;
} }
STYLE get_style() { struct color* get_color2() {
return args.style; return args.color2;
} }
bool show_help() { bool show_help() {
@@ -55,18 +48,58 @@ bool verbose_enabled() {
return false; return false;
} }
bool parse_color(char* optarg, struct color** c1, struct color** c2) {
*c1 = malloc(sizeof(struct color));
*c2 = malloc(sizeof(struct color));
int32_t ret;
ret = sscanf(optarg, "%d,%d,%d:%d,%d,%d", &(*c1)->R, &(*c1)->G, &(*c1)->B, &(*c2)->R, &(*c2)->G, &(*c2)->B);
if(ret != 6) {
printErr("Expected to read 6 values for color 1 but read %d", ret);
return false;
}
if((*c1)->R < 0 || (*c1)->R > 255) {
printErr("Red in color 1 is invalid. Must be in range (0, 255)");
return false;
}
if((*c1)->G < 0 || (*c1)->G > 255) {
printErr("Green in color 1 is invalid. Must be in range (0, 255)");
return false;
}
if((*c1)->B < 0 || (*c1)->B > 255) {
printErr("Blue in color 1 is invalid. Must be in range (0, 255)");
return false;
}
if((*c2)->R < 0 || (*c2)->R > 255) {
printErr("Red in color 2 is invalid. Must be in range (0, 255)");
return false;
}
if((*c2)->G < 0 || (*c2)->G > 255) {
printErr("Green in color 2 is invalid. Must be in range (0, 255)");
return false;
}
if((*c2)->B < 0 || (*c2)->B > 255) {
printErr("Blue in color 2 is invalid. Must be in range (0, 255)");
return false;
}
return true;
}
bool parse_args(int argc, char* argv[]) { bool parse_args(int argc, char* argv[]) {
int c; int c;
int digit_optind = 0; int digit_optind = 0;
int option_index = 0; int option_index = 0;
opterr = 0; opterr = 0;
bool color_flag = false;
args.levels_flag = false; args.levels_flag = false;
args.help_flag = false; args.help_flag = false;
args.style = STYLE_EMPTY;
static struct option long_options[] = { 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_HELP, no_argument, 0, ARG_CHAR_HELP },
{ARG_STR_LEVELS, no_argument, 0, ARG_CHAR_LEVELS }, {ARG_STR_LEVELS, no_argument, 0, ARG_CHAR_LEVELS },
{ARG_STR_VERSION, no_argument, 0, ARG_CHAR_VERSION }, {ARG_STR_VERSION, no_argument, 0, ARG_CHAR_VERSION },
@@ -76,57 +109,57 @@ bool parse_args(int argc, char* argv[]) {
c = getopt_long(argc, argv,"",long_options, &option_index); c = getopt_long(argc, argv,"",long_options, &option_index);
while (c != -1) { while (c != -1) {
if(c == ARG_CHAR_STYLE) { if(c == ARG_CHAR_COLOR) {
if(args.style != STYLE_EMPTY) { if(color_flag) {
printf("ERROR: Style option specified more than once\n"); printErr("Color option specified more than once");
return false; return false;
} }
args.style = parse_style(optarg); color_flag = true;
if(args.style == STYLE_INVALID) { if(!parse_color(optarg, &args.color1, &args.color2)) {
printf("ERROR: Invalid style '%s'\n",optarg); printErr("Color parsing failed");
return false; return false;
} }
} }
else if(c == ARG_CHAR_HELP) { else if(c == ARG_CHAR_HELP) {
if(args.help_flag) { if(args.help_flag) {
printf("ERROR: Help option specified more than once\n"); printErr("Help option specified more than once");
return false; return false;
} }
args.help_flag = true; args.help_flag = true;
} }
else if(c == ARG_CHAR_LEVELS) { else if(c == ARG_CHAR_LEVELS) {
if(args.levels_flag) { if(args.levels_flag) {
printf("ERROR: Levels option specified more than once\n"); printErr("Levels option specified more than once");
return false; return false;
} }
args.levels_flag = true; args.levels_flag = true;
} }
else if (c == ARG_CHAR_VERSION) { else if (c == ARG_CHAR_VERSION) {
if(args.version_flag) { if(args.version_flag) {
printf("ERROR: Version option specified more than once\n"); printErr("Version option specified more than once");
return false; return false;
} }
args.version_flag = true; args.version_flag = true;
} }
else if(c == '?') { else if(c == '?') {
printf("WARNING: Invalid options\n"); printWarn("Invalid options");
args.help_flag = true; args.help_flag = true;
break; break;
} }
else else
printf("Bug at line number %d in file %s\n", __LINE__, __FILE__); printBug("Bug at line number %d in file %s", __LINE__, __FILE__);
option_index = 0; option_index = 0;
c = getopt_long(argc, argv,"",long_options, &option_index); c = getopt_long(argc, argv,"",long_options, &option_index);
} }
if (optind < argc) { if (optind < argc) {
printf("WARNING: Invalid options\n"); printWarn("Invalid options");
args.help_flag = true; args.help_flag = true;
} }
if((args.help_flag + args.version_flag + (args.style != STYLE_EMPTY)) > 1) { if((args.help_flag + args.version_flag + color_flag) > 1) {
printf("WARNING: You should specify just one option\n"); printWarn("You should specify just one option");
args.help_flag = true; args.help_flag = true;
} }

View File

@@ -2,13 +2,22 @@
#define __ARGS__ #define __ARGS__
#include <stdbool.h> #include <stdbool.h>
#include "printer.h" #include <stdint.h>
bool parse_args(int argc, char* argv[]); bool parse_args(int argc, char* argv[]);
STYLE get_style();
bool show_help(); bool show_help();
bool show_levels(); bool show_levels();
bool show_version(); bool show_version();
bool verbose_enabled(); bool verbose_enabled();
struct color* get_color1();
struct color* get_color2();
struct color {
int32_t R;
int32_t G;
int32_t B;
};
#include "printer.h"
#endif #endif

View File

@@ -24,7 +24,7 @@
\ \
\ \
\ \
" "
#define INTEL_ASCII \ #define INTEL_ASCII \
" ################ \ " ################ \
@@ -46,6 +46,6 @@
##### ########## \ ##### ########## \
########## ################ \ ########## ################ \
############################### \ ############################### \
" "
#endif #endif

View File

@@ -6,15 +6,12 @@
#include "cpuid.h" #include "cpuid.h"
#include "global.h" #include "global.h"
static const char* VERSION = "0.5"; static const char* VERSION = "0.51";
void print_help(char *argv[]) { void print_help(char *argv[]) {
printf("Usage: %s [--version] [--help] [--style STYLE]\n\ printf("Usage: %s [--version] [--help] [--style STYLE]\n\
Options: \n\ Options: \n\
--style Set logo style color\n\ --color Set text color. Two colors (in RGB format) must be specified in the form: R,G,B:R,G,B\n\
default: Default style color\n\
dark: Dark style color\n\
none: Don't use colors\n\
--help Prints this help and exit\n\ --help Prints this help and exit\n\
--levels Prints CPU model and cpuid levels (debug purposes)\n\ --levels Prints CPU model and cpuid levels (debug purposes)\n\
--version Prints cpufetch version and exit\n", --version Prints cpufetch version and exit\n",
@@ -63,7 +60,7 @@ int main(int argc, char* argv[]) {
if(topo == NULL) if(topo == NULL)
return EXIT_FAILURE; return EXIT_FAILURE;
if(print_cpufetch(cpu, cach, freq, topo, get_style())) if(print_cpufetch(cpu, cach, freq, topo, get_color1(), get_color2()))
return EXIT_SUCCESS; return EXIT_SUCCESS;
else else
return EXIT_FAILURE; return EXIT_FAILURE;

View File

@@ -7,16 +7,23 @@
#include "ascii.h" #include "ascii.h"
#include "global.h" #include "global.h"
/* style: retro (#)
fancy ( )
default -> fancy
color: default -> amd / intel
blue
blue_dark
*/
#define COL_NONE "" #define COL_NONE ""
#define COL_INTEL_DEFAULT_1 "\x1b[36;1m" #define COL_INTEL_DEFAULT_1 "\x1b[36;1m"
#define COL_INTEL_DEFAULT_2 "\x1b[37;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_1 "\x1b[37;1m"
#define COL_AMD_DEFAULT_2 "\x1b[31;1m" #define COL_AMD_DEFAULT_2 "\x1b[31;1m"
#define COL_AMD_DARK_1 "\x1b[30;1m" #define RESET "\x1b[m"
#define COL_AMD_DARK_2 "\x1b[32;1m"
#define RESET "\x1b[0m" #define COL_BACK_1 "\x1b[32;44m"
#define COL_BACK_2 "\x1b[32;47m"
#define TITLE_NAME "Name:" #define TITLE_NAME "Name:"
#define TITLE_FREQUENCY "Frequency:" #define TITLE_FREQUENCY "Frequency:"
@@ -51,8 +58,6 @@
#define ATTRIBUTE_L3 13 #define ATTRIBUTE_L3 13
#define ATTRIBUTE_PEAK 14 #define ATTRIBUTE_PEAK 14
static const int STYLES_CODE_LIST [STYLES_COUNT] = {STYLE_DEFAULT, STYLE_DARK};
static const char* ATTRIBUTE_FIELDS [MAX_ATTRIBUTE_COUNT] = { TITLE_NAME, TITLE_FREQUENCY, TITLE_SOCKETS, static const char* ATTRIBUTE_FIELDS [MAX_ATTRIBUTE_COUNT] = { TITLE_NAME, TITLE_FREQUENCY, TITLE_SOCKETS,
TITLE_NCORES, TITLE_NCORES_DUAL, TITLE_NCORES, TITLE_NCORES_DUAL,
TITLE_AVX, TITLE_SSE, TITLE_AVX, TITLE_SSE,
@@ -69,9 +74,9 @@ static const int ATTRIBUTE_LIST[MAX_ATTRIBUTE_COUNT] = { ATTRIBUTE_NAME, ATTRIB
struct ascii { struct ascii {
char art[NUMBER_OF_LINES][LINE_SIZE]; char art[NUMBER_OF_LINES][LINE_SIZE];
char color1[10]; char color1[100];
char color2[10]; char color2[100];
char reset[10]; char reset[100];
char* attributes[MAX_ATTRIBUTE_COUNT]; char* attributes[MAX_ATTRIBUTE_COUNT];
uint32_t n_attributes_set; uint32_t n_attributes_set;
VENDOR vendor; VENDOR vendor;
@@ -82,7 +87,17 @@ void setAttribute(struct ascii* art, int type, char* value) {
art->n_attributes_set++; art->n_attributes_set++;
} }
struct ascii* set_ascii(VENDOR cpuVendor, STYLE style) { char* rgb_to_ansi(struct color* c, 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);
return str;
}
struct ascii* set_ascii(VENDOR cpuVendor, struct color* c1, struct color* c2) {
// Sanity checks // // Sanity checks //
for(int i=0; i < MAX_ATTRIBUTE_COUNT; i++) { for(int i=0; i < MAX_ATTRIBUTE_COUNT; i++) {
if(ATTRIBUTE_FIELDS[i] == NULL) { if(ATTRIBUTE_FIELDS[i] == NULL) {
@@ -95,7 +110,7 @@ struct ascii* set_ascii(VENDOR cpuVendor, STYLE style) {
} }
} }
char *COL_DEFAULT_1, *COL_DEFAULT_2, *COL_DARK_1, *COL_DARK_2; char *COL_1, *COL_2;
struct ascii* art = malloc(sizeof(struct ascii)); struct ascii* art = malloc(sizeof(struct ascii));
art->n_attributes_set = 0; art->n_attributes_set = 0;
art->vendor = cpuVendor; art->vendor = cpuVendor;
@@ -103,42 +118,33 @@ struct ascii* set_ascii(VENDOR cpuVendor, STYLE style) {
art->attributes[i] = NULL; art->attributes[i] = NULL;
strcpy(art->reset,RESET); strcpy(art->reset,RESET);
if(cpuVendor == VENDOR_INTEL) { if(c1 != NULL && c2 != NULL) {
COL_DEFAULT_1 = COL_INTEL_DEFAULT_1; COL_1 = rgb_to_ansi(c1, true);
COL_DEFAULT_2 = COL_INTEL_DEFAULT_2; COL_2 = rgb_to_ansi(c2, false);
COL_DARK_1 = COL_INTEL_DARK_1;
COL_DARK_2 = COL_INTEL_DARK_2;
} }
else { else {
COL_DEFAULT_1 = COL_AMD_DEFAULT_1; if(cpuVendor == VENDOR_INTEL) {
COL_DEFAULT_2 = COL_AMD_DEFAULT_2; COL_1 = COL_INTEL_DEFAULT_1;
COL_DARK_1 = COL_AMD_DARK_1; COL_2 = COL_INTEL_DEFAULT_2;
COL_DARK_2 = COL_AMD_DARK_2; }
else {
COL_1 = COL_AMD_DEFAULT_1;
COL_2 = COL_AMD_DEFAULT_2;
}
} }
switch(style) { #ifdef _WIN32
case STYLE_NONE: strcpy(art->color1,COL_NONE);
strcpy(art->color1,COL_NONE); strcpy(art->color2,COL_NONE);
strcpy(art->color2,COL_NONE); art->reset[0] = '\0';
break; #else
case STYLE_EMPTY: strcpy(art->color1,COL_1);
#ifdef _WIN32 strcpy(art->color2,COL_2);
strcpy(art->color1,COL_NONE); #endif
strcpy(art->color2,COL_NONE);
art->reset[0] = '\0'; if(c1 != NULL && c2 != NULL) {
break; free(COL_1);
#endif free(COL_2);
case STYLE_DEFAULT:
strcpy(art->color1,COL_DEFAULT_1);
strcpy(art->color2,COL_DEFAULT_2);
break;
case STYLE_DARK:
strcpy(art->color1,COL_DARK_1);
strcpy(art->color2,COL_DARK_2);
break;
default:
printBug("Found invalid style (%d)",style);
return NULL;
} }
char tmp[NUMBER_OF_LINES*LINE_SIZE]; char tmp[NUMBER_OF_LINES*LINE_SIZE];
@@ -169,21 +175,20 @@ void print_ascii_intel(struct ascii* art, uint32_t la) {
if(flag) { if(flag) {
if(art->art[n][i] == ' ') { if(art->art[n][i] == ' ') {
flag = false; flag = false;
printf("%c",art->art[n][i]); printf("%c",' ');
} }
else else
printf("%s%c%s", art->color1, art->art[n][i], art->reset); printf("%s%c%s", art->color1, '#', art->reset);
} }
else { else {
if(art->art[n][i] != ' ') { if(art->art[n][i] != ' ' && art->art[n][i] != '\0') {
flag = true; flag = true;
printf("%s%c%s", art->color2, art->art[n][i], art->reset); printf("%s%c%s", art->color2, '#', art->reset);
} }
else else
printf("%c",art->art[n][i]); printf("%c",' ');
} }
} }
if(n > space_up-1 && n < NUMBER_OF_LINES-space_down) { if(n > space_up-1 && n < NUMBER_OF_LINES-space_down) {
attr_to_print = get_next_attribute(art, attr_to_print); attr_to_print = get_next_attribute(art, attr_to_print);
@@ -242,8 +247,8 @@ void print_ascii(struct ascii* art) {
print_ascii_amd(art, longest_attribute); print_ascii_amd(art, longest_attribute);
} }
bool print_cpufetch(struct cpuInfo* cpu, struct cache* cach, struct frequency* freq, struct topology* topo, STYLE s) { 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), s); struct ascii* art = set_ascii(get_cpu_vendor(cpu), c1, c2);
if(art == NULL) if(art == NULL)
return false; return false;

View File

@@ -1,17 +1,9 @@
#ifndef __PRINTER__ #ifndef __PRINTER__
#define __PRINTER__ #define __PRINTER__
#include "args.h"
#include "cpuid.h" #include "cpuid.h"
typedef int STYLE; bool print_cpufetch(struct cpuInfo* cpu, struct cache* cach, struct frequency* freq, struct topology* topo, struct color* c1, struct color* c2);
#define STYLES_COUNT 3
#define STYLE_EMPTY -2
#define STYLE_INVALID -1
#define STYLE_DEFAULT 0
#define STYLE_DARK 1
#define STYLE_NONE 2
bool print_cpufetch(struct cpuInfo* cpu, struct cache* cach, struct frequency* freq, struct topology* topo, STYLE s);
#endif #endif