Bar: add customizable bar color options for percentage bars

This commit is contained in:
李通洲
2025-08-01 13:50:02 +08:00
parent afe2c85047
commit c6675f82f0
5 changed files with 72 additions and 5 deletions
+14
View File
@@ -1027,6 +1027,20 @@
"description": "If both borderLeftElapsed and borderRightElapsed are set, the border will be used as parts of bar content",
"default": ""
},
"colorElapsed": {
"description": "Color to use in the elapsed part of percentage bars",
"$ref": "#/$defs/colors"
},
"colorTotal": {
"description": "Color to use in the total part of percentage bars",
"$ref": "#/$defs/colors",
"default": "light_white"
},
"colorBorder": {
"description": "Color to use in the borders of percentage bars",
"$ref": "#/$defs/colors",
"default": "light_white"
},
"width": {
"type": "integer",
"description": "Set the width of the bar, in number of characters",
+11 -5
View File
@@ -68,7 +68,7 @@ void ffPercentAppendBar(FFstrbuf* buffer, double percent, FFPercentageModuleConf
if(!borderAsValue && options->barBorderLeft.length)
{
if(!options->pipe)
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_WHITE "m");
ffStrbufAppendF(buffer, "\e[%sm", options->barColorBorder.chars);
ffStrbufAppend(buffer, &options->barBorderLeft);
}
@@ -78,11 +78,13 @@ void ffPercentAppendBar(FFstrbuf* buffer, double percent, FFPercentageModuleConf
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_BLACK "m");
for (uint32_t i = 0; i < options->barWidth; ++i)
{
ffStrbufAppend(buffer, borderAsValue && i == 0
? &options->barBorderLeft
: borderAsValue && i == options->barWidth - 1
? &options->barBorderRight
: &options->barCharTotal);
}
}
else
{
@@ -91,7 +93,7 @@ void ffPercentAppendBar(FFstrbuf* buffer, double percent, FFPercentageModuleConf
const char* colorRed = options->percentColorRed.chars;
FFPercentageTypeFlags percentType = config.type == 0 ? options->percentType : config.type;
bool monochrome = !!(percentType & FF_PERCENTAGE_TYPE_BAR_MONOCHROME_BIT);
bool monochrome = !!(percentType & FF_PERCENTAGE_TYPE_BAR_MONOCHROME_BIT) || options->barColorElapsed.length;
for (uint32_t i = 0; i < blocksPercent; ++i)
{
@@ -100,7 +102,9 @@ void ffPercentAppendBar(FFstrbuf* buffer, double percent, FFPercentageModuleConf
if (monochrome)
{
const char* color = NULL;
if (green <= yellow)
if (options->barColorElapsed.length)
color = options->barColorElapsed.chars;
else if (green <= yellow)
{
if (percent < green) color = colorGreen;
else if (percent < yellow) color = colorYellow;
@@ -136,20 +140,22 @@ void ffPercentAppendBar(FFstrbuf* buffer, double percent, FFPercentageModuleConf
if (blocksPercent < options->barWidth)
{
if(!options->pipe)
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_WHITE "m");
ffStrbufAppendF(buffer, "\e[%sm", options->barColorTotal.chars);
for (uint32_t i = blocksPercent; i < options->barWidth; ++i)
{
ffStrbufAppend(buffer, borderAsValue && i == 0
? &options->barBorderLeft
: borderAsValue && i == options->barWidth - 1
? &options->barBorderRight
: &options->barCharTotal);
}
}
}
if(!borderAsValue && options->barBorderRight.length)
{
if(!options->pipe)
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_WHITE "m");
ffStrbufAppendF(buffer, "\e[%sm", options->barColorBorder.chars);
ffStrbufAppend(buffer, &options->barBorderRight);
}
+25
View File
@@ -715,6 +715,31 @@
"default": ""
}
},
{
"long": "bar-color-elapsed",
"desc": "Set the color to use in the elapsed part of percentage bars",
"remark": "By default, auto selected by percent-color-{green,yellow,red}",
"arg": {
"type": "color",
"default": "<auto>"
}
},
{
"long": "bar-color-total",
"desc": "Set the color to use in the total part of percentage bars",
"arg": {
"type": "color",
"default": "light_white"
}
},
{
"long": "bar-color-border",
"desc": "Set the color to use in the borders of percentage bars",
"arg": {
"type": "color",
"default": "light_white"
}
},
{
"long": "bar-width",
"desc": "Set the width of percentage bars in characters",
+19
View File
@@ -289,6 +289,15 @@ const char* ffOptionsParseDisplayJsonConfig(FFOptionsDisplay* options, yyjson_va
if (borderRightElapsed)
ffStrbufSetS(&options->barBorderRightElapsed, yyjson_get_str(borderRightElapsed));
yyjson_val* colorElapsed = yyjson_obj_get(val, "colorElapsed");
if (colorElapsed) ffOptionParseColor(yyjson_get_str(colorElapsed), &options->barColorElapsed);
yyjson_val* colorTotal = yyjson_obj_get(val, "colorTotal");
if (colorTotal) ffOptionParseColor(yyjson_get_str(colorTotal), &options->barColorTotal);
yyjson_val* colorBorder = yyjson_obj_get(val, "colorBorder");
if (colorBorder) ffOptionParseColor(yyjson_get_str(colorBorder), &options->barColorBorder);
yyjson_val* width = yyjson_obj_get(val, "width");
if (width)
options->barWidth = (uint8_t) yyjson_get_uint(width);
@@ -635,6 +644,12 @@ bool ffOptionsParseDisplayCommandLine(FFOptionsDisplay* options, const char* key
ffOptionParseString(key, value, &options->barBorderLeftElapsed);
else if(ffStrEqualsIgnCase(subkey, "border-right-elapsed"))
ffOptionParseString(key, value, &options->barBorderRightElapsed);
else if(ffStrEqualsIgnCase(subkey, "color-elapsed"))
ffOptionParseColor(value, &options->barColorElapsed);
else if(ffStrEqualsIgnCase(subkey, "color-total"))
ffOptionParseColor(value, &options->barColorTotal);
else if(ffStrEqualsIgnCase(subkey, "color-border"))
ffOptionParseColor(value, &options->barColorBorder);
else
return false;
}
@@ -705,7 +720,11 @@ void ffOptionsInitDisplay(FFOptionsDisplay* options)
ffStrbufInitStatic(&options->barBorderRight, " ]");
ffStrbufInit(&options->barBorderLeftElapsed);
ffStrbufInit(&options->barBorderRightElapsed);
ffStrbufInit(&options->barColorElapsed);
ffStrbufInitStatic(&options->barColorTotal, instance.state.terminalLightTheme ? FF_COLOR_FG_WHITE : FF_COLOR_FG_LIGHT_WHITE);
ffStrbufInitStatic(&options->barColorBorder, instance.state.terminalLightTheme ? FF_COLOR_FG_WHITE : FF_COLOR_FG_LIGHT_WHITE);
options->barWidth = 10;
options->durationAbbreviation = false;
options->durationSpaceBeforeUnit = FF_SPACE_BEFORE_UNIT_DEFAULT;
options->percentType = 9;
+3
View File
@@ -62,6 +62,9 @@ typedef struct FFOptionsDisplay
FFstrbuf barBorderRight;
FFstrbuf barBorderLeftElapsed;
FFstrbuf barBorderRightElapsed;
FFstrbuf barColorElapsed;
FFstrbuf barColorTotal;
FFstrbuf barColorBorder;
uint8_t barWidth;
FFPercentageTypeFlags percentType;
uint8_t percentNdigits;