Global: add support for colored percentage numbers output

This commit is contained in:
李通洲
2023-01-31 19:42:02 +08:00
parent d36e827ec9
commit 2476b44300
8 changed files with 63 additions and 9 deletions
+50
View File
@@ -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, ')');
}
+5 -3
View File
@@ -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
+1 -1
View File
@@ -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:
+1 -1
View File
@@ -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 <value>: Set the OpenGL context creation library to use. Must be auto, egl, glx or osmesa. Default is auto
--percent-type <value>: Set the percentage output type. 1 for percentage number, 2 for bar, 3 for both, 6 for bar only. Default is 1
--percent-type <value>: 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 <str>: Set the shell program to execute the command text. Default is cmd for Windows, csh for FreeBSD, bash for others
--command-key <str>: Set the module key to display, can be specified mulitple times
--command-text <str>: Set the command text to be executed, can be specified mulitple times
+2 -1
View File
@@ -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);
}
}
+1 -1
View File
@@ -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);
+2 -1
View File
@@ -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
+1 -1
View File
@@ -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);