Skip to content

Commit 81bac8b

Browse files
committed
Percent: refactors bar color logic for clarity and consistency; update examples
1 parent 56d2229 commit 81bac8b

File tree

3 files changed

+45
-39
lines changed

3 files changed

+45
-39
lines changed

doc/json_schema.json

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,19 @@
22
"$schema": "https://json-schema.org/draft-07/schema",
33
"$defs": {
44
"colors": {
5-
"type": "string",
6-
"description": "https://github.com/fastfetch-cli/fastfetch/wiki/Color-Format-Specification",
7-
"examples": [
8-
"reset_", "bright_", "dim_", "italic_", "underline_", "blink_", "inverse_", "hidden_", "strike_", "light_",
9-
"black", "red", "green", "yellow", "blue", "magenta", "cyan", "white", "default"
5+
"oneOf": [
6+
{
7+
"type": "string",
8+
"$comment": "https://github.com/fastfetch-cli/fastfetch/wiki/Color-Format-Specification",
9+
"examples": [
10+
"reset_", "bright_", "dim_", "italic_", "underline_", "blink_", "inverse_", "hidden_", "strike_", "light_",
11+
"black", "red", "green", "yellow", "blue", "magenta", "cyan", "white", "default"
12+
]
13+
},
14+
{
15+
"type": "null",
16+
"$comment": "Disable default color"
17+
}
1018
]
1119
},
1220
"key": {

presets/examples/29.jsonc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
"bar": {
1616
"border": {
1717
"left": "[",
18-
"leftElapsed": "[",
18+
"leftElapsed": "[",
1919
"right": "]",
20-
"rightElapsed": "]"
20+
"rightElapsed": "]"
2121
},
2222
"char": {
2323
"elapsed": "",
@@ -27,7 +27,7 @@
2727
"elapsed": "default",
2828
"total": "light_black"
2929
},
30-
"width": 14
30+
"width": 16
3131
},
3232
"color": {
3333
"separator": "default",

src/common/percent.c

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -97,40 +97,38 @@ void ffPercentAppendBar(FFstrbuf* buffer, double percent, FFPercentageModuleConf
9797

9898
bool autoColorElapsed = ffStrbufIgnCaseEqualS(&options->barColorElapsed, "auto");
9999

100+
bool monochrome = (percentType & FF_PERCENTAGE_TYPE_BAR_MONOCHROME_BIT) || !autoColorElapsed;
101+
if (!options->pipe && options->barColorElapsed.length > 0 && monochrome)
102+
{
103+
const char* color = NULL;
104+
if (!autoColorElapsed)
105+
color = options->barColorElapsed.chars;
106+
else if (green <= yellow)
107+
{
108+
if (percent < green) color = colorGreen;
109+
else if (percent < yellow) color = colorYellow;
110+
else color = colorRed;
111+
}
112+
else
113+
{
114+
if (percent < yellow) color = colorRed;
115+
else if (percent < green) color = colorYellow;
116+
else color = colorGreen;
117+
}
118+
ffStrbufAppendF(buffer, "\e[%sm", color);
119+
}
100120
for (uint8_t i = 0; i < blocksPercent; ++i)
101121
{
102-
if(!options->pipe && options->barColorElapsed.length > 0)
122+
if (!options->pipe && options->barColorElapsed.length > 0 && !monochrome)
103123
{
104-
if ((percentType & FF_PERCENTAGE_TYPE_BAR_MONOCHROME_BIT) || !autoColorElapsed)
105-
{
106-
const char* color = NULL;
107-
if (!autoColorElapsed)
108-
color = options->barColorElapsed.chars;
109-
else if (green <= yellow)
110-
{
111-
if (percent < green) color = colorGreen;
112-
else if (percent < yellow) color = colorYellow;
113-
else color = colorRed;
114-
}
115-
else
116-
{
117-
if (percent < yellow) color = colorRed;
118-
else if (percent < green) color = colorYellow;
119-
else color = colorGreen;
120-
}
121-
ffStrbufAppendF(buffer, "\e[%sm", color);
122-
}
123-
else
124-
{
125-
uint32_t section1Begin = (uint32_t) ((green <= yellow ? green : yellow) / 100.0 * options->barWidth + 0.5);
126-
uint32_t section2Begin = (uint32_t) ((green > yellow ? green : yellow) / 100.0 * options->barWidth + 0.5);
127-
if (i == section2Begin)
128-
ffStrbufAppendF(buffer, "\e[%sm", (green > yellow ? colorGreen : colorRed));
129-
else if (i == section1Begin)
130-
ffStrbufAppendF(buffer, "\e[%sm", colorYellow);
131-
else if (i == 0)
132-
ffStrbufAppendF(buffer, "\e[%sm", (green <= yellow ? colorGreen : colorRed));
133-
}
124+
uint32_t section1Begin = (uint32_t) ((green <= yellow ? green : yellow) / 100.0 * options->barWidth + 0.5);
125+
uint32_t section2Begin = (uint32_t) ((green > yellow ? green : yellow) / 100.0 * options->barWidth + 0.5);
126+
if (i == section2Begin)
127+
ffStrbufAppendF(buffer, "\e[%sm", (green > yellow ? colorGreen : colorRed));
128+
else if (i == section1Begin)
129+
ffStrbufAppendF(buffer, "\e[%sm", colorYellow);
130+
else if (i == 0)
131+
ffStrbufAppendF(buffer, "\e[%sm", (green <= yellow ? colorGreen : colorRed));
134132
}
135133
ffStrbufAppend(buffer, borderAsValue && i == 0
136134
? &options->barBorderLeftElapsed

0 commit comments

Comments
 (0)