Add "none" style (useful for non colored terminals). Refactoring in printer

This commit is contained in:
Dr-Noob
2020-06-21 11:07:35 +02:00
parent a1496278c3
commit 0cba7b4348
6 changed files with 101 additions and 126 deletions

View File

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

View File

@@ -4,7 +4,7 @@
#include <stdbool.h>
#include "printer.h"
int parseArgs(int argc, char* argv[]);
bool parseArgs(int argc, char* argv[]);
STYLE getStyle();
int showHelp();
int showVersion();

View File

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

View File

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

View File

@@ -1,11 +1,13 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#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"
@@ -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;
if(i != ATTRIBUTE_COUNT)
art->atributes[i] = value;
return BOOLEAN_TRUE;
else
printBug("Setting attribute failed because it was not found");
}
struct ascii* set_ascii(VENDOR cpuVendor, STYLE style) {
@@ -73,8 +75,8 @@ 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) {
case STYLE_EMPTY:
case STYLE_DEFAULT:
@@ -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;n<NUMBER_OF_LINES;n++) {
@@ -162,7 +135,7 @@ void print_ascii_intel(struct ascii* art) {
for(int i=0;i<LINE_SIZE;i++) {
if(flag) {
if(art->art[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

View File

@@ -4,9 +4,6 @@
#include "standart.h"
#include "ascii.h"
#define BOOLEAN_TRUE 1
#define BOOLEAN_FALSE 0
#define ATTRIBUTE_COUNT 12
#define ATTRIBUTE_NAME 0
#define ATTRIBUTE_FREQUENCY 1
@@ -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_INVALID -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