Support for simple args options(currently, help and style). New feature to switch colors of logo, to improve visibility in bright terminals

This commit is contained in:
Dr-Noob
2018-06-21 15:30:29 +02:00
parent 5bf6cbf2c7
commit 7b059c046a
6 changed files with 186 additions and 19 deletions

View File

@@ -2,8 +2,8 @@ CXX=gcc
CXXFLAGS=-g -Wall -Werror -fstack-protector-all -pedantic -Wno-unused
SOURCE=main.c standart.c extended.c cpuid.c udev.c printer.c
HEADERS=standart.h extended.h cpuid.h udev.h printer.h ascii.h
SOURCE=main.c standart.c extended.c cpuid.c udev.c printer.c args.c
HEADERS=standart.h extended.h cpuid.h udev.h printer.h ascii.h args.h
OUTPUT=cpufetch

93
args.c Normal file
View File

@@ -0,0 +1,93 @@
#include <getopt.h>
#include <stdio.h>
#include <string.h>
#include "args.h"
#define ARG_STR_STYLE "style"
#define ARG_STR_HELP "help"
#define ARG_CHAR_STYLE 's'
#define ARG_CHAR_HELP 'h'
#define STYLE_STR_1 "default"
#define STYLE_STR_2 "dark"
struct args_struct {
int help_flag;
STYLE style;
};
static const char* SYTLES_STR_LIST[STYLES_COUNT] = { STYLE_STR_1, STYLE_STR_2 };
static struct args_struct args;
STYLE parseStyle(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;
}
STYLE getStyle() {
return args.style;
}
int showHelp() {
return args.help_flag;
}
int parseArgs(int argc, char* argv[]) {
int c;
int digit_optind = 0;
int option_index = 0;
opterr = 0;
args.help_flag = BOOLEAN_FALSE;
args.style = STYLE_EMPTY;
static struct option long_options[] = {
{ARG_STR_STYLE, required_argument, 0, ARG_CHAR_STYLE},
{ARG_STR_HELP, no_argument, 0, ARG_CHAR_HELP },
{0, 0, 0, 0}
};
c = getopt_long(argc, argv,"",long_options, &option_index);
while (c != -1) {
if(c == ARG_CHAR_STYLE) {
if(args.style != STYLE_EMPTY) {
printf("ERROR: Style option specified more than once\n");
return BOOLEAN_FALSE;
}
args.style = parseStyle(optarg);
if(args.style == STYLE_INVALID) {
printf("ERROR: Invalid style '%s'\n",optarg);
return BOOLEAN_FALSE;
}
}
else if(c == ARG_CHAR_HELP) {
if(args.help_flag) {
printf("ERROR: Help option specified more than once\n");
return BOOLEAN_FALSE;
}
args.help_flag = BOOLEAN_TRUE;
}
else if(c == '?') {
printf("WARNING: Invalid options\n");
args.help_flag = BOOLEAN_TRUE;
break;
}
else
printf("Bug at line number %d in file %s\n", __LINE__, __FILE__);
option_index = 0;
c = getopt_long(argc, argv,"",long_options, &option_index);
}
if (optind < argc) {
printf("WARNING: Invalid options\n");
args.help_flag = BOOLEAN_TRUE;
}
return BOOLEAN_TRUE;
}

10
args.h Normal file
View File

@@ -0,0 +1,10 @@
#ifndef __ARGS__
#define __ARGS__
#include "printer.h"
int parseArgs(int argc, char* argv[]);
STYLE getStyle();
int showHelp();
#endif

26
main.c
View File

@@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include "args.h"
#include "printer.h"
#include "standart.h"
#include "udev.h"
@@ -24,14 +25,35 @@ Peak FLOPS: 512 GFLOP/s(in simple precision)
***/
int main() {
void help(int argc, char *argv[])
{
printf("Usage: %s [--help] [--style STYLE]\n\
Options: \n\
--style Set logo style color\n\
default: Default style color\n\
dark: Dark style color\n\n\
--help Print this help and exit\n",
argv[0]);
}
int main(int argc, char* argv[]) {
if(!parseArgs(argc,argv))
return EXIT_FAILURE;
if(showHelp()) {
help(argc,argv);
return EXIT_SUCCESS;
}
struct cpuInfo* cpu = getCPUInfo();
if(cpu == NULL)
return EXIT_FAILURE;
struct cache* cach = new_cache();
struct frequency* freq = new_frequency();
struct ascii* art = set_ascii(getCPUVendorInternal(cpu));
struct ascii* art = set_ascii(getCPUVendorInternal(cpu),getStyle());
if(art == NULL)
return EXIT_FAILURE;
char* cpuName = getString_CPUName();
char* maxFrequency = getString_MaxFrequency(freq);

View File

@@ -5,10 +5,14 @@
#include "printer.h"
#include "ascii.h"
#define COL_INTEL_1 "\x1b[34;1m"
#define COL_INTEL_2 "\x1b[37;1m"
#define COL_AMD_1 "\x1b[30;1m"
#define COL_AMD_2 "\x1b[32;1m"
#define COL_INTEL_DEFAULT_1 "\x1b[36;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_2 "\x1b[31;1m"
#define COL_AMD_DARK_1 "\x1b[30;1m"
#define COL_AMD_DARK_2 "\x1b[32;1m"
#define RESET "\x1b[0m"
#define TITLE_NAME "Name: "
@@ -55,13 +59,28 @@ int setAttribute(struct ascii* art, int type, char* value) {
return BOOLEAN_TRUE;
}
struct ascii* set_ascii(VENDOR cpuVendor) {
struct ascii* set_ascii(VENDOR cpuVendor, STYLE style) {
struct ascii* art = malloc(sizeof(struct ascii));
art->vendor = cpuVendor;
if(cpuVendor == VENDOR_INTEL) {
strcpy(art->color1,COL_INTEL_1);
strcpy(art->color2,COL_INTEL_2);
/*** CHECK STYLE ***/
switch (style) {
case STYLE_EMPTY:
case STYLE_DEFAULT:
strcpy(art->color1,COL_INTEL_DEFAULT_1);
strcpy(art->color2,COL_INTEL_DEFAULT_2);
break;
case STYLE_DARK:
strcpy(art->color1,COL_INTEL_DARK_1);
strcpy(art->color2,COL_INTEL_DARK_2);
break;
default:
//TODO Bugs function
printf("Bug at line number %d in file %s\n", __LINE__, __FILE__);
return NULL;
}
/*** COPY ASCII-ART ***/
strcpy(art->art[0],INTEL1);
strcpy(art->art[1],INTEL2);
strcpy(art->art[2],INTEL3);
@@ -84,8 +103,22 @@ struct ascii* set_ascii(VENDOR cpuVendor) {
strcpy(art->art[19],INTEL20);
}
else {
strcpy(art->color1,COL_AMD_1);
strcpy(art->color2,COL_AMD_2);
/*** CHECK STYLE ***/
switch (style) {
case STYLE_EMPTY:
case STYLE_DEFAULT:
strcpy(art->color1,COL_AMD_DEFAULT_1);
strcpy(art->color2,COL_AMD_DEFAULT_2);
break;
case STYLE_DARK:
strcpy(art->color1,COL_AMD_DARK_1);
strcpy(art->color2,COL_AMD_DARK_2);
break;
default:
//TODO Bugs function
printf("Bug at line number %d in file %s\n", __LINE__, __FILE__);
return NULL;
}
strcpy(art->art[0],AMD1);
strcpy(art->art[1],AMD2);

View File

@@ -4,12 +4,6 @@
#include "standart.h"
#include "ascii.h"
struct ascii;
struct ascii* set_ascii(VENDOR cpuVendor);
void print_ascii(struct ascii* art);
int setAttribute(struct ascii* art, int type, char* value);
#define BOOLEAN_TRUE 1
#define BOOLEAN_FALSE 0
@@ -28,4 +22,19 @@ int setAttribute(struct ascii* art, int type, char* value);
#define ATTRIBUTE_L3 11
#define ATTRIBUTE_PEAK 12
typedef int STYLE;
#define STYLES_COUNT 2
#define STYLE_EMPTY -2
#define STYLE_INVALID -1
#define STYLE_DEFAULT 0
#define STYLE_DARK 1
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);
#endif