diff --git a/src/common/bar.c b/src/common/bar.c index 34b14aa7a..d4a8cf0f1 100644 --- a/src/common/bar.c +++ b/src/common/bar.c @@ -47,3 +47,53 @@ void ffAppendPercentBar(FFinstance* instance, FFstrbuf* buffer, uint8_t percent, else ffStrbufAppendS(buffer, " ]"); } + +// if (green < yellow) +// [0, green]: print green +// (green, yellow]: print yellow +// (yellow, 100]: print red +// +// if (green > yellow) +// [green, 100]: print green +// [yellow, green): print yellow +// [0, yellow): PRINT RED +void ffAppendPercentNum(FFinstance* instance, FFstrbuf* buffer, uint8_t percent, uint8_t green, uint8_t yellow, bool parentheses) +{ + assert(green <= 100 && yellow <= 100); + + bool colored = !!(instance->config.percentType & FF_PERCENTAGE_TYPE_NUM_COLOR_BIT); + + if (parentheses) + ffStrbufAppendC(buffer, '('); + + if (colored && !instance->config.pipe) + { + if(green < yellow) + { + if (percent <= green) + ffStrbufAppendS(buffer, "\033[32m"); + else if (percent <= yellow) + ffStrbufAppendS(buffer, "\033[93m"); + else + ffStrbufAppendS(buffer, "\033[91m"); + } + else + { + if (percent >= green) + ffStrbufAppendS(buffer, "\033[32m"); + else if (percent >= yellow) + ffStrbufAppendS(buffer, "\033[93m"); + else + ffStrbufAppendS(buffer, "\033[91m"); + } + } + ffStrbufAppendF(buffer, "%u%%", (unsigned) percent); + + if (colored && !instance->config.pipe) + { + ffStrbufAppendS(buffer, FASTFETCH_TEXT_MODIFIER_RESET); + } + + if (parentheses) + ffStrbufAppendC(buffer, ')'); +} diff --git a/src/common/bar.h b/src/common/bar.h index 575adc01d..f28cbe777 100644 --- a/src/common/bar.h +++ b/src/common/bar.h @@ -7,11 +7,13 @@ enum { - FF_PERCENTAGE_TYPE_NUM_BIT = 1, - FF_PERCENTAGE_TYPE_BAR_BIT = 2, - FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT = 4, + FF_PERCENTAGE_TYPE_NUM_BIT = 1 << 0, + FF_PERCENTAGE_TYPE_BAR_BIT = 1 << 1, + FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT = 1 << 2, + FF_PERCENTAGE_TYPE_NUM_COLOR_BIT = 1 << 3, }; void ffAppendPercentBar(FFinstance* instance, FFstrbuf* buffer, uint8_t percent, uint8_t green, uint8_t yellow, uint8_t red); +void ffAppendPercentNum(FFinstance* instance, FFstrbuf* buffer, uint8_t percent, uint8_t green, uint8_t yellow, bool parentheses); #endif diff --git a/src/data/config_user.txt b/src/data/config_user.txt index 1b9ac9635..e4fcef289 100644 --- a/src/data/config_user.txt +++ b/src/data/config_user.txt @@ -262,7 +262,7 @@ # Percentage output type option # Applies to all modules that prints percentage values. Currently memory, swap, disk, battery and CPU usage are supported. # Only works with default format ( without --module-format option ). -# 0: prints none; 1: prints percent number only; 2: prints bar only; 3: prints both percent number and bar; 6: prints bar and hide other texts +# 0: prints none; 1: prints percent number only; 2: prints bar only; 3: prints both percent number and bar; 6: prints bar and hide other texts; 9: prints colored number #--percent-type 1 # Key options: diff --git a/src/data/help.txt b/src/data/help.txt index c4fd1acd3..37adf2eed 100644 --- a/src/data/help.txt +++ b/src/data/help.txt @@ -130,7 +130,7 @@ Module specific options: --weather-output-format: The output weather format to be used. It must be URI encoded. --player-name: The name of the player to use --gl : Set the OpenGL context creation library to use. Must be auto, egl, glx or osmesa. Default is auto - --percent-type : Set the percentage output type. 1 for percentage number, 2 for bar, 3 for both, 6 for bar only. Default is 1 + --percent-type : Set the percentage output type. 1 for percentage number, 2 for bar, 3 for both, 6 for bar only, 9 for colored number. Default is 1 --command-shell : Set the shell program to execute the command text. Default is cmd for Windows, csh for FreeBSD, bash for others --command-key : Set the module key to display, can be specified mulitple times --command-text : Set the command text to be executed, can be specified mulitple times diff --git a/src/modules/battery.c b/src/modules/battery.c index 79cdf4c79..6bafb0ee7 100644 --- a/src/modules/battery.c +++ b/src/modules/battery.c @@ -36,7 +36,8 @@ static void printBattery(FFinstance* instance, BatteryResult* result, uint8_t in { if(str.length > 0) ffStrbufAppendC(&str, ' '); - ffStrbufAppendF(&str, str.length > 0 ? "(%u%%)" : "%u%%", (unsigned) result->capacity); + + ffAppendPercentNum(instance, &str, (uint8_t) result->capacity, 51, 21, str.length > 0); } } diff --git a/src/modules/cpuUsage.c b/src/modules/cpuUsage.c index 3e4165b90..f46de6259 100644 --- a/src/modules/cpuUsage.c +++ b/src/modules/cpuUsage.c @@ -29,7 +29,7 @@ void ffPrintCPUUsage(FFinstance* instance) { if(str.length > 0) ffStrbufAppendC(&str, ' '); - ffStrbufAppendF(&str, "%.2lf%%", percentage); + ffAppendPercentNum(instance, &str, (uint8_t) percentage, 50, 80, str.length > 0); } ffStrbufPutTo(&str, stdout); ffStrbufDestroy(&str); diff --git a/src/modules/disk.c b/src/modules/disk.c index c1e074812..d2311f11f 100644 --- a/src/modules/disk.c +++ b/src/modules/disk.c @@ -53,7 +53,8 @@ static void printDisk(FFinstance* instance, const FFDisk* disk) if(instance->config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT) { - ffStrbufAppendF(&str, str.length > 0 ? "(%u%%) " : "%u%% ", bytesPercentage); + ffAppendPercentNum(instance, &str, (uint8_t) bytesPercentage, 50, 80, str.length > 0); + ffStrbufAppendC(&str, ' '); } } else diff --git a/src/modules/memory.c b/src/modules/memory.c index fbb893f02..05d7c33cf 100644 --- a/src/modules/memory.c +++ b/src/modules/memory.c @@ -56,7 +56,7 @@ static void printMemory(FFinstance* instance, const char* name, const FFModuleAr ffStrbufAppendF(&str, "%s / %s ", usedPretty.chars, totalPretty.chars); if(instance->config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT) - ffStrbufAppendF(&str, str.length > 0 ? "(%u%%)" : "%u%%", percentage); + ffAppendPercentNum(instance, &str, (uint8_t) percentage, 50, 80, str.length > 0); ffStrbufTrimRight(&str, ' '); ffStrbufPutTo(&str, stdout);