Global: add --bar-char-elapsed, --bar-char-total and --bar-border options

This commit is contained in:
李通洲
2023-08-16 14:41:56 +08:00
parent 8ed28673a9
commit 559bca4223
9 changed files with 101 additions and 28 deletions
+1
View File
@@ -7,6 +7,7 @@ Changes:
Features:
* Add `--key-width` for aligning the left edge of values
* Add `--bar-char-elapsed`, `--bar-char-total` and `--bar-border` options
Bugfixes:
* Fix label detection (Disk, Linux)
+21
View File
@@ -352,6 +352,27 @@
"enum": ["CELSIUS", "C", "FAHRENHEIT", "F", "KELVIN", "K"],
"default": "C"
},
"bar": {
"type": "object",
"title": "Set the bar configuration",
"properties": {
"charElapsed": {
"type": "string",
"title": "Set the character to use in elapsed part",
"default": "■"
},
"charTotal": {
"type": "string",
"title": "Set the character to use in total part",
"default": "-"
},
"border": {
"type": "boolean",
"title": "Whether to show a border around the bar",
"default": true
}
}
},
"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",
+30 -20
View File
@@ -1,5 +1,6 @@
#include "common/bar.h"
#include "common/color.h"
#include "util/textModifier.h"
#include "bar.h"
// green, yellow, red: print the color on nth (0~9) block
// set its value == 10 means the color will not be printed
@@ -15,37 +16,46 @@ void ffAppendPercentBar(FFstrbuf* buffer, uint8_t percent, uint8_t green, uint8_
percent = (uint8_t)(percent + 5) / 10;
assert(percent <= 10);
if(!instance.config.pipe)
ffStrbufAppendS(buffer, "\033[97m[ ");
else
ffStrbufAppendS(buffer, "[ ");
if(instance.config.barBorder)
{
if(!instance.config.pipe)
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_WHITE "m[ ");
else
ffStrbufAppendS(buffer, "[ ");
}
for (uint8_t i = 0; i < percent; ++i)
{
if(!instance.config.pipe)
{
if (i == green)
ffStrbufAppendS(buffer, "\033[32m");
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_GREEN "m");
else if (i == yellow)
ffStrbufAppendS(buffer, "\033[93m");
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_YELLOW "m");
else if (i == red)
ffStrbufAppendS(buffer, "\033[91m");
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_RED "m");
}
ffStrbufAppendS(buffer, "■");
ffStrbufAppend(buffer, &instance.config.barCharElapsed);
}
if (percent < 10)
{
if(!instance.config.pipe)
ffStrbufAppendS(buffer, "\033[97m");
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_WHITE "m");
for (uint8_t i = percent; i < 10; ++i)
ffStrbufAppendS(buffer, "-");
ffStrbufAppend(buffer, &instance.config.barCharTotal);
}
if(instance.config.barBorder)
{
if(!instance.config.pipe)
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_WHITE "m ]");
else
ffStrbufAppendS(buffer, " ]");
}
if(!instance.config.pipe)
ffStrbufAppendS(buffer, "\033[97m ]" FASTFETCH_TEXT_MODIFIER_RESET);
else
ffStrbufAppendS(buffer, " ]");
ffStrbufAppendS(buffer, FASTFETCH_TEXT_MODIFIER_RESET);
}
// if (green < yellow)
@@ -71,20 +81,20 @@ void ffAppendPercentNum(FFstrbuf* buffer, uint8_t percent, uint8_t green, uint8_
if(green < yellow)
{
if (percent <= green)
ffStrbufAppendS(buffer, "\033[32m");
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_GREEN "m");
else if (percent <= yellow)
ffStrbufAppendS(buffer, "\033[93m");
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_YELLOW "m");
else
ffStrbufAppendS(buffer, "\033[91m");
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_RED "m");
}
else
{
if (percent >= green)
ffStrbufAppendS(buffer, "\033[32m");
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_GREEN "m");
else if (percent >= yellow)
ffStrbufAppendS(buffer, "\033[93m");
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_YELLOW "m");
else
ffStrbufAppendS(buffer, "\033[91m");
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_RED "m");
}
}
ffStrbufAppendF(buffer, "%u%%", (unsigned) percent);
+7 -2
View File
@@ -70,6 +70,11 @@ static void defaultConfig(void)
instance.config.noBuffer = false;
instance.config.keyWidth = 0;
ffStrbufInitStatic(&instance.config.barCharElapsed, "■");
ffStrbufInitStatic(&instance.config.barCharTotal, "-");
instance.config.barBorder = true;
instance.config.percentType = 1;
ffInitTitleOptions(&instance.config.title);
ffInitOSOptions(&instance.config.os);
ffInitHostOptions(&instance.config.host);
@@ -146,8 +151,6 @@ static void defaultConfig(void)
ffStrbufInit(&instance.config.libPulse);
ffStrbufInit(&instance.config.libnm);
ffStrbufInit(&instance.config.libDdcutil);
instance.config.percentType = 1;
}
void ffInitInstance(void)
@@ -273,6 +276,8 @@ static void destroyConfig(void)
ffStrbufDestroy(&instance.config.colorKeys);
ffStrbufDestroy(&instance.config.colorTitle);
ffStrbufDestroy(&instance.config.keyValueSeparator);
ffStrbufDestroy(&instance.config.barCharElapsed);
ffStrbufDestroy(&instance.config.barCharTotal);
#if defined(__linux__) || defined(__FreeBSD__)
ffStrbufDestroy(&instance.config.playerName);
+17
View File
@@ -436,6 +436,23 @@ const char* ffParseDisplayJsonConfig(void)
}
else if (ffStrEqualsIgnCase(key, "percentType"))
config->percentType = (uint32_t) yyjson_get_uint(val);
else if (ffStrEqualsIgnCase(key, "bar"))
{
if (yyjson_is_obj(val))
{
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);
}
else
return "display.bar must be an object";
}
else if (ffStrEqualsIgnCase(key, "noBuffer"))
config->noBuffer = yyjson_get_bool(val);
else if (ffStrEqualsIgnCase(key, "keyWidth"))
+3
View File
@@ -69,6 +69,9 @@ Display options:
--hide-cursor <?value>: Whether to hide the cursor during the run
--binary-prefix <value>: Set the binary prefix to used. Must be IEC, SI or JEDEC. Default is IEC
--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
--bar-char-elapsed <str>: Set the character to use in elapsed part. Default is '■'
--bar-char-total <str>: Set the character to use in total part. Default is '-'
--bar-border <?value>: Whether to show a border around the bar. Default is true
--no-buffer <?value>: Set if the stdout application buffer should be disabled. Default is false
--size-ndigits <value>: Set the number of digits to keep after the decimal point when formatting sizes
--size-max-prefix <value>: Set the largest binary prefix to use when formatting sizes. Default is YB
+6
View File
@@ -1047,6 +1047,12 @@ static void parseOption(FFdata* data, const char* key, const char* value)
}
else if(ffStrEqualsIgnCase(key, "--percent-type"))
instance.config.percentType = ffOptionParseUInt32(key, value);
else if(ffStrEqualsIgnCase(key, "--bar-char-elapsed"))
ffOptionParseString(key, value, &instance.config.barCharElapsed);
else if(ffStrEqualsIgnCase(key, "--bar-char-total"))
ffOptionParseString(key, value, &instance.config.barCharTotal);
else if(ffStrEqualsIgnCase(key, "--bar-border"))
instance.config.barBorder = ffOptionParseBoolean(value);
else if(ffStrEqualsIgnCase(key, "--no-buffer"))
instance.config.noBuffer = ffOptionParseBoolean(value);
+3
View File
@@ -54,6 +54,9 @@ typedef struct FFconfig
uint8_t sizeNdigits;
uint8_t sizeMaxPrefix;
FFTemperatureUnit temperatureUnit;
FFstrbuf barCharElapsed;
FFstrbuf barCharTotal;
bool barBorder;
uint32_t percentType;
bool pipe; //disables logo and all escape sequences
bool multithreading;
+13 -6
View File
@@ -32,12 +32,19 @@ void ffPrintSwap(FFSwapOptions* options)
if(options->moduleArgs.outputFormat.length == 0)
{
ffPrintLogoAndKey(FF_SWAP_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate();
if (storage.bytesTotal == 0)
puts("Disabled");
{
if(instance.config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
{
ffAppendPercentBar(&str, 0, 0, 5, 8);
ffStrbufAppendC(&str, ' ');
}
if(!(instance.config.percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT))
ffStrbufAppendS(&str, "Disabled");
}
else
{
FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate();
if(instance.config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
{
ffAppendPercentBar(&str, percentage, 0, 5, 8);
@@ -49,10 +56,10 @@ void ffPrintSwap(FFSwapOptions* options)
if(instance.config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
ffAppendPercentNum(&str, (uint8_t) percentage, 50, 80, str.length > 0);
ffStrbufTrimRight(&str, ' ');
ffStrbufPutTo(&str, stdout);
}
ffStrbufTrimRight(&str, ' ');
ffStrbufPutTo(&str, stdout);
}
else
{