mirror of
https://github.com/Dr-Noob/cpufetch.git
synced 2026-03-25 16:00:39 +01:00
Support for 4 colors with --color (2 for ascii, 2 for text)
This commit is contained in:
48
src/args.c
48
src/args.c
@@ -25,8 +25,7 @@ struct args_struct {
|
||||
bool help_flag;
|
||||
bool version_flag;
|
||||
STYLE style;
|
||||
struct color* color1;
|
||||
struct color* color2;
|
||||
struct colors* colors;
|
||||
};
|
||||
|
||||
static const char* SYTLES_STR_LIST[STYLES_COUNT] = { STYLE_STR_1, STYLE_STR_2 };
|
||||
@@ -36,12 +35,8 @@ STYLE get_style() {
|
||||
return args.style;
|
||||
}
|
||||
|
||||
struct color* get_color1() {
|
||||
return args.color1;
|
||||
}
|
||||
|
||||
struct color* get_color2() {
|
||||
return args.color2;
|
||||
struct colors* get_colors() {
|
||||
return args.colors;
|
||||
}
|
||||
|
||||
bool show_help() {
|
||||
@@ -70,18 +65,38 @@ STYLE parse_style(char* style) {
|
||||
return i;
|
||||
}
|
||||
|
||||
bool parse_color(char* optarg, struct color** c1, struct color** c2) {
|
||||
*c1 = malloc(sizeof(struct color));
|
||||
*c2 = malloc(sizeof(struct color));
|
||||
void free_colors_struct(struct colors* cs) {
|
||||
free(cs->c1);
|
||||
free(cs->c2);
|
||||
free(cs->c3);
|
||||
free(cs->c4);
|
||||
free(cs);
|
||||
}
|
||||
|
||||
bool parse_color(char* optarg, struct colors** cs) {
|
||||
*cs = malloc(sizeof(struct colors));
|
||||
(*cs)->c1 = malloc(sizeof(struct color));
|
||||
(*cs)->c2 = malloc(sizeof(struct color));
|
||||
(*cs)->c3 = malloc(sizeof(struct color));
|
||||
(*cs)->c4 = malloc(sizeof(struct color));
|
||||
struct color** c1 = &((*cs)->c1);
|
||||
struct color** c2 = &((*cs)->c2);
|
||||
struct color** c3 = &((*cs)->c3);
|
||||
struct color** c4 = &((*cs)->c4);
|
||||
int32_t ret;
|
||||
|
||||
ret = sscanf(optarg, "%d,%d,%d:%d,%d,%d", &(*c1)->R, &(*c1)->G, &(*c1)->B, &(*c2)->R, &(*c2)->G, &(*c2)->B);
|
||||
ret = sscanf(optarg, "%d,%d,%d:%d,%d,%d:%d,%d,%d:%d,%d,%d",
|
||||
&(*c1)->R, &(*c1)->G, &(*c1)->B,
|
||||
&(*c2)->R, &(*c2)->G, &(*c2)->B,
|
||||
&(*c3)->R, &(*c3)->G, &(*c3)->B,
|
||||
&(*c4)->R, &(*c4)->G, &(*c4)->B);
|
||||
|
||||
if(ret != 6) {
|
||||
printErr("Expected to read 6 values for color 1 but read %d", ret);
|
||||
if(ret != 12) {
|
||||
printErr("Expected to read 12 values for color 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;
|
||||
@@ -105,7 +120,7 @@ bool parse_color(char* optarg, struct color** c1, struct color** c2) {
|
||||
if((*c2)->B < 0 || (*c2)->B > 255) {
|
||||
printErr("Blue in color 2 is invalid. Must be in range (0, 255)");
|
||||
return false;
|
||||
}
|
||||
}*/
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -120,6 +135,7 @@ bool parse_args(int argc, char* argv[]) {
|
||||
args.levels_flag = false;
|
||||
args.help_flag = false;
|
||||
args.style = STYLE_EMPTY;
|
||||
args.colors = NULL;
|
||||
|
||||
static struct option long_options[] = {
|
||||
{ARG_STR_STYLE, required_argument, 0, ARG_CHAR_STYLE },
|
||||
@@ -139,7 +155,7 @@ bool parse_args(int argc, char* argv[]) {
|
||||
return false;
|
||||
}
|
||||
color_flag = true;
|
||||
if(!parse_color(optarg, &args.color1, &args.color2)) {
|
||||
if(!parse_color(optarg, &args.colors)) {
|
||||
printErr("Color parsing failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
11
src/args.h
11
src/args.h
@@ -10,6 +10,13 @@ struct color {
|
||||
int32_t B;
|
||||
};
|
||||
|
||||
struct colors {
|
||||
struct color* c1;
|
||||
struct color* c2;
|
||||
struct color* c3;
|
||||
struct color* c4;
|
||||
};
|
||||
|
||||
#include "printer.h"
|
||||
|
||||
bool parse_args(int argc, char* argv[]);
|
||||
@@ -17,8 +24,8 @@ bool show_help();
|
||||
bool show_levels();
|
||||
bool show_version();
|
||||
bool verbose_enabled();
|
||||
struct color* get_color1();
|
||||
struct color* get_color2();
|
||||
void free_colors_struct(struct colors* cs);
|
||||
struct colors* get_colors();
|
||||
STYLE get_style();
|
||||
|
||||
#endif
|
||||
|
||||
10
src/main.c
10
src/main.c
@@ -6,12 +6,14 @@
|
||||
#include "cpuid.h"
|
||||
#include "global.h"
|
||||
|
||||
static const char* VERSION = "0.52";
|
||||
static const char* VERSION = "0.53";
|
||||
|
||||
void print_help(char *argv[]) {
|
||||
printf("Usage: %s [--version] [--help] [--levels] [--style STYLE] [--color 'R,G,B:R,G,B']\n\
|
||||
printf("Usage: %s [--version] [--help] [--levels] [--style fancy|retro] [--color 'R,G,B:R,G,B: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\
|
||||
--color Set text color. 4 colors (in RGB format) must be specified in the form: R,G,B:R,G,B:...\n\
|
||||
These colors correspond to the ASCII art color (2 colors) and for the text colors (next 2)\n\
|
||||
Suggested color (Intel): --color 15,125,194:230,230,230:40,150,220:230,230,230\n\
|
||||
--style Set the style of the ASCII art:\n\
|
||||
* fancy \n\
|
||||
* retro \n\
|
||||
@@ -63,7 +65,7 @@ int main(int argc, char* argv[]) {
|
||||
if(topo == NULL)
|
||||
return EXIT_FAILURE;
|
||||
|
||||
if(print_cpufetch(cpu, cach, freq, topo, get_style(), get_color1(), get_color2()))
|
||||
if(print_cpufetch(cpu, cach, freq, topo, get_style(), get_colors()))
|
||||
return EXIT_SUCCESS;
|
||||
else
|
||||
return EXIT_FAILURE;
|
||||
|
||||
@@ -102,7 +102,7 @@ char* rgb_to_ansi(struct color* c, bool background, bool bold) {
|
||||
return str;
|
||||
}
|
||||
|
||||
struct ascii* set_ascii(VENDOR cpuVendor, STYLE style, struct color* c1, struct color* c2) {
|
||||
struct ascii* set_ascii(VENDOR cpuVendor, STYLE style, struct colors* cs) {
|
||||
// Sanity checks //
|
||||
for(int i=0; i < MAX_ATTRIBUTE_COUNT; i++) {
|
||||
if(ATTRIBUTE_FIELDS[i] == NULL) {
|
||||
@@ -115,7 +115,7 @@ struct ascii* set_ascii(VENDOR cpuVendor, STYLE style, struct color* c1, struct
|
||||
}
|
||||
}
|
||||
|
||||
char *COL_FANCY_1, *COL_FANCY_2, *COL_FANCY_3, *COL_FANCY_4, *COL_RETRO_1, *COL_RETRO_2;
|
||||
char *COL_FANCY_1, *COL_FANCY_2, *COL_FANCY_3, *COL_FANCY_4, *COL_RETRO_1, *COL_RETRO_2, *COL_RETRO_3, *COL_RETRO_4;
|
||||
struct ascii* art = malloc(sizeof(struct ascii));
|
||||
art->n_attributes_set = 0;
|
||||
art->vendor = cpuVendor;
|
||||
@@ -130,6 +130,8 @@ struct ascii* set_ascii(VENDOR cpuVendor, STYLE style, struct color* c1, struct
|
||||
COL_FANCY_4 = COL_INTEL_FANCY_4;
|
||||
COL_RETRO_1 = COL_INTEL_RETRO_1;
|
||||
COL_RETRO_2 = COL_INTEL_RETRO_2;
|
||||
COL_RETRO_3 = COL_INTEL_RETRO_1;
|
||||
COL_RETRO_4 = COL_INTEL_RETRO_2;
|
||||
art->ascii_chars[0] = '#';
|
||||
}
|
||||
else {
|
||||
@@ -139,6 +141,8 @@ struct ascii* set_ascii(VENDOR cpuVendor, STYLE style, struct color* c1, struct
|
||||
COL_FANCY_4 = COL_AMD_FANCY_4;
|
||||
COL_RETRO_1 = COL_AMD_RETRO_1;
|
||||
COL_RETRO_2 = COL_AMD_RETRO_2;
|
||||
COL_RETRO_3 = COL_AMD_RETRO_1;
|
||||
COL_RETRO_4 = COL_AMD_RETRO_2;
|
||||
art->ascii_chars[0] = '@';
|
||||
}
|
||||
art->ascii_chars[1] = '#';
|
||||
@@ -154,11 +158,11 @@ struct ascii* set_ascii(VENDOR cpuVendor, STYLE style, struct color* c1, struct
|
||||
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);
|
||||
if(cs != NULL) {
|
||||
COL_FANCY_1 = rgb_to_ansi(cs->c1, true, true);
|
||||
COL_FANCY_2 = rgb_to_ansi(cs->c2, true, true);
|
||||
COL_FANCY_3 = rgb_to_ansi(cs->c3, false, true);
|
||||
COL_FANCY_4 = rgb_to_ansi(cs->c4, false, true);
|
||||
}
|
||||
art->ascii_chars[0] = ' ';
|
||||
art->ascii_chars[1] = ' ';
|
||||
@@ -166,7 +170,7 @@ struct ascii* set_ascii(VENDOR cpuVendor, STYLE style, struct color* c1, struct
|
||||
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) {
|
||||
if(cs != NULL) {
|
||||
free(COL_FANCY_1);
|
||||
free(COL_FANCY_2);
|
||||
free(COL_FANCY_3);
|
||||
@@ -174,17 +178,21 @@ struct ascii* set_ascii(VENDOR cpuVendor, STYLE style, struct color* c1, struct
|
||||
}
|
||||
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);
|
||||
if(cs != NULL) {
|
||||
COL_RETRO_1 = rgb_to_ansi(cs->c1, false, true);
|
||||
COL_RETRO_2 = rgb_to_ansi(cs->c2, false, true);
|
||||
COL_RETRO_3 = rgb_to_ansi(cs->c3, false, true);
|
||||
COL_RETRO_4 = rgb_to_ansi(cs->c4, 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) {
|
||||
strcpy(art->color1_text,COL_RETRO_3);
|
||||
strcpy(art->color2_text,COL_RETRO_4);
|
||||
if(cs != NULL) {
|
||||
free(COL_RETRO_1);
|
||||
free(COL_RETRO_2);
|
||||
free(COL_RETRO_3);
|
||||
free(COL_RETRO_4);
|
||||
}
|
||||
break;
|
||||
case STYLE_INVALID:
|
||||
@@ -293,8 +301,8 @@ void print_ascii(struct ascii* art, STYLE s) {
|
||||
print_ascii_amd(art, s, longest_attribute);
|
||||
}
|
||||
|
||||
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);
|
||||
bool print_cpufetch(struct cpuInfo* cpu, struct cache* cach, struct frequency* freq, struct topology* topo, STYLE s, struct colors* cs) {
|
||||
struct ascii* art = set_ascii(get_cpu_vendor(cpu), s, cs);
|
||||
if(art == NULL)
|
||||
return false;
|
||||
|
||||
@@ -359,6 +367,7 @@ bool print_cpufetch(struct cpuInfo* cpu, struct cache* cach, struct frequency* f
|
||||
|
||||
free(cpu);
|
||||
free(art);
|
||||
if(cs != NULL) free_colors_struct(cs);
|
||||
free_cache_struct(cach);
|
||||
free_topo_struct(topo);
|
||||
free_freq_struct(freq);
|
||||
|
||||
@@ -13,6 +13,6 @@ typedef int STYLE;
|
||||
#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);
|
||||
bool print_cpufetch(struct cpuInfo* cpu, struct cache* cach, struct frequency* freq, struct topology* topo, STYLE s, struct colors* cs);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user