Add two different styles. The old one is now called retro, and the new one, which is the default, is called fancy

This commit is contained in:
Dr-Noob
2020-07-02 18:53:28 +02:00
parent 942a86c04f
commit ba047c76e3
6 changed files with 176 additions and 84 deletions

View File

@@ -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_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");

View File

@@ -4,14 +4,6 @@
#include <stdbool.h>
#include <stdint.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();
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

View File

@@ -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

View File

@@ -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;

View File

@@ -7,24 +7,21 @@
#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 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 COL_BACK_1 "\x1b[32;44m"
#define COL_BACK_2 "\x1b[32;47m"
#define TITLE_NAME "Name:"
#define TITLE_FREQUENCY "Frequency:"
#define TITLE_SOCKETS "Sockets:"
@@ -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(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);
}
else {
if(cpuVendor == VENDOR_INTEL) {
COL_1 = COL_INTEL_DEFAULT_1;
COL_2 = COL_INTEL_DEFAULT_2;
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 {
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] = '#';
switch(style) {
case STYLE_EMPTY:
#ifdef _WIN32
strcpy(art->color1,COL_NONE);
strcpy(art->color2,COL_NONE);
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';
#else
strcpy(art->color1,COL_1);
strcpy(art->color2,COL_2);
break;
#endif
case STYLE_FANCY:
if(c1 != NULL && c2 != NULL) {
free(COL_1);
free(COL_2);
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;n<NUMBER_OF_LINES;n++) {
for(int i=0;i<LINE_SIZE;i++) {
if(art->art[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);

View File

@@ -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