Skip to content

Commit afe2c85

Browse files
committed
Bar: add configurable elapsed bar borders for percent bars
Fix #1875
1 parent 671b111 commit afe2c85

5 files changed

Lines changed: 61 additions & 5 deletions

File tree

doc/json_schema.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,16 @@
10171017
"description": "Set the string to use at right border",
10181018
"default": " ]"
10191019
},
1020+
"borderLeftElapsed": {
1021+
"type": "string",
1022+
"description": "If both borderLeftElapsed and borderRightElapsed are set, the border will be used as parts of bar content",
1023+
"default": ""
1024+
},
1025+
"borderRightElapsed": {
1026+
"type": "string",
1027+
"description": "If both borderLeftElapsed and borderRightElapsed are set, the border will be used as parts of bar content",
1028+
"default": ""
1029+
},
10201030
"width": {
10211031
"type": "integer",
10221032
"description": "Set the width of the bar, in number of characters",

src/common/percent.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,12 @@ void ffPercentAppendBar(FFstrbuf* buffer, double percent, FFPercentageModuleConf
6060

6161
const FFOptionsDisplay* options = &instance.config.display;
6262

63+
const bool borderAsValue = options->barBorderLeftElapsed.length && options->barBorderRightElapsed.length;
64+
6365
uint32_t blocksPercent = (uint32_t) (percent / 100.0 * options->barWidth + 0.5);
6466
assert(blocksPercent <= options->barWidth);
6567

66-
if(options->barBorderLeft.length)
68+
if(!borderAsValue && options->barBorderLeft.length)
6769
{
6870
if(!options->pipe)
6971
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_WHITE "m");
@@ -76,7 +78,11 @@ void ffPercentAppendBar(FFstrbuf* buffer, double percent, FFPercentageModuleConf
7678
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_BLACK "m");
7779

7880
for (uint32_t i = 0; i < options->barWidth; ++i)
79-
ffStrbufAppend(buffer, &options->barCharElapsed);
81+
ffStrbufAppend(buffer, borderAsValue && i == 0
82+
? &options->barBorderLeft
83+
: borderAsValue && i == options->barWidth - 1
84+
? &options->barBorderRight
85+
: &options->barCharTotal);
8086
}
8187
else
8288
{
@@ -120,19 +126,27 @@ void ffPercentAppendBar(FFstrbuf* buffer, double percent, FFPercentageModuleConf
120126
ffStrbufAppendF(buffer, "\e[%sm", (green <= yellow ? colorGreen : colorRed));
121127
}
122128
}
123-
ffStrbufAppend(buffer, &options->barCharElapsed);
129+
ffStrbufAppend(buffer, borderAsValue && i == 0
130+
? &options->barBorderLeftElapsed
131+
: borderAsValue && i == options->barWidth - 1
132+
? &options->barBorderRightElapsed
133+
: &options->barCharElapsed);
124134
}
125135

126136
if (blocksPercent < options->barWidth)
127137
{
128138
if(!options->pipe)
129139
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_WHITE "m");
130140
for (uint32_t i = blocksPercent; i < options->barWidth; ++i)
131-
ffStrbufAppend(buffer, &options->barCharTotal);
141+
ffStrbufAppend(buffer, borderAsValue && i == 0
142+
? &options->barBorderLeft
143+
: borderAsValue && i == options->barWidth - 1
144+
? &options->barBorderRight
145+
: &options->barCharTotal);
132146
}
133147
}
134148

135-
if(options->barBorderRight.length)
149+
if(!borderAsValue && options->barBorderRight.length)
136150
{
137151
if(!options->pipe)
138152
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_WHITE "m");

src/data/help.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,22 @@
699699
"default": " ]"
700700
}
701701
},
702+
{
703+
"long": "bar-border-left-elapsed",
704+
"desc": "If both bar-border-left-elapsed and bar-border-right-elapsed are set, the border will be used as parts of bar content",
705+
"arg": {
706+
"type": "string",
707+
"default": ""
708+
}
709+
},
710+
{
711+
"long": "bar-border-right-elapsed",
712+
"desc": "If both bar-border-left-elapsed and bar-border-right-elapsed are set, the border will be used as parts of bar content",
713+
"arg": {
714+
"type": "string",
715+
"default": ""
716+
}
717+
},
702718
{
703719
"long": "bar-width",
704720
"desc": "Set the width of percentage bars in characters",

src/options/display.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,14 @@ const char* ffOptionsParseDisplayJsonConfig(FFOptionsDisplay* options, yyjson_va
281281
if (borderRight)
282282
ffStrbufSetS(&options->barBorderRight, yyjson_get_str(borderRight));
283283

284+
yyjson_val* borderLeftElapsed = yyjson_obj_get(val, "borderLeftElapsed");
285+
if (borderLeftElapsed)
286+
ffStrbufSetS(&options->barBorderLeftElapsed, yyjson_get_str(borderLeftElapsed));
287+
288+
yyjson_val* borderRightElapsed = yyjson_obj_get(val, "borderRightElapsed");
289+
if (borderRightElapsed)
290+
ffStrbufSetS(&options->barBorderRightElapsed, yyjson_get_str(borderRightElapsed));
291+
284292
yyjson_val* width = yyjson_obj_get(val, "width");
285293
if (width)
286294
options->barWidth = (uint8_t) yyjson_get_uint(width);
@@ -623,6 +631,10 @@ bool ffOptionsParseDisplayCommandLine(FFOptionsDisplay* options, const char* key
623631
ffOptionParseString(key, value, &options->barBorderLeft);
624632
else if(ffStrEqualsIgnCase(subkey, "border-right"))
625633
ffOptionParseString(key, value, &options->barBorderRight);
634+
else if(ffStrEqualsIgnCase(subkey, "border-left-elapsed"))
635+
ffOptionParseString(key, value, &options->barBorderLeftElapsed);
636+
else if(ffStrEqualsIgnCase(subkey, "border-right-elapsed"))
637+
ffOptionParseString(key, value, &options->barBorderRightElapsed);
626638
else
627639
return false;
628640
}
@@ -691,6 +703,8 @@ void ffOptionsInitDisplay(FFOptionsDisplay* options)
691703
ffStrbufInitStatic(&options->barCharTotal, "-");
692704
ffStrbufInitStatic(&options->barBorderLeft, "[ ");
693705
ffStrbufInitStatic(&options->barBorderRight, " ]");
706+
ffStrbufInit(&options->barBorderLeftElapsed);
707+
ffStrbufInit(&options->barBorderRightElapsed);
694708
options->barWidth = 10;
695709
options->durationAbbreviation = false;
696710
options->durationSpaceBeforeUnit = FF_SPACE_BEFORE_UNIT_DEFAULT;

src/options/display.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ typedef struct FFOptionsDisplay
6060
FFstrbuf barCharTotal;
6161
FFstrbuf barBorderLeft;
6262
FFstrbuf barBorderRight;
63+
FFstrbuf barBorderLeftElapsed;
64+
FFstrbuf barBorderRightElapsed;
6365
uint8_t barWidth;
6466
FFPercentageTypeFlags percentType;
6567
uint8_t percentNdigits;

0 commit comments

Comments
 (0)