diff --git a/doc/json_schema.json b/doc/json_schema.json index 884fd4c2e..88ffbec0b 100644 --- a/doc/json_schema.json +++ b/doc/json_schema.json @@ -457,6 +457,24 @@ "minimum": 0, "maximum": 9, "default": 0 + }, + "color": { + "type": "object", + "description": "Set color used in different states of percentage bars and numbers", + "properties": { + "green": { + "description": "Color used in green state", + "$ref": "#/$defs/colors" + }, + "yellow": { + "description": "Color used in yellow state", + "$ref": "#/$defs/colors" + }, + "red": { + "description": "Color used in red state", + "$ref": "#/$defs/colors" + } + } } } }, diff --git a/src/common/option.c b/src/common/option.c index cb72a385e..81efd69d1 100644 --- a/src/common/option.c +++ b/src/common/option.c @@ -143,7 +143,7 @@ void ffOptionParseColor(const char* value, FFstrbuf* buffer) while(*value != '\0') { #define FF_APPEND_COLOR_CODE_COND(prefix, code) \ - if(strncasecmp(value, #prefix, strlen(#prefix)) == 0) { ffStrbufAppendS(buffer, code); value += strlen(#prefix); } + if(ffStrStartsWithIgnCase(value, #prefix)) { ffStrbufAppendS(buffer, code); value += strlen(#prefix); } FF_APPEND_COLOR_CODE_COND(reset_, FF_COLOR_MODE_RESET) else FF_APPEND_COLOR_CODE_COND(bright_, FF_COLOR_MODE_BOLD) diff --git a/src/common/percent.c b/src/common/percent.c index 0b154e744..306838d06 100644 --- a/src/common/percent.c +++ b/src/common/percent.c @@ -24,28 +24,43 @@ void ffPercentAppendBar(FFstrbuf* buffer, double percent, FFPercentConfig config ffStrbufAppendS(buffer, "[ "); } - for (uint32_t i = 0; i < blocksPercent; ++i) + if (percent != percent) { if(!options->pipe) - { - uint32_t section1Begin = (uint32_t) ((green <= yellow ? green : yellow) / 100.0 * options->barWidth + 0.5); - uint32_t section2Begin = (uint32_t) ((green > yellow ? green : yellow) / 100.0 * options->barWidth + 0.5); - if (i == section2Begin) - ffStrbufAppendF(buffer, "\e[%sm", (green > yellow ? FF_COLOR_FG_LIGHT_GREEN : FF_COLOR_FG_LIGHT_RED)); - else if (i == section1Begin) - ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_YELLOW "m"); - else if (i == 0) - ffStrbufAppendF(buffer, "\e[%sm", (green <= yellow ? FF_COLOR_FG_GREEN : FF_COLOR_FG_LIGHT_RED)); - } - ffStrbufAppend(buffer, &options->barCharElapsed); - } + ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_BLACK "m"); - if (blocksPercent < options->barWidth) + for (uint32_t i = 0; i < options->barWidth; ++i) + ffStrbufAppend(buffer, &options->barCharElapsed); + } + else { - if(!options->pipe) - ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_WHITE "m"); - for (uint32_t i = blocksPercent; i < options->barWidth; ++i) - ffStrbufAppend(buffer, &options->barCharTotal); + const char* colorGreen = instance.config.display.percentColorGreen.chars; + const char* colorYellow = instance.config.display.percentColorYellow.chars; + const char* colorRed = instance.config.display.percentColorRed.chars; + + for (uint32_t i = 0; i < blocksPercent; ++i) + { + if(!options->pipe) + { + uint32_t section1Begin = (uint32_t) ((green <= yellow ? green : yellow) / 100.0 * options->barWidth + 0.5); + uint32_t section2Begin = (uint32_t) ((green > yellow ? green : yellow) / 100.0 * options->barWidth + 0.5); + if (i == section2Begin) + ffStrbufAppendF(buffer, "\e[%sm", (green > yellow ? colorGreen : colorRed)); + else if (i == section1Begin) + ffStrbufAppendF(buffer, "\e[%sm", colorYellow); + else if (i == 0) + ffStrbufAppendF(buffer, "\e[%sm", (green <= yellow ? colorGreen : colorRed)); + } + ffStrbufAppend(buffer, &options->barCharElapsed); + } + + if (blocksPercent < options->barWidth) + { + if(!options->pipe) + ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_WHITE "m"); + for (uint32_t i = blocksPercent; i < options->barWidth; ++i) + ffStrbufAppend(buffer, &options->barCharTotal); + } } if(options->barBorder) @@ -74,26 +89,30 @@ void ffPercentAppendNum(FFstrbuf* buffer, double percent, FFPercentConfig config if (colored && !options->pipe) { + const char* colorGreen = instance.config.display.percentColorGreen.chars; + const char* colorYellow = instance.config.display.percentColorYellow.chars; + const char* colorRed = instance.config.display.percentColorRed.chars; + if(percent != percent) ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_BLACK "m"); else if(green <= yellow) { if (percent > yellow) - ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_RED "m"); + ffStrbufAppendF(buffer, "\e[%sm", colorRed); else if (percent > green) - ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_YELLOW "m"); + ffStrbufAppendF(buffer, "\e[%sm", colorYellow); else - ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_GREEN "m"); + ffStrbufAppendF(buffer, "\e[%sm", colorGreen); } else { if (percent < yellow) - ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_RED "m"); + ffStrbufAppendF(buffer, "\e[%sm", colorRed); else if (percent < green) - ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_YELLOW "m"); + ffStrbufAppendF(buffer, "\e[%sm", colorYellow); else - ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_GREEN "m"); + ffStrbufAppendF(buffer, "\e[%sm", colorGreen); } } ffStrbufAppendF(buffer, "%.*f%%", options->percentNdigits, percent); diff --git a/src/data/help.json b/src/data/help.json index c22c42ef5..9af1a17e6 100644 --- a/src/data/help.json +++ b/src/data/help.json @@ -198,6 +198,7 @@ { "long": "logo-color-[1-9]", "desc": "Overwrite a color in the logo", + "remark": "See `-h color` for the list of available colors", "arg": { "type": "color" }, @@ -432,6 +433,7 @@ { "long": "color-keys", "desc": "Set the color of the keys", + "remark": "See `-h color` for the list of available colors", "arg": { "type": "color" } @@ -439,6 +441,7 @@ { "long": "color-title", "desc": "Set the color of the title", + "remark": "See `-h color` for the list of available colors", "arg": { "type": "color" } @@ -548,6 +551,33 @@ "default": 0 } }, + { + "long": "percent-color-green", + "desc": "Set color used in green state of percentage bars and numbers", + "remark": "See `-h color` for the list of available colors", + "arg": { + "type": "color", + "default": "green" + } + }, + { + "long": "percent-color-yellow", + "desc": "Set color used in yellow state of percentage bars and numbers", + "remark": "See `-h color` for the list of available colors", + "arg": { + "type": "color", + "default": "light_yellow" + } + }, + { + "long": "percent-color-red", + "desc": "Set color used in red state of percentage bars and numbers", + "remark": "See `-h color` for the list of available colors", + "arg": { + "type": "color", + "default": "light_red" + } + }, { "long": "bar-char-elapsed", "desc": "Set the character to use in elapsed part of percentage bars", @@ -821,6 +851,7 @@ { "long": "title-color-user", "desc": "Set color of the user name (left part)", + "remark": "See `-h color` for the list of available colors", "arg": { "type": "color", "default": "Use color of \"--color-title\"" @@ -829,6 +860,7 @@ { "long": "title-color-at", "desc": "Set color of the @ symbol (middle part)", + "remark": "See `-h color` for the list of available colors", "arg": { "type": "color", "default": "Use color of \"--color-title\"" @@ -837,6 +869,7 @@ { "long": "title-color-host", "desc": "Set color of the host name (right part)", + "remark": "See `-h color` for the list of available colors", "arg": { "type": "color", "default": "default" diff --git a/src/options/display.c b/src/options/display.c index 3236a7b1d..5ba60b58e 100644 --- a/src/options/display.c +++ b/src/options/display.c @@ -1,4 +1,5 @@ #include "fastfetch.h" +#include "common/color.h" #include "common/jsonconfig.h" #include "util/stringUtils.h" #include "options/display.h" @@ -118,6 +119,22 @@ const char* ffOptionsParseDisplayJsonConfig(FFOptionsDisplay* options, yyjson_va yyjson_val* ndigits = yyjson_obj_get(val, "ndigits"); if (ndigits) options->percentNdigits = (uint8_t) yyjson_get_uint(ndigits); + + yyjson_val* color = yyjson_obj_get(val, "color"); + if (color) + { + if (!yyjson_is_obj(color)) + return "display.percent.color must be an object"; + + yyjson_val* green = yyjson_obj_get(color, "green"); + if (green) ffOptionParseColor(yyjson_get_str(green), &options->percentColorGreen); + + yyjson_val* yellow = yyjson_obj_get(color, "yellow"); + if (yellow) ffOptionParseColor(yyjson_get_str(yellow), &options->percentColorYellow); + + yyjson_val* red = yyjson_obj_get(color, "red"); + if (red) ffOptionParseColor(yyjson_get_str(red), &options->percentColorRed); + } } else if (ffStrEqualsIgnCase(key, "bar")) { @@ -248,10 +265,22 @@ bool ffOptionsParseDisplayCommandLine(FFOptionsDisplay* options, const char* key {}, }); } - else if(ffStrEqualsIgnCase(key, "--percent-type")) - options->percentType = (uint8_t) ffOptionParseUInt32(key, value); - else if(ffStrEqualsIgnCase(key, "--percent-ndigits")) - options->percentNdigits = (uint8_t) ffOptionParseUInt32(key, value); + else if(ffStrStartsWithIgnCase(key, "--percent-")) + { + const char* subkey = key + strlen("--percent-"); + if(ffStrEqualsIgnCase(subkey, "type")) + options->percentType = (uint8_t) ffOptionParseUInt32(key, value); + else if(ffStrEqualsIgnCase(subkey, "ndigits")) + options->percentNdigits = (uint8_t) ffOptionParseUInt32(key, value); + else if(ffStrEqualsIgnCase(subkey, "color-green")) + ffOptionParseColor(value, &options->percentColorGreen); + else if(ffStrEqualsIgnCase(subkey, "color-yellow")) + ffOptionParseColor(value, &options->percentColorYellow); + else if(ffStrEqualsIgnCase(subkey, "color-red")) + ffOptionParseColor(value, &options->percentColorRed); + else + return false; + } else if(ffStrEqualsIgnCase(key, "--no-buffer")) options->noBuffer = ffOptionParseBoolean(value); else if(ffStrStartsWithIgnCase(key, "--bar-")) @@ -305,6 +334,9 @@ void ffOptionsInitDisplay(FFOptionsDisplay* options) options->barBorder = true; options->percentType = 9; options->percentNdigits = 0; + ffStrbufInitStatic(&options->percentColorGreen, FF_COLOR_FG_GREEN); + ffStrbufInitStatic(&options->percentColorYellow, FF_COLOR_FG_LIGHT_YELLOW); + ffStrbufInitStatic(&options->percentColorRed, FF_COLOR_FG_LIGHT_RED); } void ffOptionsDestroyDisplay(FFOptionsDisplay* options) diff --git a/src/options/display.h b/src/options/display.h index c1ef5a886..296e95a24 100644 --- a/src/options/display.h +++ b/src/options/display.h @@ -41,6 +41,9 @@ typedef struct FFOptionsDisplay bool barBorder; uint8_t percentType; uint8_t percentNdigits; + FFstrbuf percentColorGreen; + FFstrbuf percentColorYellow; + FFstrbuf percentColorRed; bool noBuffer; uint32_t keyWidth; } FFOptionsDisplay;