[v0.06] Add --color option

This commit is contained in:
Dr-Noob
2021-08-16 22:23:43 +02:00
parent 6afe26f884
commit 9f52f4916d
4 changed files with 87 additions and 7 deletions

View File

@@ -10,7 +10,11 @@
#define OVERFLOW -1
#define UNDERFLOW -2
#define INVALID_ARG -3
#define NUM_COLORS 4
#define NUM_COLORS 4
#define COLOR_STR_NVIDIA "nvidia"
#define COLOR_DEFAULT_NVIDIA "118,185,0:0,0,0:255,255,255:118,185,0"
struct args_struct {
bool help_flag;
@@ -24,12 +28,14 @@ int errn = 0;
static struct args_struct args;
const char args_chr[] = {
/* [ARG_CHAR_COLOR] = */ 'c',
/* [ARG_CHAR_GPU] = */ 'g',
/* [ARG_CHAR_HELP] = */ 'h',
/* [ARG_CHAR_VERSION] = */ 'V',
};
const char *args_str[] = {
/* [ARG_CHAR_COLOR] = */ "color",
/* [ARG_CHAR_GPU] = */ "gpu",
/* [ARG_CHAR_HELP] = */ "help",
/* [ARG_CHAR_VERSION] = */ "version",
@@ -113,12 +119,65 @@ char* build_short_options() {
char* str = (char *) emalloc(sizeof(char) * (len*2 + 1));
memset(str, 0, sizeof(char) * (len*2 + 1));
sprintf(str, "%c:%c%c", c[ARG_GPU],
c[ARG_HELP], c[ARG_VERSION]);
sprintf(str, "%c:%c:%c%c", c[ARG_GPU],
c[ARG_COLOR], c[ARG_HELP], c[ARG_VERSION]);
return str;
}
bool parse_color(char* optarg_str, struct color*** cs) {
for(int i=0; i < NUM_COLORS; i++) {
(*cs)[i] = (struct color *) emalloc(sizeof(struct color));
}
struct color** c = *cs;
int32_t ret;
char* str_to_parse = NULL;
const char* color_to_copy = NULL;
bool free_ptr = true;
if(strcmp(optarg_str, COLOR_STR_NVIDIA) == 0) color_to_copy = COLOR_DEFAULT_NVIDIA;
else {
str_to_parse = optarg_str;
free_ptr = false;
}
if(str_to_parse == NULL) {
str_to_parse = (char *) emalloc(sizeof(char) * (strlen(color_to_copy) + 1));
strcpy(str_to_parse, color_to_copy);
}
ret = sscanf(str_to_parse, "%d,%d,%d:%d,%d,%d:%d,%d,%d:%d,%d,%d",
&c[0]->R, &c[0]->G, &c[0]->B,
&c[1]->R, &c[1]->G, &c[1]->B,
&c[2]->R, &c[2]->G, &c[2]->B,
&c[3]->R, &c[3]->G, &c[3]->B);
if(ret != 12) {
printErr("Expected to read 12 values for color but read %d", ret);
return false;
}
for(int i=0; i < NUM_COLORS; i++) {
if(c[i]->R < 0 || c[i]->R > 255) {
printErr("Red in color %d is invalid: %d; must be in range (0, 255)", i+1, c[i]->R);
return false;
}
if(c[i]->G < 0 || c[i]->G > 255) {
printErr("Green in color %d is invalid: %d; must be in range (0, 255)", i+1, c[i]->G);
return false;
}
if(c[i]->B < 0 || c[i]->B > 255) {
printErr("Blue in color %d is invalid: %d; must be in range (0, 255)", i+1, c[i]->B);
return false;
}
}
if(free_ptr) free (str_to_parse);
return true;
}
bool parse_args(int argc, char* argv[]) {
int opt;
int option_index = 0;
@@ -127,8 +186,10 @@ bool parse_args(int argc, char* argv[]) {
args.version_flag = false;
args.help_flag = false;
args.gpu_idx = 0;
args.colors = NULL;
const struct option long_options[] = {
{args_str[ARG_COLOR], required_argument, 0, args_chr[ARG_COLOR] },
{args_str[ARG_GPU], required_argument, 0, args_chr[ARG_GPU] },
{args_str[ARG_HELP], no_argument, 0, args_chr[ARG_HELP] },
{args_str[ARG_VERSION], no_argument, 0, args_chr[ARG_VERSION] },
@@ -139,7 +200,13 @@ bool parse_args(int argc, char* argv[]) {
opt = getopt_long(argc, argv, short_options, long_options, &option_index);
while (!args.help_flag && !args.version_flag && opt != -1) {
if(opt == args_chr[ARG_GPU]) {
if(opt == args_chr[ARG_COLOR]) {
args.colors = (struct color **) emalloc(sizeof(struct color *) * NUM_COLORS);
if(!parse_color(optarg, &args.colors)) {
return false;
}
}
else if(opt == args_chr[ARG_GPU]) {
args.gpu_idx = getarg_int(optarg);
if(errn != 0) {
printErr("Option %s: ", args_str[ARG_GPU]);

View File

@@ -19,6 +19,7 @@ enum {
};
enum {
ARG_COLOR,
ARG_GPU,
ARG_HELP,
ARG_VERSION

View File

@@ -7,7 +7,7 @@
#include "../cuda/cuda.hpp"
#include "../cuda/uarch.hpp"
static const char* VERSION = "0.05";
static const char* VERSION = "0.06";
void print_help(char *argv[]) {
const char **t = args_str;
@@ -18,10 +18,24 @@ void print_help(char *argv[]) {
printf("Simple yet fancy GPU architecture fetching tool\n\n");
printf("Options: \n");
printf(" -%c, --%s %*s Sets the color scheme (by default, gpufetch uses the system color scheme) See COLORS section for a more detailed explanation\n", c[ARG_COLOR], t[ARG_COLOR], (int) (max_len-strlen(t[ARG_COLOR])), "");
printf(" -%c, --%s %*s Selects the GPU to use (default: 0)\n", c[ARG_GPU], t[ARG_GPU], (int) (max_len-strlen(t[ARG_GPU])), "");
printf(" -%c, --%s %*s Prints this help and exit\n", c[ARG_HELP], t[ARG_HELP], (int) (max_len-strlen(t[ARG_HELP])), "");
printf(" -%c, --%s %*s Prints gpufetch version and exit\n", c[ARG_VERSION], t[ARG_VERSION], (int) (max_len-strlen(t[ARG_VERSION])), "");
printf("\nCOLORS: \n");
printf(" Color scheme can be set using a predefined color scheme or a custom one:\n");
printf(" 1. To use a predefined color scheme, the name of the scheme must be provided. Possible values are:\n");
printf(" * \"nvidia\": Use NVIDIA default color scheme \n");
printf(" 2. To use a custom color scheme, 4 colors must be given in RGB with the format: R,G,B:R,G,B:...\n");
printf(" The first 2 colors are the GPU art color and the following 2 colors are the text colors\n");
printf("\nEXAMPLES: \n");
printf(" Run gpufetch with NVIDIA color scheme:\n");
printf(" ./gpufetch --color nvidia\n");
printf(" Run gpufetch with a custom color scheme:\n");
printf(" ./gpufetch --color 239,90,45:210,200,200:100,200,45:0,200,200\n");
printf("\nBUGS: \n");
printf(" Report bugs to https://github.com/Dr-Noob/gpufetch/issues\n");

View File

@@ -6,8 +6,6 @@ typedef int STYLE;
#include "args.hpp"
#include "../cuda/cuda.hpp"
#define COLOR_DEFAULT_NVIDIA "15,125,194:230,230,230:40,150,220:230,230,230"
bool print_gpufetch(struct gpu_info* gpu, STYLE s, struct color** cs);
#endif