From da5c44b3d04c4a7fb5f7f2695bba00bce625a160 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sat, 20 Jan 2024 13:27:55 +0800 Subject: [PATCH] Percentage: simplify usage of `ffAppendPercentBar` --- src/common/percent.c | 50 ++++++++++++----------------- src/common/percent.h | 12 ++++++- src/modules/battery/battery.c | 9 ++---- src/modules/brightness/brightness.c | 4 +-- src/modules/cpuusage/cpuusage.c | 2 +- src/modules/disk/disk.c | 2 +- src/modules/memory/memory.c | 2 +- src/modules/swap/swap.c | 4 +-- 8 files changed, 41 insertions(+), 44 deletions(-) diff --git a/src/common/percent.c b/src/common/percent.c index 206850083..703ce40a3 100644 --- a/src/common/percent.c +++ b/src/common/percent.c @@ -1,17 +1,15 @@ -#include "common/color.h" +#include "fastfetch.h" #include "common/percent.h" +#include "common/color.h" #include "util/textModifier.h" -void ffAppendPercentBar(FFstrbuf* buffer, double percent, uint8_t green, uint8_t yellow, uint8_t red) +void ffAppendPercentBar(FFstrbuf* buffer, double percent, uint8_t green, uint8_t yellow) { - assert(green <= 100 && yellow <= 100 && red <= 100); + assert(green <= 100 && yellow <= 100); const FFOptionsDisplay* options = &instance.config.display; uint32_t blocksPercent = (uint32_t) (percent / 100.0 * options->barWidth + 0.5); - uint32_t blocksGreen = (uint32_t) (green / 100.0 * options->barWidth + 0.5); - uint32_t blocksYellow = (uint32_t) (yellow / 100.0 * options->barWidth + 0.5); - uint32_t blocksRed = (uint32_t) (red / 100.0 * options->barWidth + 0.5); assert(blocksPercent <= options->barWidth); if(options->barBorder) @@ -26,12 +24,14 @@ void ffAppendPercentBar(FFstrbuf* buffer, double percent, uint8_t green, uint8_t { if(!options->pipe) { - if (i == blocksGreen) - ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_GREEN "m"); - else if (i == blocksYellow) + 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 == blocksRed) - ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_RED "m"); + else if (i == 0) + ffStrbufAppendF(buffer, "\e[%sm", (green <= yellow ? FF_COLOR_FG_GREEN : FF_COLOR_FG_LIGHT_RED)); } ffStrbufAppend(buffer, &options->barCharElapsed); } @@ -56,15 +56,6 @@ void ffAppendPercentBar(FFstrbuf* buffer, double percent, uint8_t green, uint8_t ffStrbufAppendS(buffer, FASTFETCH_TEXT_MODIFIER_RESET); } -// 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(FFstrbuf* buffer, double percent, uint8_t green, uint8_t yellow, bool parentheses) { assert(green <= 100 && yellow <= 100); @@ -80,23 +71,24 @@ void ffAppendPercentNum(FFstrbuf* buffer, double percent, uint8_t green, uint8_t { if(percent != percent) ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_BLACK "m"); - else if(green < yellow) + else if(green <= yellow) { - if (percent <= green) - ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_GREEN "m"); - else if (percent <= yellow) + if (percent > yellow) + ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_RED "m"); + else if (percent > green) ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_YELLOW "m"); else - ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_RED "m"); + ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_GREEN "m"); + } else { - if (percent >= green) - ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_GREEN "m"); - else if (percent >= yellow) + if (percent < yellow) + ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_RED "m"); + else if (percent < green) ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_YELLOW "m"); else - ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_RED "m"); + ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_GREEN "m"); } } ffStrbufAppendF(buffer, "%.*f%%", options->percentNdigits, percent); diff --git a/src/common/percent.h b/src/common/percent.h index c77234ae6..43a1f49d9 100644 --- a/src/common/percent.h +++ b/src/common/percent.h @@ -10,5 +10,15 @@ enum FF_PERCENTAGE_TYPE_NUM_COLOR_BIT = 1 << 3, }; -void ffAppendPercentBar(FFstrbuf* buffer, double percent, uint8_t green, uint8_t yellow, uint8_t red); +// 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 ffAppendPercentBar(FFstrbuf* buffer, double percent, uint8_t green, uint8_t yellow); void ffAppendPercentNum(FFstrbuf* buffer, double percent, uint8_t green, uint8_t yellow, bool parentheses); diff --git a/src/modules/battery/battery.c b/src/modules/battery/battery.c index 3accac67c..89d339a30 100644 --- a/src/modules/battery/battery.c +++ b/src/modules/battery/battery.c @@ -24,12 +24,7 @@ static void printBattery(FFBatteryOptions* options, FFBatteryResult* result, uin { if(instance.config.display.percentType & FF_PERCENTAGE_TYPE_BAR_BIT) { - if(result->capacity <= 20) - ffAppendPercentBar(&str, result->capacity, 100, 100, 0); - else if(result->capacity <= 50) - ffAppendPercentBar(&str, result->capacity, 100, 0, 100); - else - ffAppendPercentBar(&str, result->capacity, 0, 100, 100); + ffAppendPercentBar(&str, result->capacity, 50, 20); } if(instance.config.display.percentType & FF_PERCENTAGE_TYPE_NUM_BIT) @@ -37,7 +32,7 @@ static void printBattery(FFBatteryOptions* options, FFBatteryResult* result, uin if(str.length > 0) ffStrbufAppendC(&str, ' '); - ffAppendPercentNum(&str, result->capacity, 51, 21, str.length > 0); + ffAppendPercentNum(&str, result->capacity, 50, 20, str.length > 0); } } diff --git a/src/modules/brightness/brightness.c b/src/modules/brightness/brightness.c index 4f3342227..67074b07a 100644 --- a/src/modules/brightness/brightness.c +++ b/src/modules/brightness/brightness.c @@ -52,7 +52,7 @@ void ffPrintBrightness(FFBrightnessOptions* options) if (instance.config.display.percentType & FF_PERCENTAGE_TYPE_BAR_BIT) { - ffAppendPercentBar(&str, percent, 0, 100, 100); + ffAppendPercentBar(&str, percent, 100, 100); } if(instance.config.display.percentType & FF_PERCENTAGE_TYPE_NUM_BIT) @@ -60,7 +60,7 @@ void ffPrintBrightness(FFBrightnessOptions* options) if(str.length > 0) ffStrbufAppendC(&str, ' '); - ffAppendPercentNum(&str, percent, 10, 10, str.length > 0); + ffAppendPercentNum(&str, percent, 100, 100, str.length > 0); } ffStrbufPutTo(&str, stdout); diff --git a/src/modules/cpuusage/cpuusage.c b/src/modules/cpuusage/cpuusage.c index c21491f4e..85797a213 100644 --- a/src/modules/cpuusage/cpuusage.c +++ b/src/modules/cpuusage/cpuusage.c @@ -52,7 +52,7 @@ void ffPrintCPUUsage(FFCPUUsageOptions* options) if (!options->separate) { if(instance.config.display.percentType & FF_PERCENTAGE_TYPE_BAR_BIT) - ffAppendPercentBar(&str, avgValue, 0, 50, 80); + ffAppendPercentBar(&str, avgValue, 50, 80); if(instance.config.display.percentType & FF_PERCENTAGE_TYPE_NUM_BIT) { if(str.length > 0) diff --git a/src/modules/disk/disk.c b/src/modules/disk/disk.c index cd7e582e4..69cb6333a 100644 --- a/src/modules/disk/disk.c +++ b/src/modules/disk/disk.c @@ -59,7 +59,7 @@ static void printDisk(FFDiskOptions* options, const FFDisk* disk) { if(instance.config.display.percentType & FF_PERCENTAGE_TYPE_BAR_BIT) { - ffAppendPercentBar(&str, bytesPercentage, 0, 50, 80); + ffAppendPercentBar(&str, bytesPercentage, 50, 80); ffStrbufAppendC(&str, ' '); } diff --git a/src/modules/memory/memory.c b/src/modules/memory/memory.c index 73f1a3e2c..bccb12e9f 100644 --- a/src/modules/memory/memory.c +++ b/src/modules/memory/memory.c @@ -40,7 +40,7 @@ void ffPrintMemory(FFMemoryOptions* options) if(instance.config.display.percentType & FF_PERCENTAGE_TYPE_BAR_BIT) { - ffAppendPercentBar(&str, percentage, 0, 50, 80); + ffAppendPercentBar(&str, percentage, 50, 80); ffStrbufAppendC(&str, ' '); } diff --git a/src/modules/swap/swap.c b/src/modules/swap/swap.c index 6b4fb168b..83c0d86cf 100644 --- a/src/modules/swap/swap.c +++ b/src/modules/swap/swap.c @@ -37,7 +37,7 @@ void ffPrintSwap(FFSwapOptions* options) { if(instance.config.display.percentType & FF_PERCENTAGE_TYPE_BAR_BIT) { - ffAppendPercentBar(&str, 0, 0, 50, 80); + ffAppendPercentBar(&str, 0, 50, 80); ffStrbufAppendC(&str, ' '); } if(!(instance.config.display.percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT)) @@ -47,7 +47,7 @@ void ffPrintSwap(FFSwapOptions* options) { if(instance.config.display.percentType & FF_PERCENTAGE_TYPE_BAR_BIT) { - ffAppendPercentBar(&str, percentage, 0, 50, 80); + ffAppendPercentBar(&str, percentage, 50, 80); ffStrbufAppendC(&str, ' '); }