Skip to content

Commit 54c5bc5

Browse files
committed
Bar: refactor config to use nested objects and deprecates old fields
1 parent c6675f8 commit 54c5bc5

5 files changed

Lines changed: 186 additions & 69 deletions

File tree

doc/json_schema.json

Lines changed: 86 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -997,50 +997,108 @@
997997
"additionalProperties": false,
998998
"description": "Set the bar configuration",
999999
"properties": {
1000+
"char": {
1001+
"type": "object",
1002+
"additionalProperties": false,
1003+
"description": "Set the characters used in the bar",
1004+
"properties": {
1005+
"elapsed": {
1006+
"type": "string",
1007+
"description": "Set the character to use in elapsed part",
1008+
"default": ""
1009+
},
1010+
"total": {
1011+
"type": "string",
1012+
"description": "Set the character to use in total part",
1013+
"default": "-"
1014+
}
1015+
}
1016+
},
1017+
"border": {
1018+
"oneOf": [
1019+
{
1020+
"type": "null",
1021+
"description": "Disable bar borders"
1022+
},
1023+
{
1024+
"type": "object",
1025+
"additionalProperties": false,
1026+
"description": "Set the string to use of borders of percentage bars",
1027+
"properties": {
1028+
"left": {
1029+
"type": "string",
1030+
"description": "Set the string to use at left border",
1031+
"default": "[ "
1032+
},
1033+
"right": {
1034+
"type": "string",
1035+
"description": "Set the string to use at right border",
1036+
"default": " ]"
1037+
},
1038+
"leftElapsed": {
1039+
"type": "string",
1040+
"description": "If both leftElapsed and rightElapsed are set, the border will be used as parts of bar content",
1041+
"default": ""
1042+
},
1043+
"rightElapsed": {
1044+
"type": "string",
1045+
"description": "If both leftElapsed and rightElapsed are set, the border will be used as parts of bar content",
1046+
"default": ""
1047+
}
1048+
}
1049+
}
1050+
]
1051+
},
1052+
"color": {
1053+
"oneOf": [
1054+
{
1055+
"type": "null",
1056+
"description": "Disable color in percentage bars"
1057+
},
1058+
{
1059+
"type": "object",
1060+
"additionalProperties": false,
1061+
"description": "Set the color to use of percentage bars",
1062+
"properties": {
1063+
"elapsed": {
1064+
"description": "Color to use in the elapsed part of percentage bars\nBy default, auto selected by percent.color.{green,yellow,red}",
1065+
"$ref": "#/$defs/colors",
1066+
"default": "auto"
1067+
},
1068+
"total": {
1069+
"description": "Color to use in the total part of percentage bars",
1070+
"$ref": "#/$defs/colors",
1071+
"default": "light_white"
1072+
},
1073+
"border": {
1074+
"description": "Color to use in the borders of percentage bars",
1075+
"$ref": "#/$defs/colors",
1076+
"default": "light_white"
1077+
}
1078+
}
1079+
}
1080+
]
1081+
},
10001082
"charElapsed": {
10011083
"type": "string",
1002-
"description": "Set the character to use in elapsed part",
1084+
"description": "Set the character to use in elapsed part. Deprecated in v2.50.0",
10031085
"default": ""
10041086
},
10051087
"charTotal": {
10061088
"type": "string",
1007-
"description": "Set the character to use in total part",
1089+
"description": "Set the character to use in total part. Deprecated in v2.50.0",
10081090
"default": "-"
10091091
},
10101092
"borderLeft": {
10111093
"type": "string",
1012-
"description": "Set the string to use at left border",
1094+
"description": "Set the string to use at left border. Deprecated in v2.50.0",
10131095
"default": "[ "
10141096
},
10151097
"borderRight": {
10161098
"type": "string",
1017-
"description": "Set the string to use at right border",
1099+
"description": "Set the string to use at right border. Deprecated in v2.50.0",
10181100
"default": " ]"
10191101
},
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-
},
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-
},
10441102
"width": {
10451103
"type": "integer",
10461104
"description": "Set the width of the bar, in number of characters",

src/common/option.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ bool ffOptionParseBoolean(const char* str)
157157

158158
void ffOptionParseColorNoClear(const char* value, FFstrbuf* buffer)
159159
{
160+
if (value[0] == '\0') return;
161+
160162
// If value is already an ANSI escape code, use it
161163
if (value[0] == '\e' && value[1] == '[')
162164
{

src/common/percent.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,15 @@ void ffPercentAppendBar(FFstrbuf* buffer, double percent, FFPercentageModuleConf
6767

6868
if(!borderAsValue && options->barBorderLeft.length)
6969
{
70-
if(!options->pipe)
70+
if(!options->pipe && options->barColorBorder.length > 0)
7171
ffStrbufAppendF(buffer, "\e[%sm", options->barColorBorder.chars);
7272
ffStrbufAppend(buffer, &options->barBorderLeft);
7373
}
7474

7575
if (percent != percent)
7676
{
77-
if(!options->pipe)
77+
// No color config for NaN, use total color for simplification
78+
if(!options->pipe && options->barColorTotal.length > 0)
7879
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_BLACK "m");
7980

8081
for (uint32_t i = 0; i < options->barWidth; ++i)
@@ -93,16 +94,17 @@ void ffPercentAppendBar(FFstrbuf* buffer, double percent, FFPercentageModuleConf
9394
const char* colorRed = options->percentColorRed.chars;
9495

9596
FFPercentageTypeFlags percentType = config.type == 0 ? options->percentType : config.type;
96-
bool monochrome = !!(percentType & FF_PERCENTAGE_TYPE_BAR_MONOCHROME_BIT) || options->barColorElapsed.length;
97+
98+
bool autoColorElapsed = ffStrbufIgnCaseEqualS(&options->barColorElapsed, "auto");
9799

98100
for (uint32_t i = 0; i < blocksPercent; ++i)
99101
{
100-
if(!options->pipe)
102+
if(!options->pipe && options->barColorElapsed.length > 0)
101103
{
102-
if (monochrome)
104+
if ((percentType & FF_PERCENTAGE_TYPE_BAR_MONOCHROME_BIT) || !autoColorElapsed)
103105
{
104106
const char* color = NULL;
105-
if (options->barColorElapsed.length)
107+
if (!autoColorElapsed)
106108
color = options->barColorElapsed.chars;
107109
else if (green <= yellow)
108110
{
@@ -139,7 +141,7 @@ void ffPercentAppendBar(FFstrbuf* buffer, double percent, FFPercentageModuleConf
139141

140142
if (blocksPercent < options->barWidth)
141143
{
142-
if(!options->pipe)
144+
if(!options->pipe && options->barColorTotal.length > 0)
143145
ffStrbufAppendF(buffer, "\e[%sm", options->barColorTotal.chars);
144146
for (uint32_t i = blocksPercent; i < options->barWidth; ++i)
145147
{
@@ -154,12 +156,12 @@ void ffPercentAppendBar(FFstrbuf* buffer, double percent, FFPercentageModuleConf
154156

155157
if(!borderAsValue && options->barBorderRight.length)
156158
{
157-
if(!options->pipe)
159+
if(!options->pipe && options->barColorBorder.length > 0)
158160
ffStrbufAppendF(buffer, "\e[%sm", options->barColorBorder.chars);
159161
ffStrbufAppend(buffer, &options->barBorderRight);
160162
}
161163

162-
if(!options->pipe)
164+
if(!options->pipe && (options->barColorElapsed.length > 0 || options->barColorTotal.length > 0 || options->barColorBorder.length > 0))
163165
{
164166
ffStrbufAppendS(buffer, FASTFETCH_TEXT_MODIFIER_RESET);
165167
appendOutputColor(buffer, module);

src/options/display.c

Lines changed: 84 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ const char* ffOptionsParseDisplayJsonConfig(FFOptionsDisplay* options, yyjson_va
7676
}
7777
else if (ffStrEqualsIgnCase(key, "brightColor"))
7878
options->brightColor = yyjson_get_bool(val);
79-
else if (ffStrEqualsIgnCase(key, "binaryPrefix"))
80-
return "`display.binaryPrefix` has been renamed to `display.size.binaryPrefix`. Sorry for another break change.";
8179
else if (ffStrEqualsIgnCase(key, "duration"))
8280
{
8381
if (!yyjson_is_obj(val))
@@ -265,38 +263,95 @@ const char* ffOptionsParseDisplayJsonConfig(FFOptionsDisplay* options, yyjson_va
265263
{
266264
if (yyjson_is_obj(val))
267265
{
268-
const char* charElapsed = yyjson_get_str(yyjson_obj_get(val, "charElapsed"));
269-
if (charElapsed)
270-
ffStrbufSetS(&options->barCharElapsed, charElapsed);
271-
272-
const char* charTotal = yyjson_get_str(yyjson_obj_get(val, "charTotal"));
273-
if (charTotal)
274-
ffStrbufSetS(&options->barCharTotal, charTotal);
275-
276-
yyjson_val* borderLeft = yyjson_obj_get(val, "borderLeft");
277-
if (borderLeft)
278-
ffStrbufSetS(&options->barBorderLeft, yyjson_get_str(borderLeft));
266+
yyjson_val* char_ = yyjson_obj_get(val, "char");
267+
if (char_)
268+
{
269+
if (!yyjson_is_obj(char_)) return "display.bar.char must be an object";
279270

280-
yyjson_val* borderRight = yyjson_obj_get(val, "borderRight");
281-
if (borderRight)
282-
ffStrbufSetS(&options->barBorderRight, yyjson_get_str(borderRight));
271+
const char* charElapsed = yyjson_get_str(yyjson_obj_get(char_, "elapsed"));
272+
if (charElapsed)
273+
ffStrbufSetS(&options->barCharElapsed, charElapsed);
283274

284-
yyjson_val* borderLeftElapsed = yyjson_obj_get(val, "borderLeftElapsed");
285-
if (borderLeftElapsed)
286-
ffStrbufSetS(&options->barBorderLeftElapsed, yyjson_get_str(borderLeftElapsed));
275+
const char* charTotal = yyjson_get_str(yyjson_obj_get(char_, "total"));
276+
if (charTotal)
277+
ffStrbufSetS(&options->barCharTotal, charTotal);
278+
}
279+
else
280+
{
281+
const char* charElapsed = yyjson_get_str(yyjson_obj_get(val, "charElapsed"));
282+
if (charElapsed)
283+
return "display.bar.charElapsed has been renamed to display.bar.char.elapsed.";
287284

288-
yyjson_val* borderRightElapsed = yyjson_obj_get(val, "borderRightElapsed");
289-
if (borderRightElapsed)
290-
ffStrbufSetS(&options->barBorderRightElapsed, yyjson_get_str(borderRightElapsed));
285+
const char* charTotal = yyjson_get_str(yyjson_obj_get(val, "charTotal"));
286+
if (charTotal)
287+
return "display.bar.charTotal has been renamed to display.bar.char.total.";
288+
}
291289

292-
yyjson_val* colorElapsed = yyjson_obj_get(val, "colorElapsed");
293-
if (colorElapsed) ffOptionParseColor(yyjson_get_str(colorElapsed), &options->barColorElapsed);
290+
yyjson_val* border = yyjson_obj_get(val, "border");
291+
if (border)
292+
{
293+
if (yyjson_is_null(border))
294+
{
295+
ffStrbufClear(&options->barBorderLeft);
296+
ffStrbufClear(&options->barBorderRight);
297+
ffStrbufClear(&options->barBorderLeftElapsed);
298+
ffStrbufClear(&options->barBorderRightElapsed);
299+
}
300+
else
301+
{
302+
if (!yyjson_is_obj(border)) return "display.bar.border must be an object";
303+
304+
yyjson_val* borderLeft = yyjson_obj_get(border, "left");
305+
if (borderLeft)
306+
ffStrbufSetS(&options->barBorderLeft, yyjson_get_str(borderLeft));
307+
308+
yyjson_val* borderRight = yyjson_obj_get(border, "right");
309+
if (borderRight)
310+
ffStrbufSetS(&options->barBorderRight, yyjson_get_str(borderRight));
311+
312+
yyjson_val* borderLeftElapsed = yyjson_obj_get(border, "leftElapsed");
313+
if (borderLeftElapsed)
314+
ffStrbufSetS(&options->barBorderLeftElapsed, yyjson_get_str(borderLeftElapsed));
315+
316+
yyjson_val* borderRightElapsed = yyjson_obj_get(border, "rightElapsed");
317+
if (borderRightElapsed)
318+
ffStrbufSetS(&options->barBorderRightElapsed, yyjson_get_str(borderRightElapsed));
319+
}
320+
}
321+
else
322+
{
323+
yyjson_val* borderLeft = yyjson_obj_get(val, "borderLeft");
324+
if (borderLeft)
325+
return "display.bar.borderLeft has been renamed to display.bar.border.left.";
294326

295-
yyjson_val* colorTotal = yyjson_obj_get(val, "colorTotal");
296-
if (colorTotal) ffOptionParseColor(yyjson_get_str(colorTotal), &options->barColorTotal);
327+
yyjson_val* borderRight = yyjson_obj_get(val, "borderRight");
328+
if (borderRight)
329+
return "display.bar.borderRight has been renamed to display.bar.border.right.";
330+
}
297331

298-
yyjson_val* colorBorder = yyjson_obj_get(val, "colorBorder");
299-
if (colorBorder) ffOptionParseColor(yyjson_get_str(colorBorder), &options->barColorBorder);
332+
yyjson_val* color = yyjson_obj_get(val, "color");
333+
if (color)
334+
{
335+
if (yyjson_is_null(color))
336+
{
337+
ffStrbufClear(&options->barColorElapsed);
338+
ffStrbufClear(&options->barColorTotal);
339+
ffStrbufClear(&options->barColorBorder);
340+
}
341+
else
342+
{
343+
if (!yyjson_is_obj(color)) return "display.bar.color must be an object";
344+
345+
yyjson_val* colorElapsed = yyjson_obj_get(color, "elapsed");
346+
if (colorElapsed) ffOptionParseColor(yyjson_get_str(colorElapsed), &options->barColorElapsed);
347+
348+
yyjson_val* colorTotal = yyjson_obj_get(color, "total");
349+
if (colorTotal) ffOptionParseColor(yyjson_get_str(colorTotal), &options->barColorTotal);
350+
351+
yyjson_val* colorBorder = yyjson_obj_get(color, "border");
352+
if (colorBorder) ffOptionParseColor(yyjson_get_str(colorBorder), &options->barColorBorder);
353+
}
354+
}
300355

301356
yyjson_val* width = yyjson_obj_get(val, "width");
302357
if (width)
@@ -720,7 +775,7 @@ void ffOptionsInitDisplay(FFOptionsDisplay* options)
720775
ffStrbufInitStatic(&options->barBorderRight, " ]");
721776
ffStrbufInit(&options->barBorderLeftElapsed);
722777
ffStrbufInit(&options->barBorderRightElapsed);
723-
ffStrbufInit(&options->barColorElapsed);
778+
ffStrbufInitStatic(&options->barColorElapsed, "auto");
724779
ffStrbufInitStatic(&options->barColorTotal, instance.state.terminalLightTheme ? FF_COLOR_FG_WHITE : FF_COLOR_FG_LIGHT_WHITE);
725780
ffStrbufInitStatic(&options->barColorBorder, instance.state.terminalLightTheme ? FF_COLOR_FG_WHITE : FF_COLOR_FG_LIGHT_WHITE);
726781
options->barWidth = 10;

src/options/display.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ typedef struct FFOptionsDisplay
6262
FFstrbuf barBorderRight;
6363
FFstrbuf barBorderLeftElapsed;
6464
FFstrbuf barBorderRightElapsed;
65-
FFstrbuf barColorElapsed;
66-
FFstrbuf barColorTotal;
67-
FFstrbuf barColorBorder;
65+
FFstrbuf barColorElapsed; // "auto" for auto selection from percent config; empty for no custom color (inherits)
66+
FFstrbuf barColorTotal; // empty for no custom color (inherits)
67+
FFstrbuf barColorBorder; // empty for no custom color (inherits)
6868
uint8_t barWidth;
6969
FFPercentageTypeFlags percentType;
7070
uint8_t percentNdigits;

0 commit comments

Comments
 (0)