Skip to content

Commit c6675f8

Browse files
committed
Bar: add customizable bar color options for percentage bars
1 parent afe2c85 commit c6675f8

5 files changed

Lines changed: 72 additions & 5 deletions

File tree

doc/json_schema.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,20 @@
10271027
"description": "If both borderLeftElapsed and borderRightElapsed are set, the border will be used as parts of bar content",
10281028
"default": ""
10291029
},
1030+
"colorElapsed": {
1031+
"description": "Color to use in the elapsed part of percentage bars",
1032+
"$ref": "#/$defs/colors"
1033+
},
1034+
"colorTotal": {
1035+
"description": "Color to use in the total part of percentage bars",
1036+
"$ref": "#/$defs/colors",
1037+
"default": "light_white"
1038+
},
1039+
"colorBorder": {
1040+
"description": "Color to use in the borders of percentage bars",
1041+
"$ref": "#/$defs/colors",
1042+
"default": "light_white"
1043+
},
10301044
"width": {
10311045
"type": "integer",
10321046
"description": "Set the width of the bar, in number of characters",

src/common/percent.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void ffPercentAppendBar(FFstrbuf* buffer, double percent, FFPercentageModuleConf
6868
if(!borderAsValue && options->barBorderLeft.length)
6969
{
7070
if(!options->pipe)
71-
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_WHITE "m");
71+
ffStrbufAppendF(buffer, "\e[%sm", options->barColorBorder.chars);
7272
ffStrbufAppend(buffer, &options->barBorderLeft);
7373
}
7474

@@ -78,11 +78,13 @@ void ffPercentAppendBar(FFstrbuf* buffer, double percent, FFPercentageModuleConf
7878
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_BLACK "m");
7979

8080
for (uint32_t i = 0; i < options->barWidth; ++i)
81+
{
8182
ffStrbufAppend(buffer, borderAsValue && i == 0
8283
? &options->barBorderLeft
8384
: borderAsValue && i == options->barWidth - 1
8485
? &options->barBorderRight
8586
: &options->barCharTotal);
87+
}
8688
}
8789
else
8890
{
@@ -91,7 +93,7 @@ void ffPercentAppendBar(FFstrbuf* buffer, double percent, FFPercentageModuleConf
9193
const char* colorRed = options->percentColorRed.chars;
9294

9395
FFPercentageTypeFlags percentType = config.type == 0 ? options->percentType : config.type;
94-
bool monochrome = !!(percentType & FF_PERCENTAGE_TYPE_BAR_MONOCHROME_BIT);
96+
bool monochrome = !!(percentType & FF_PERCENTAGE_TYPE_BAR_MONOCHROME_BIT) || options->barColorElapsed.length;
9597

9698
for (uint32_t i = 0; i < blocksPercent; ++i)
9799
{
@@ -100,7 +102,9 @@ void ffPercentAppendBar(FFstrbuf* buffer, double percent, FFPercentageModuleConf
100102
if (monochrome)
101103
{
102104
const char* color = NULL;
103-
if (green <= yellow)
105+
if (options->barColorElapsed.length)
106+
color = options->barColorElapsed.chars;
107+
else if (green <= yellow)
104108
{
105109
if (percent < green) color = colorGreen;
106110
else if (percent < yellow) color = colorYellow;
@@ -136,20 +140,22 @@ void ffPercentAppendBar(FFstrbuf* buffer, double percent, FFPercentageModuleConf
136140
if (blocksPercent < options->barWidth)
137141
{
138142
if(!options->pipe)
139-
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_WHITE "m");
143+
ffStrbufAppendF(buffer, "\e[%sm", options->barColorTotal.chars);
140144
for (uint32_t i = blocksPercent; i < options->barWidth; ++i)
145+
{
141146
ffStrbufAppend(buffer, borderAsValue && i == 0
142147
? &options->barBorderLeft
143148
: borderAsValue && i == options->barWidth - 1
144149
? &options->barBorderRight
145150
: &options->barCharTotal);
151+
}
146152
}
147153
}
148154

149155
if(!borderAsValue && options->barBorderRight.length)
150156
{
151157
if(!options->pipe)
152-
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_WHITE "m");
158+
ffStrbufAppendF(buffer, "\e[%sm", options->barColorBorder.chars);
153159
ffStrbufAppend(buffer, &options->barBorderRight);
154160
}
155161

src/data/help.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,31 @@
715715
"default": ""
716716
}
717717
},
718+
{
719+
"long": "bar-color-elapsed",
720+
"desc": "Set the color to use in the elapsed part of percentage bars",
721+
"remark": "By default, auto selected by percent-color-{green,yellow,red}",
722+
"arg": {
723+
"type": "color",
724+
"default": "<auto>"
725+
}
726+
},
727+
{
728+
"long": "bar-color-total",
729+
"desc": "Set the color to use in the total part of percentage bars",
730+
"arg": {
731+
"type": "color",
732+
"default": "light_white"
733+
}
734+
},
735+
{
736+
"long": "bar-color-border",
737+
"desc": "Set the color to use in the borders of percentage bars",
738+
"arg": {
739+
"type": "color",
740+
"default": "light_white"
741+
}
742+
},
718743
{
719744
"long": "bar-width",
720745
"desc": "Set the width of percentage bars in characters",

src/options/display.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,15 @@ const char* ffOptionsParseDisplayJsonConfig(FFOptionsDisplay* options, yyjson_va
289289
if (borderRightElapsed)
290290
ffStrbufSetS(&options->barBorderRightElapsed, yyjson_get_str(borderRightElapsed));
291291

292+
yyjson_val* colorElapsed = yyjson_obj_get(val, "colorElapsed");
293+
if (colorElapsed) ffOptionParseColor(yyjson_get_str(colorElapsed), &options->barColorElapsed);
294+
295+
yyjson_val* colorTotal = yyjson_obj_get(val, "colorTotal");
296+
if (colorTotal) ffOptionParseColor(yyjson_get_str(colorTotal), &options->barColorTotal);
297+
298+
yyjson_val* colorBorder = yyjson_obj_get(val, "colorBorder");
299+
if (colorBorder) ffOptionParseColor(yyjson_get_str(colorBorder), &options->barColorBorder);
300+
292301
yyjson_val* width = yyjson_obj_get(val, "width");
293302
if (width)
294303
options->barWidth = (uint8_t) yyjson_get_uint(width);
@@ -635,6 +644,12 @@ bool ffOptionsParseDisplayCommandLine(FFOptionsDisplay* options, const char* key
635644
ffOptionParseString(key, value, &options->barBorderLeftElapsed);
636645
else if(ffStrEqualsIgnCase(subkey, "border-right-elapsed"))
637646
ffOptionParseString(key, value, &options->barBorderRightElapsed);
647+
else if(ffStrEqualsIgnCase(subkey, "color-elapsed"))
648+
ffOptionParseColor(value, &options->barColorElapsed);
649+
else if(ffStrEqualsIgnCase(subkey, "color-total"))
650+
ffOptionParseColor(value, &options->barColorTotal);
651+
else if(ffStrEqualsIgnCase(subkey, "color-border"))
652+
ffOptionParseColor(value, &options->barColorBorder);
638653
else
639654
return false;
640655
}
@@ -705,7 +720,11 @@ void ffOptionsInitDisplay(FFOptionsDisplay* options)
705720
ffStrbufInitStatic(&options->barBorderRight, " ]");
706721
ffStrbufInit(&options->barBorderLeftElapsed);
707722
ffStrbufInit(&options->barBorderRightElapsed);
723+
ffStrbufInit(&options->barColorElapsed);
724+
ffStrbufInitStatic(&options->barColorTotal, instance.state.terminalLightTheme ? FF_COLOR_FG_WHITE : FF_COLOR_FG_LIGHT_WHITE);
725+
ffStrbufInitStatic(&options->barColorBorder, instance.state.terminalLightTheme ? FF_COLOR_FG_WHITE : FF_COLOR_FG_LIGHT_WHITE);
708726
options->barWidth = 10;
727+
709728
options->durationAbbreviation = false;
710729
options->durationSpaceBeforeUnit = FF_SPACE_BEFORE_UNIT_DEFAULT;
711730
options->percentType = 9;

src/options/display.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ typedef struct FFOptionsDisplay
6262
FFstrbuf barBorderRight;
6363
FFstrbuf barBorderLeftElapsed;
6464
FFstrbuf barBorderRightElapsed;
65+
FFstrbuf barColorElapsed;
66+
FFstrbuf barColorTotal;
67+
FFstrbuf barColorBorder;
6568
uint8_t barWidth;
6669
FFPercentageTypeFlags percentType;
6770
uint8_t percentNdigits;

0 commit comments

Comments
 (0)