From 2b5511997cee9c9107d1c78ce09fd5fbd36777a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 12 Jan 2023 22:41:11 +0800 Subject: [PATCH] Global: support hiding other texts when printing percentage values --- CHANGELOG.md | 1 + src/common/bar.h | 1 + src/data/config_user.txt | 2 +- src/data/help.txt | 2 +- src/modules/battery.c | 3 ++- src/modules/disk.c | 15 ++++++++++----- src/modules/memory.c | 6 ++++-- 7 files changed, 20 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dcbc706da..737092a23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ Features: * Detect terminal version when available * Support `WezTerm` terminal font detection (requires [`wezterm` executable](https://wezfurlong.org/wezterm/cli/general.html) being available) * Add `--shell-version` and `--terminal-version` options to disable shell / terminal version detection +* Enhance `--percent-type` to allow hiding other texts (#387) Logos: * Raspbian (@IamNoRobot, #373) diff --git a/src/common/bar.h b/src/common/bar.h index a6c2be234..575adc01d 100644 --- a/src/common/bar.h +++ b/src/common/bar.h @@ -9,6 +9,7 @@ enum { FF_PERCENTAGE_TYPE_NUM_BIT = 1, FF_PERCENTAGE_TYPE_BAR_BIT = 2, + FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT = 4, }; void ffAppendPercentBar(FFinstance* instance, FFstrbuf* buffer, uint8_t percent, uint8_t green, uint8_t yellow, uint8_t red); diff --git a/src/data/config_user.txt b/src/data/config_user.txt index b68b845ca..435143dc4 100644 --- a/src/data/config_user.txt +++ b/src/data/config_user.txt @@ -259,7 +259,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 +# 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 #--percent-type 1 # Key options: diff --git a/src/data/help.txt b/src/data/help.txt index c83dec794..81b52dd7f 100644 --- a/src/data/help.txt +++ b/src/data/help.txt @@ -125,7 +125,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. Default is 1 + --percent-type : Set the percentage output type. 1 for percentage number, 2 for bar, 3 for both, 6 for bar only. Default is 1 Parsing is not case sensitive. E.g. "--lib-PCI" is equal to "--Lib-Pci" If a value starts with a ?, it is optional. "true" will be used if not set. diff --git a/src/modules/battery.c b/src/modules/battery.c index 97c603d9a..79cdf4c79 100644 --- a/src/modules/battery.c +++ b/src/modules/battery.c @@ -13,6 +13,7 @@ static void printBattery(FFinstance* instance, BatteryResult* result, uint8_t in ffPrintLogoAndKey(instance, FF_BATTERY_MODULE_NAME, index, &instance->config.battery.key); bool showStatus = + !(instance->config.percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT) && result->status.length > 0 && ffStrbufIgnCaseCompS(&result->status, "Unknown") != 0; @@ -35,7 +36,7 @@ static void printBattery(FFinstance* instance, BatteryResult* result, uint8_t in { if(str.length > 0) ffStrbufAppendC(&str, ' '); - ffStrbufAppendF(&str, "%.0f%%", result->capacity); + ffStrbufAppendF(&str, str.length > 0 ? "(%u%%)" : "%u%%", (unsigned) result->capacity); } } diff --git a/src/modules/disk.c b/src/modules/disk.c index ba07fb823..df7614d5c 100644 --- a/src/modules/disk.c +++ b/src/modules/disk.c @@ -47,17 +47,22 @@ static void printDisk(FFinstance* instance, const FFDisk* disk) ffAppendPercentBar(instance, &str, bytesPercentage, 0, 5, 8); ffStrbufAppendC(&str, ' '); } - ffStrbufAppendF(&str, "%s / %s", usedPretty.chars, totalPretty.chars); + + if(!(instance->config.percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT)) + ffStrbufAppendF(&str, "%s / %s ", usedPretty.chars, totalPretty.chars); if(instance->config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT) - ffStrbufAppendF(&str, " (%u%%)", bytesPercentage); + { + ffStrbufAppendF(&str, str.length > 0 ? "(%u%%) " : "%u%% ", bytesPercentage); + } } else - ffStrbufAppendS(&str, "Unknown"); + ffStrbufAppendS(&str, "Unknown "); - if(disk->type == FF_DISK_TYPE_EXTERNAL) - ffStrbufAppendS(&str, " [Removable]"); + if(disk->type == FF_DISK_TYPE_EXTERNAL && !(instance->config.percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT)) + ffStrbufAppendS(&str, "[Removable]"); + ffStrbufTrimRight(&str, ' '); ffStrbufPutTo(&str, stdout); ffStrbufDestroy(&str); } diff --git a/src/modules/memory.c b/src/modules/memory.c index 149eef45b..fbb893f02 100644 --- a/src/modules/memory.c +++ b/src/modules/memory.c @@ -52,11 +52,13 @@ static void printMemory(FFinstance* instance, const char* name, const FFModuleAr ffStrbufAppendC(&str, ' '); } - ffStrbufAppendF(&str, "%s / %s", usedPretty.chars, totalPretty.chars); + if(!(instance->config.percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT)) + ffStrbufAppendF(&str, "%s / %s ", usedPretty.chars, totalPretty.chars); if(instance->config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT) - ffStrbufAppendF(&str, " (%u%%)", percentage); + ffStrbufAppendF(&str, str.length > 0 ? "(%u%%)" : "%u%%", percentage); + ffStrbufTrimRight(&str, ' '); ffStrbufPutTo(&str, stdout); ffStrbufDestroy(&str); }