From 856d732e13bd25e59f38b6173a878324dbb2e308 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Mon, 10 Jul 2023 15:25:45 +0800 Subject: [PATCH] Global: add option `--size-ndigits` and `--size-max-prefix` --- CHANGELOG.md | 1 + doc/json_schema.json | 10 ++++++++++ src/common/init.c | 2 ++ src/common/jsonconfig.c | 4 ++++ src/common/parsing.c | 18 ++++++++---------- src/data/config_user.txt | 12 ++++++++++++ src/data/help.txt | 2 ++ src/fastfetch.c | 4 ++++ src/fastfetch.h | 2 ++ 9 files changed, 45 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3270a4c3b..b11b10f56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ Features: * Add `--wmi-timeout` option (Windows) * Add `--logo-type small` to search for small logos * Support detecting brightness of external displays with DDC/CI (guard behind `--allow-slow-operations`) (Brightness) +* Add option `--size-ndigits` and `--size-max-prefix` (#494) # 1.12.2 diff --git a/doc/json_schema.json b/doc/json_schema.json index ce264db47..01f05602d 100644 --- a/doc/json_schema.json +++ b/doc/json_schema.json @@ -296,6 +296,16 @@ "jedec" ] }, + "sizeNdigits": { + "type": "integer", + "title": "Set the number of digits to keep after the decimal point when formatting sizes", + "default": 2 + }, + "sizeMaxPrefix": { + "type": "integer", + "title": "Set the largest binary prefix to use when formatting sizes. 0 is B; 1 is KB; 2 is MB; etc.", + "default": 255 + }, "percentType": { "type": "number", "title": "Set the percentage output type. 1 for percentage number, 2 for bar, 3 for both, 6 for bar only, 9 for colored number", diff --git a/src/common/init.c b/src/common/init.c index c17188bd4..53426ad2f 100644 --- a/src/common/init.c +++ b/src/common/init.c @@ -60,6 +60,8 @@ static void defaultConfig(void) instance.config.escapeBedrock = true; instance.config.binaryPrefixType = FF_BINARY_PREFIX_TYPE_IEC; + instance.config.sizeNdigits = 2; + instance.config.sizeMaxPrefix = UINT8_MAX; instance.config.multithreading = true; instance.config.stat = false; instance.config.noBuffer = false; diff --git a/src/common/jsonconfig.c b/src/common/jsonconfig.c index f67766a7c..4b6d09546 100644 --- a/src/common/jsonconfig.c +++ b/src/common/jsonconfig.c @@ -386,6 +386,10 @@ const char* ffParseDisplayJsonConfig(void) if (error) return error; config->binaryPrefixType = (FFBinaryPrefixType) value; } + else if (ffStrEqualsIgnCase(key, "sizeNdigits")) + config->sizeNdigits = (uint8_t) yyjson_get_uint(val); + else if (ffStrEqualsIgnCase(key, "sizeMaxPrefix")) + config->sizeMaxPrefix = (uint8_t) yyjson_get_uint(val); else if (ffStrEqualsIgnCase(key, "percentType")) config->percentType = (uint32_t) yyjson_get_uint(val); else if (ffStrEqualsIgnCase(key, "noBuffer")) diff --git a/src/common/parsing.c b/src/common/parsing.c index 4bd8c19be..8fd5da367 100644 --- a/src/common/parsing.c +++ b/src/common/parsing.c @@ -60,12 +60,12 @@ void ffVersionToPretty(const FFVersion* version, FFstrbuf* pretty) ffStrbufAppendF(pretty, ".%u", version->patch); } -static void parseSize(FFstrbuf* result, uint64_t bytes, uint32_t base, uint8_t prefixesLength, const char** prefixes) +static void parseSize(FFstrbuf* result, uint64_t bytes, uint32_t base, const char** prefixes) { - long double size = (long double) bytes; + double size = (double) bytes; uint8_t counter = 0; - while(size >= base && counter < prefixesLength - 1) + while(size >= base && counter < instance.config.sizeMaxPrefix && prefixes[counter + 1]) { size /= base; counter++; @@ -73,22 +73,20 @@ static void parseSize(FFstrbuf* result, uint64_t bytes, uint32_t base, uint8_t p if(counter == 0) ffStrbufAppendF(result, "%"PRIu64" %s", bytes, prefixes[0]); - else if(counter < 3 || (counter == 3 && size < 100.0)) - ffStrbufAppendF(result, "%.2Lf %s", size, prefixes[counter]); else - ffStrbufAppendF(result, "%.0Lf %s", size, prefixes[counter]); + ffStrbufAppendF(result, "%.*f %s", instance.config.sizeNdigits, size, prefixes[counter]); } void ffParseSize(uint64_t bytes, FFBinaryPrefixType binaryPrefix, FFstrbuf* result) { if(binaryPrefix == FF_BINARY_PREFIX_TYPE_IEC) - parseSize(result, bytes, 1024, 5, (const char*[]) {"B", "KiB", "MiB", "GiB", "TiB"}); + parseSize(result, bytes, 1024, (const char*[]) {"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB", NULL}); else if(binaryPrefix == FF_BINARY_PREFIX_TYPE_SI) - parseSize(result, bytes, 1000, 5, (const char*[]) {"B", "kB", "MB", "GB", "TB"}); + parseSize(result, bytes, 1000, (const char*[]) {"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB", NULL}); else if(binaryPrefix == FF_BINARY_PREFIX_TYPE_JEDEC) - parseSize(result, bytes, 1024, 5, (const char*[]) {"B", "KB", "MB", "GB", "TB"}); + parseSize(result, bytes, 1024, (const char*[]) {"B", "KB", "MB", "GB", "TB", NULL}); else - parseSize(result, bytes, 1024, 1, (const char*[]) {"B"}); + parseSize(result, bytes, 1024, (const char*[]) {"B", NULL}); } void ffParseGTK(FFstrbuf* buffer, const FFstrbuf* gtk2, const FFstrbuf* gtk3, const FFstrbuf* gtk4) diff --git a/src/data/config_user.txt b/src/data/config_user.txt index b424a4585..2128c51a2 100644 --- a/src/data/config_user.txt +++ b/src/data/config_user.txt @@ -152,6 +152,18 @@ # Default is IEC. #--binary-prefix IEC +# Size number of digits option: +# Sets the number of digits to keep after the decimal point when formatting sizes. +# Must be an possitive integer. +# Default is 2. +#--size-ndigits 2 + +# Size max prefix option: +# Sets the largest binary prefix to use when formatting sizes. 0 is B; 1 is KB; 2 is MB; etc. +# Must be an possitive integer. +# Default is 255 (unlimited). +#--size-max-prefix 255 + # Disable output buffer option: # Sets if the stdout application buffer should be disabled. # Must be true or false. diff --git a/src/data/help.txt b/src/data/help.txt index 0f60e7b7a..fdc21004a 100644 --- a/src/data/help.txt +++ b/src/data/help.txt @@ -65,6 +65,8 @@ Display options: --binary-prefix : Set the binary prefix to used. Must be IEC, SI or JEDEC. Default is IEC --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 --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 + --size-max-prefix : Set the largest binary prefix to use when formatting sizes. 0 is B; 1 is KB; 2 is MB; etc. General module options: ---format : Set the format string to use for each specific module. diff --git a/src/fastfetch.c b/src/fastfetch.c index c6850f7d4..94453774b 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -915,6 +915,10 @@ static void parseOption(FFdata* data, const char* key, const char* value) {} }); } + else if(ffStrEqualsIgnCase(key, "--size-ndigits")) + instance.config.sizeNdigits = (uint8_t) ffOptionParseUInt32(key, value); + else if(ffStrEqualsIgnCase(key, "--size-max-prefix")) + instance.config.sizeMaxPrefix = (uint8_t) ffOptionParseUInt32(key, value); else if(ffStrEqualsIgnCase(key, "--percent-type")) instance.config.percentType = ffOptionParseUInt32(key, value); else if(ffStrEqualsIgnCase(key, "--no-buffer")) diff --git a/src/fastfetch.h b/src/fastfetch.h index f59506877..5c455cd70 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -44,6 +44,8 @@ typedef struct FFconfig bool hideCursor; bool escapeBedrock; FFBinaryPrefixType binaryPrefixType; + uint8_t sizeNdigits; + uint8_t sizeMaxPrefix; bool pipe; //disables logo and all escape sequences bool multithreading; bool stat;