From b7c32fcd4acbca3bb85fc2086a362b4826f423c3 Mon Sep 17 00:00:00 2001 From: Dr-Noob Date: Sun, 8 Aug 2021 12:53:16 +0200 Subject: [PATCH] [v0.99] Center automatically logo and text, when logo is longer than text and viceversa (before this commit only the first case was supported) --- src/common/ascii.h | 19 +++---------------- src/common/printer.c | 35 +++++++++++++++++++---------------- 2 files changed, 22 insertions(+), 32 deletions(-) diff --git a/src/common/ascii.h b/src/common/ascii.h index 2fbb39c..9f4d7a6 100644 --- a/src/common/ascii.h +++ b/src/common/ascii.h @@ -19,25 +19,12 @@ struct ascii_logo { }; #define ASCII_AMD \ -" \ - \ - \ - \ - \ - \ -$C1 @@@@ @@@ @@@ @@@@@@@@ $C2############ \ +"$C1 @@@@ @@@ @@@ @@@@@@@@ $C2############ \ $C1 @@@@@@ @@@@@ @@@@@ @@@ @@@ $C2########## \ $C1 @@@ @@@ @@@@@@@@@@@@@ @@@ @@ $C2# ##### \ $C1 @@@ @@@ @@@ @@@ @@@ @@@ @@ $C2### ##### \ $C1 @@@@@@@@@@@@ @@@ @@@ @@@ @@@ $C2######### ### \ -$C1 @@@ @@@ @@@ @@@ @@@@@@@@@ $C2######## ## \ - \ - \ - \ - \ - \ - \ - " +$C1 @@@ @@@ @@@ @@@ @@@@@@@@@ $C2######## ## " // 1 2 3 4 5 6 // 123456789012345678901234567890123456789012345678901234567890 @@ -234,7 +221,7 @@ $C1 ######################### \ // LOGO W H COLORS LOGO (>0 && <10) COLORS TEXT (=2) // -------------------------------------------------------------------------------- -static struct ascii_logo logo_amd = { ASCII_AMD, 62, 19, {COLOR_WHITE, COLOR_GREEN}, {COLOR_WHITE, COLOR_GREEN} }; +static struct ascii_logo logo_amd = { ASCII_AMD, 62, 6, {COLOR_WHITE, COLOR_GREEN}, {COLOR_WHITE, COLOR_GREEN} }; static struct ascii_logo logo_intel = { ASCII_INTEL, 62, 19, {COLOR_CYAN}, {COLOR_CYAN, COLOR_WHITE} }; static struct ascii_logo logo_snapd = { ASCII_SNAPD, 62, 19, {COLOR_RED, COLOR_WHITE}, {COLOR_RED, COLOR_WHITE} }; static struct ascii_logo logo_mtk = { ASCII_MTK, 62, 19, {COLOR_BLUE, COLOR_YELLOW}, {COLOR_BLUE, COLOR_YELLOW} }; diff --git a/src/common/printer.c b/src/common/printer.c index 954d82b..8f2248e 100644 --- a/src/common/printer.c +++ b/src/common/printer.c @@ -322,22 +322,31 @@ void print_ascii_x86(struct ascii* art, uint32_t la) { int attr_type; char* attr_value; uint32_t space_right; - uint32_t space_up = (logo->height - art->n_attributes_set)/2; - uint32_t space_down = logo->height - art->n_attributes_set - space_up; + int32_t space_up = ((int)logo->height - (int)art->n_attributes_set)/2; + int32_t space_down = (int)logo->height - (int)art->n_attributes_set - (int)space_up; uint32_t logo_pos = 0; + int32_t iters = max(logo->height, art->n_attributes_set); printf("\n"); - for(uint32_t n=0; n < logo->height; n++) { - for(uint32_t i=0; i < logo->width; i++) { - if(logo->art[logo_pos] == '$' && logo->art[logo_pos+1] == 'C') { - parse_print_color(logo, &logo_pos); + for(int32_t n=0; n < iters; n++) { + // 1. Print logo + if(space_up > 0 || (space_up + n >= 0 && n + space_down < (int)logo->height)) { + for(uint32_t i=0; i < logo->width; i++) { + if(logo->art[logo_pos] == '$' && logo->art[logo_pos+1] == 'C') { + parse_print_color(logo, &logo_pos); + } + printf("%c", logo->art[logo_pos]); + logo_pos++; } - printf("%c", logo->art[logo_pos]); - logo_pos++; + printf("%s", art->reset); + } + else { + // If logo should not be printed, fill with spaces + printf("%*c", logo->width, ' '); } - printf("%s", art->reset); - if(n > space_up-1 && n < logo->height - space_down) { + // 2. Print text + if(space_up < 0 || (n > space_up-1 && n < (int)logo->height - space_down)) { attr_type = art->attributes[attr_to_print]->type; attr_value = art->attributes[attr_to_print]->value; attr_to_print++; @@ -355,7 +364,6 @@ bool print_cpufetch_x86(struct cpuInfo* cpu, STYLE s, struct color** cs) { if(art == NULL) return false; - struct ascii_logo* logo = art->art; char* uarch = get_str_uarch(cpu); char* manufacturing_process = get_str_process(cpu); char* sockets = get_str_sockets(cpu->topo); @@ -398,11 +406,6 @@ bool print_cpufetch_x86(struct cpuInfo* cpu, STYLE s, struct color** cs) { } setAttribute(art,ATTRIBUTE_PEAK,pp); - if(art->n_attributes_set > logo->height) { - printBug("The number of attributes set is bigger than the max that can be displayed"); - return false; - } - uint32_t longest_attribute = longest_attribute_length(art); print_ascii_x86(art, longest_attribute);