From a0c4f2febd8db69bcdf0f860b62fa75e823b1bfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Wed, 16 Aug 2023 16:46:21 +0800 Subject: [PATCH] Global: support `--bar-width` --- CHANGELOG.md | 2 +- doc/json_schema.json | 6 +++++ presets/examples/9.jsonc | 28 ++++++++++++++++++++++++ src/common/bar.c | 34 +++++++++++++---------------- src/common/bar.h | 4 ++-- src/common/init.c | 1 + src/common/jsonconfig.c | 6 +++++ src/data/help.txt | 1 + src/fastfetch.c | 2 ++ src/fastfetch.h | 1 + src/modules/battery/battery.c | 8 +++---- src/modules/brightness/brightness.c | 4 ++-- src/modules/cpuusage/cpuusage.c | 4 ++-- src/modules/disk/disk.c | 6 ++--- src/modules/gpu/gpu.c | 2 +- src/modules/memory/memory.c | 8 +++---- src/modules/swap/swap.c | 10 ++++----- 17 files changed, 84 insertions(+), 43 deletions(-) create mode 100644 presets/examples/9.jsonc diff --git a/CHANGELOG.md b/CHANGELOG.md index 1df312cdc..052e00c5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ Changes: Features: * Add `--key-width` for aligning the left edge of values, supported both for global `--key-width` and specific module `--module-key-width` -* Add `--bar-char-elapsed`, `--bar-char-total` and `--bar-border` options +* Add `--bar-char-elapsed`, `--bar-char-total`, `--bar-width` and `--bar-border` options Bugfixes: * Fix label detection. Use `--disk-key 'Disk ({2})'` to display it (Disk, Linux) diff --git a/doc/json_schema.json b/doc/json_schema.json index 7980a2837..0ecf5920a 100644 --- a/doc/json_schema.json +++ b/doc/json_schema.json @@ -370,6 +370,12 @@ "type": "boolean", "title": "Whether to show a border around the bar", "default": true + }, + "width": { + "type": "integer", + "title": "Set the width of the bar", + "minimum": 1, + "default": 10 } } }, diff --git a/presets/examples/9.jsonc b/presets/examples/9.jsonc new file mode 100644 index 000000000..1dd6f7307 --- /dev/null +++ b/presets/examples/9.jsonc @@ -0,0 +1,28 @@ +{ + "$schema": "https://github.com/fastfetch-cli/fastfetch/raw/dev/doc/json_schema.json", + "logo": { + "type": "small" + }, + "display": { + "percentType": 6, + "keyWidth": 11, + "bar": { + "charElapsed": "=", + "charTotal": "-", + "width": 13 + } + }, + "modules": [ + "title", + "separator", + "memory", + "swap", + "disk", + "battery", + { + "type": "colors", + "paddingLeft": 11, + "symbol": "circle" + } + ] +} diff --git a/src/common/bar.c b/src/common/bar.c index f71326a17..549c3ba50 100644 --- a/src/common/bar.c +++ b/src/common/bar.c @@ -2,19 +2,15 @@ #include "common/color.h" #include "util/textModifier.h" -// green, yellow, red: print the color on nth (0~9) block -// set its value == 10 means the color will not be printed -void ffAppendPercentBar(FFstrbuf* buffer, uint8_t percent, uint8_t green, uint8_t yellow, uint8_t red) +void ffAppendPercentBar(FFstrbuf* buffer, double percent, uint8_t green, uint8_t yellow, uint8_t red) { - assert(green <= 10 && yellow <= 10 && red <= 10); + assert(green <= 100 && yellow <= 100 && red <= 100); - // [ 0%, 5%) prints 0 blocks - // [ 5%, 15%) prints 1 block; - // ... - // [85%, 95%) prints 9 blocks; - // [95%,100%] prints 10 blocks - percent = (uint8_t)(percent + 5) / 10; - assert(percent <= 10); + uint32_t blocksPercent = (uint32_t) (percent / 100.0 * instance.config.barWidth + 0.5); + uint32_t blocksGreen = (uint32_t) (green / 100.0 * instance.config.barWidth + 0.5); + uint32_t blocksYellow = (uint32_t) (yellow / 100.0 * instance.config.barWidth + 0.5); + uint32_t blocksRed = (uint32_t) (red / 100.0 * instance.config.barWidth + 0.5); + assert(blocksPercent <= instance.config.barWidth); if(instance.config.barBorder) { @@ -24,25 +20,25 @@ void ffAppendPercentBar(FFstrbuf* buffer, uint8_t percent, uint8_t green, uint8_ ffStrbufAppendS(buffer, "[ "); } - for (uint8_t i = 0; i < percent; ++i) + for (uint32_t i = 0; i < blocksPercent; ++i) { if(!instance.config.pipe) { - if (i == green) + if (i == blocksGreen) ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_GREEN "m"); - else if (i == yellow) + else if (i == blocksYellow) ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_YELLOW "m"); - else if (i == red) + else if (i == blocksRed) ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_RED "m"); } ffStrbufAppend(buffer, &instance.config.barCharElapsed); } - if (percent < 10) + if (blocksPercent < instance.config.barWidth) { if(!instance.config.pipe) ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_WHITE "m"); - for (uint8_t i = percent; i < 10; ++i) + for (uint32_t i = blocksPercent; i < instance.config.barWidth; ++i) ffStrbufAppend(buffer, &instance.config.barCharTotal); } @@ -67,7 +63,7 @@ void ffAppendPercentBar(FFstrbuf* buffer, uint8_t percent, uint8_t green, uint8_ // [green, 100]: print green // [yellow, green): print yellow // [0, yellow): PRINT RED -void ffAppendPercentNum(FFstrbuf* buffer, uint8_t percent, uint8_t green, uint8_t yellow, bool parentheses) +void ffAppendPercentNum(FFstrbuf* buffer, double percent, uint8_t green, uint8_t yellow, bool parentheses) { assert(green <= 100 && yellow <= 100); @@ -97,7 +93,7 @@ void ffAppendPercentNum(FFstrbuf* buffer, uint8_t percent, uint8_t green, uint8_ ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_RED "m"); } } - ffStrbufAppendF(buffer, "%u%%", (unsigned) percent); + ffStrbufAppendF(buffer, "%u%%", (unsigned) (percent + 0.5)); if (colored && !instance.config.pipe) { diff --git a/src/common/bar.h b/src/common/bar.h index d776454d4..d35276cef 100644 --- a/src/common/bar.h +++ b/src/common/bar.h @@ -13,7 +13,7 @@ enum FF_PERCENTAGE_TYPE_NUM_COLOR_BIT = 1 << 3, }; -void ffAppendPercentBar(FFstrbuf* buffer, uint8_t percent, uint8_t green, uint8_t yellow, uint8_t red); -void ffAppendPercentNum(FFstrbuf* buffer, uint8_t percent, uint8_t green, uint8_t yellow, bool parentheses); +void ffAppendPercentBar(FFstrbuf* buffer, double percent, uint8_t green, uint8_t yellow, uint8_t red); +void ffAppendPercentNum(FFstrbuf* buffer, double percent, uint8_t green, uint8_t yellow, bool parentheses); #endif diff --git a/src/common/init.c b/src/common/init.c index 61c6ad9c1..3510818b3 100644 --- a/src/common/init.c +++ b/src/common/init.c @@ -72,6 +72,7 @@ static void defaultConfig(void) ffStrbufInitStatic(&instance.config.barCharElapsed, "■"); ffStrbufInitStatic(&instance.config.barCharTotal, "-"); + instance.config.barWidth = 10; instance.config.barBorder = true; instance.config.percentType = 1; diff --git a/src/common/jsonconfig.c b/src/common/jsonconfig.c index b96ed5186..e504eafda 100644 --- a/src/common/jsonconfig.c +++ b/src/common/jsonconfig.c @@ -443,12 +443,18 @@ const char* ffParseDisplayJsonConfig(void) const char* charElapsed = yyjson_get_str(yyjson_obj_get(val, "charElapsed")); if (charElapsed) ffStrbufSetS(&config->barCharElapsed, charElapsed); + const char* charTotal = yyjson_get_str(yyjson_obj_get(val, "charTotal")); if (charTotal) ffStrbufSetS(&config->barCharTotal, charTotal); + yyjson_val* border = yyjson_obj_get(val, "border"); if (border) config->barBorder = yyjson_get_bool(border); + + yyjson_val* width = yyjson_obj_get(val, "width"); + if (width) + config->barWidth = (uint8_t) yyjson_get_uint(width); } else return "display.bar must be an object"; diff --git a/src/data/help.txt b/src/data/help.txt index 7f8642d37..4cac37769 100644 --- a/src/data/help.txt +++ b/src/data/help.txt @@ -71,6 +71,7 @@ Display options: --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 --bar-char-elapsed : Set the character to use in elapsed part. Default is '■' --bar-char-total : Set the character to use in total part. Default is '-' + --bar-width : Set the width of the bar, in number of characters. Default is 10 --bar-border : Whether to show a border around the bar. Default is true --no-buffer : Set if the stdout application buffer should be disabled. Default is false --size-ndigits : Set the number of digits to keep after the decimal point when formatting sizes diff --git a/src/fastfetch.c b/src/fastfetch.c index 939e6895f..1625126bd 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -1051,6 +1051,8 @@ static void parseOption(FFdata* data, const char* key, const char* value) ffOptionParseString(key, value, &instance.config.barCharElapsed); else if(ffStrEqualsIgnCase(key, "--bar-char-total")) ffOptionParseString(key, value, &instance.config.barCharTotal); + else if(ffStrEqualsIgnCase(key, "--bar-width")) + instance.config.barWidth = (uint8_t) ffOptionParseUInt32(key, value); else if(ffStrEqualsIgnCase(key, "--bar-border")) instance.config.barBorder = ffOptionParseBoolean(value); else if(ffStrEqualsIgnCase(key, "--no-buffer")) diff --git a/src/fastfetch.h b/src/fastfetch.h index 61b72be5b..3a493eea5 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -56,6 +56,7 @@ typedef struct FFconfig FFTemperatureUnit temperatureUnit; FFstrbuf barCharElapsed; FFstrbuf barCharTotal; + uint8_t barWidth; bool barBorder; uint32_t percentType; bool pipe; //disables logo and all escape sequences diff --git a/src/modules/battery/battery.c b/src/modules/battery/battery.c index eba6ba597..2dac37d05 100644 --- a/src/modules/battery/battery.c +++ b/src/modules/battery/battery.c @@ -26,11 +26,11 @@ static void printBattery(FFBatteryOptions* options, BatteryResult* result, uint8 if(instance.config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT) { if(result->capacity <= 20) - ffAppendPercentBar(&str, (uint8_t)result->capacity, 10, 10, 0); + ffAppendPercentBar(&str, result->capacity, 100, 100, 0); else if(result->capacity <= 50) - ffAppendPercentBar(&str, (uint8_t)result->capacity, 10, 0, 10); + ffAppendPercentBar(&str, result->capacity, 100, 0, 100); else - ffAppendPercentBar(&str, (uint8_t)result->capacity, 0, 10, 10); + ffAppendPercentBar(&str, result->capacity, 0, 100, 100); } if(instance.config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT) @@ -38,7 +38,7 @@ static void printBattery(FFBatteryOptions* options, BatteryResult* result, uint8 if(str.length > 0) ffStrbufAppendC(&str, ' '); - ffAppendPercentNum(&str, (uint8_t) result->capacity, 51, 21, str.length > 0); + ffAppendPercentNum(&str, result->capacity, 51, 21, str.length > 0); } } diff --git a/src/modules/brightness/brightness.c b/src/modules/brightness/brightness.c index d5aa58b46..3e6a564c3 100644 --- a/src/modules/brightness/brightness.c +++ b/src/modules/brightness/brightness.c @@ -51,7 +51,7 @@ void ffPrintBrightness(FFBrightnessOptions* options) if (instance.config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT) { - ffAppendPercentBar(&str, (uint8_t) (item->value + 0.5), 0, 10, 10); + ffAppendPercentBar(&str, item->value, 0, 100, 100); } if(instance.config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT) @@ -59,7 +59,7 @@ void ffPrintBrightness(FFBrightnessOptions* options) if(str.length > 0) ffStrbufAppendC(&str, ' '); - ffAppendPercentNum(&str, (uint8_t) (item->value + 0.5), 10, 10, str.length > 0); + ffAppendPercentNum(&str, item->value, 10, 10, str.length > 0); } ffStrbufPutTo(&str, stdout); diff --git a/src/modules/cpuusage/cpuusage.c b/src/modules/cpuusage/cpuusage.c index bb8e015ee..263fe109f 100644 --- a/src/modules/cpuusage/cpuusage.c +++ b/src/modules/cpuusage/cpuusage.c @@ -25,12 +25,12 @@ void ffPrintCPUUsage(FFCPUUsageOptions* options) FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate(); if(instance.config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT) - ffAppendPercentBar(&str, (uint8_t)percentage, 0, 5, 8); + ffAppendPercentBar(&str, percentage, 0, 50, 80); if(instance.config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT) { if(str.length > 0) ffStrbufAppendC(&str, ' '); - ffAppendPercentNum(&str, (uint8_t) percentage, 50, 80, str.length > 0); + ffAppendPercentNum(&str, percentage, 50, 80, str.length > 0); } ffStrbufPutTo(&str, stdout); } diff --git a/src/modules/disk/disk.c b/src/modules/disk/disk.c index c7269a6d0..e79bcd692 100644 --- a/src/modules/disk/disk.c +++ b/src/modules/disk/disk.c @@ -46,7 +46,7 @@ static void printDisk(FFDiskOptions* options, const FFDisk* disk) FF_STRBUF_AUTO_DESTROY totalPretty = ffStrbufCreate(); ffParseSize(disk->bytesTotal, &totalPretty); - uint8_t bytesPercentage = disk->bytesTotal > 0 ? (uint8_t) (((long double) disk->bytesUsed / (long double) disk->bytesTotal) * 100.0) : 0; + double bytesPercentage = disk->bytesTotal > 0 ? (double) disk->bytesUsed / (double) disk->bytesTotal * 100.0 : 0; if(options->moduleArgs.outputFormat.length == 0) { @@ -58,7 +58,7 @@ static void printDisk(FFDiskOptions* options, const FFDisk* disk) { if(instance.config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT) { - ffAppendPercentBar(&str, bytesPercentage, 0, 5, 8); + ffAppendPercentBar(&str, bytesPercentage, 0, 50, 80); ffStrbufAppendC(&str, ' '); } @@ -67,7 +67,7 @@ static void printDisk(FFDiskOptions* options, const FFDisk* disk) if(instance.config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT) { - ffAppendPercentNum(&str, (uint8_t) bytesPercentage, 50, 80, str.length > 0); + ffAppendPercentNum(&str, bytesPercentage, 50, 80, str.length > 0); ffStrbufAppendC(&str, ' '); } } diff --git a/src/modules/gpu/gpu.c b/src/modules/gpu/gpu.c index 9f25e72e6..b1b8fa090 100644 --- a/src/modules/gpu/gpu.c +++ b/src/modules/gpu/gpu.c @@ -58,7 +58,7 @@ static void printGPUResult(FFGPUOptions* options, uint8_t index, const FFGPUResu if(gpu->dedicated.used != FF_GPU_VMEM_SIZE_UNSET) { ffStrbufAppendS(&output, ", "); - ffAppendPercentNum(&output, (uint8_t) (gpu->dedicated.used * 100 / gpu->dedicated.total), 50, 80, false); + ffAppendPercentNum(&output, (double) gpu->dedicated.used / (double) gpu->dedicated.total * 100.0, 50, 80, false); } ffStrbufAppendC(&output, ')'); } diff --git a/src/modules/memory/memory.c b/src/modules/memory/memory.c index 7bc505c6e..a8b78316b 100644 --- a/src/modules/memory/memory.c +++ b/src/modules/memory/memory.c @@ -25,9 +25,9 @@ void ffPrintMemory(FFMemoryOptions* options) FF_STRBUF_AUTO_DESTROY totalPretty = ffStrbufCreate(); ffParseSize(storage.bytesTotal, &totalPretty); - uint8_t percentage = storage.bytesTotal == 0 + double percentage = storage.bytesTotal == 0 ? 0 - : (uint8_t) (((long double) storage.bytesUsed / (long double) storage.bytesTotal) * 100.0); + : (double) storage.bytesUsed / (double) storage.bytesTotal * 100.0; if(options->moduleArgs.outputFormat.length == 0) { @@ -40,7 +40,7 @@ void ffPrintMemory(FFMemoryOptions* options) if(instance.config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT) { - ffAppendPercentBar(&str, percentage, 0, 5, 8); + ffAppendPercentBar(&str, percentage, 0, 50, 80); ffStrbufAppendC(&str, ' '); } @@ -48,7 +48,7 @@ void ffPrintMemory(FFMemoryOptions* options) ffStrbufAppendF(&str, "%s / %s ", usedPretty.chars, totalPretty.chars); if(instance.config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT) - ffAppendPercentNum(&str, (uint8_t) percentage, 50, 80, str.length > 0); + ffAppendPercentNum(&str, percentage, 50, 80, str.length > 0); ffStrbufTrimRight(&str, ' '); ffStrbufPutTo(&str, stdout); diff --git a/src/modules/swap/swap.c b/src/modules/swap/swap.c index 844bfe896..7da77e086 100644 --- a/src/modules/swap/swap.c +++ b/src/modules/swap/swap.c @@ -25,9 +25,9 @@ void ffPrintSwap(FFSwapOptions* options) FF_STRBUF_AUTO_DESTROY totalPretty = ffStrbufCreate(); ffParseSize(storage.bytesTotal, &totalPretty); - uint8_t percentage = storage.bytesTotal == 0 + double percentage = storage.bytesTotal == 0 ? 0 - : (uint8_t) (((long double) storage.bytesUsed / (long double) storage.bytesTotal) * 100.0); + : (double) storage.bytesUsed / (double) storage.bytesTotal * 100.0; if(options->moduleArgs.outputFormat.length == 0) { @@ -37,7 +37,7 @@ void ffPrintSwap(FFSwapOptions* options) { if(instance.config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT) { - ffAppendPercentBar(&str, 0, 0, 5, 8); + ffAppendPercentBar(&str, 0, 0, 50, 80); ffStrbufAppendC(&str, ' '); } if(!(instance.config.percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT)) @@ -47,7 +47,7 @@ void ffPrintSwap(FFSwapOptions* options) { if(instance.config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT) { - ffAppendPercentBar(&str, percentage, 0, 5, 8); + ffAppendPercentBar(&str, percentage, 0, 50, 80); ffStrbufAppendC(&str, ' '); } @@ -55,7 +55,7 @@ void ffPrintSwap(FFSwapOptions* options) ffStrbufAppendF(&str, "%s / %s ", usedPretty.chars, totalPretty.chars); if(instance.config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT) - ffAppendPercentNum(&str, (uint8_t) percentage, 50, 80, str.length > 0); + ffAppendPercentNum(&str, percentage, 50, 80, str.length > 0); } ffStrbufTrimRight(&str, ' ');