Bar: add configurable elapsed bar borders for percent bars

Fix #1875
This commit is contained in:
李通洲
2025-08-01 10:51:26 +08:00
parent 671b11157e
commit afe2c85047
5 changed files with 61 additions and 5 deletions
+10
View File
@@ -1017,6 +1017,16 @@
"description": "Set the string to use at right border",
"default": " ]"
},
"borderLeftElapsed": {
"type": "string",
"description": "If both borderLeftElapsed and borderRightElapsed are set, the border will be used as parts of bar content",
"default": ""
},
"borderRightElapsed": {
"type": "string",
"description": "If both borderLeftElapsed and borderRightElapsed are set, the border will be used as parts of bar content",
"default": ""
},
"width": {
"type": "integer",
"description": "Set the width of the bar, in number of characters",
+19 -5
View File
@@ -60,10 +60,12 @@ void ffPercentAppendBar(FFstrbuf* buffer, double percent, FFPercentageModuleConf
const FFOptionsDisplay* options = &instance.config.display;
const bool borderAsValue = options->barBorderLeftElapsed.length && options->barBorderRightElapsed.length;
uint32_t blocksPercent = (uint32_t) (percent / 100.0 * options->barWidth + 0.5);
assert(blocksPercent <= options->barWidth);
if(options->barBorderLeft.length)
if(!borderAsValue && options->barBorderLeft.length)
{
if(!options->pipe)
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_WHITE "m");
@@ -76,7 +78,11 @@ 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, &options->barCharElapsed);
ffStrbufAppend(buffer, borderAsValue && i == 0
? &options->barBorderLeft
: borderAsValue && i == options->barWidth - 1
? &options->barBorderRight
: &options->barCharTotal);
}
else
{
@@ -120,7 +126,11 @@ void ffPercentAppendBar(FFstrbuf* buffer, double percent, FFPercentageModuleConf
ffStrbufAppendF(buffer, "\e[%sm", (green <= yellow ? colorGreen : colorRed));
}
}
ffStrbufAppend(buffer, &options->barCharElapsed);
ffStrbufAppend(buffer, borderAsValue && i == 0
? &options->barBorderLeftElapsed
: borderAsValue && i == options->barWidth - 1
? &options->barBorderRightElapsed
: &options->barCharElapsed);
}
if (blocksPercent < options->barWidth)
@@ -128,11 +138,15 @@ void ffPercentAppendBar(FFstrbuf* buffer, double percent, FFPercentageModuleConf
if(!options->pipe)
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_WHITE "m");
for (uint32_t i = blocksPercent; i < options->barWidth; ++i)
ffStrbufAppend(buffer, &options->barCharTotal);
ffStrbufAppend(buffer, borderAsValue && i == 0
? &options->barBorderLeft
: borderAsValue && i == options->barWidth - 1
? &options->barBorderRight
: &options->barCharTotal);
}
}
if(options->barBorderRight.length)
if(!borderAsValue && options->barBorderRight.length)
{
if(!options->pipe)
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_WHITE "m");
+16
View File
@@ -699,6 +699,22 @@
"default": " ]"
}
},
{
"long": "bar-border-left-elapsed",
"desc": "If both bar-border-left-elapsed and bar-border-right-elapsed are set, the border will be used as parts of bar content",
"arg": {
"type": "string",
"default": ""
}
},
{
"long": "bar-border-right-elapsed",
"desc": "If both bar-border-left-elapsed and bar-border-right-elapsed are set, the border will be used as parts of bar content",
"arg": {
"type": "string",
"default": ""
}
},
{
"long": "bar-width",
"desc": "Set the width of percentage bars in characters",
+14
View File
@@ -281,6 +281,14 @@ const char* ffOptionsParseDisplayJsonConfig(FFOptionsDisplay* options, yyjson_va
if (borderRight)
ffStrbufSetS(&options->barBorderRight, yyjson_get_str(borderRight));
yyjson_val* borderLeftElapsed = yyjson_obj_get(val, "borderLeftElapsed");
if (borderLeftElapsed)
ffStrbufSetS(&options->barBorderLeftElapsed, yyjson_get_str(borderLeftElapsed));
yyjson_val* borderRightElapsed = yyjson_obj_get(val, "borderRightElapsed");
if (borderRightElapsed)
ffStrbufSetS(&options->barBorderRightElapsed, yyjson_get_str(borderRightElapsed));
yyjson_val* width = yyjson_obj_get(val, "width");
if (width)
options->barWidth = (uint8_t) yyjson_get_uint(width);
@@ -623,6 +631,10 @@ bool ffOptionsParseDisplayCommandLine(FFOptionsDisplay* options, const char* key
ffOptionParseString(key, value, &options->barBorderLeft);
else if(ffStrEqualsIgnCase(subkey, "border-right"))
ffOptionParseString(key, value, &options->barBorderRight);
else if(ffStrEqualsIgnCase(subkey, "border-left-elapsed"))
ffOptionParseString(key, value, &options->barBorderLeftElapsed);
else if(ffStrEqualsIgnCase(subkey, "border-right-elapsed"))
ffOptionParseString(key, value, &options->barBorderRightElapsed);
else
return false;
}
@@ -691,6 +703,8 @@ void ffOptionsInitDisplay(FFOptionsDisplay* options)
ffStrbufInitStatic(&options->barCharTotal, "-");
ffStrbufInitStatic(&options->barBorderLeft, "[ ");
ffStrbufInitStatic(&options->barBorderRight, " ]");
ffStrbufInit(&options->barBorderLeftElapsed);
ffStrbufInit(&options->barBorderRightElapsed);
options->barWidth = 10;
options->durationAbbreviation = false;
options->durationSpaceBeforeUnit = FF_SPACE_BEFORE_UNIT_DEFAULT;
+2
View File
@@ -60,6 +60,8 @@ typedef struct FFOptionsDisplay
FFstrbuf barCharTotal;
FFstrbuf barBorderLeft;
FFstrbuf barBorderRight;
FFstrbuf barBorderLeftElapsed;
FFstrbuf barBorderRightElapsed;
uint8_t barWidth;
FFPercentageTypeFlags percentType;
uint8_t percentNdigits;