Skip to content

Commit f188e95

Browse files
committed
Colors: add brightness option for color display configuration
Fixes #2238
1 parent a39419b commit f188e95

3 files changed

Lines changed: 94 additions & 31 deletions

File tree

doc/json_schema.json

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2044,7 +2044,7 @@
20442044
"default": 0
20452045
},
20462046
"block": {
2047-
"description": "Set behavior of block printing",
2047+
"description": "Set behavior of block printing. Only works when symbol is set to `block` or `background`",
20482048
"type": "object",
20492049
"additionalProperties": false,
20502050
"properties": {
@@ -2067,6 +2067,25 @@
20672067
}
20682068
}
20692069
},
2070+
"brightness": {
2071+
"description": "Set the brightness of colors. Works together with the block color range to determine which colors are displayed",
2072+
"type": "string",
2073+
"oneOf": [
2074+
{
2075+
"const": "default",
2076+
"description": "Use the default setting (block/background: 0-15, others: 8-1)"
2077+
},
2078+
{
2079+
"const": "light",
2080+
"description": "Use ANSI bright foreground colors (codes 90-97, not color indices 9-15)"
2081+
},
2082+
{
2083+
"const": "normal",
2084+
"description": "Use normal ANSI colors (30-37; standard foreground colors, corresponding to color indices 0-7)"
2085+
}
2086+
],
2087+
"default": "default"
2088+
},
20702089
"key": {
20712090
"$ref": "#/$defs/key"
20722091
},

src/modules/colors/colors.c

Lines changed: 66 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "common/printing.h"
22
#include "common/jsonconfig.h"
33
#include "common/textModifier.h"
4+
#include "common/color.h"
45
#include "common/stringUtils.h"
56
#include "logo/logo.h"
67
#include "modules/colors/colors.h"
@@ -23,20 +24,23 @@ bool ffPrintColors(FFColorsOptions* options)
2324

2425
if (options->symbol == FF_COLORS_SYMBOL_BLOCK || options->symbol == FF_COLORS_SYMBOL_BACKGROUND)
2526
{
26-
// 3%d: Set the foreground color
27-
for(uint8_t i = options->block.range[0]; i <= min(options->block.range[1], 7); i++)
27+
if (options->brightness != FF_COLORS_BRIGHTNESS_LIGHT)
2828
{
29-
if (options->symbol == FF_COLORS_SYMBOL_BLOCK)
29+
// 3%d: Set the foreground color
30+
for(uint8_t i = options->block.range[0]; i <= min(options->block.range[1], 7); i++)
3031
{
31-
if (!instance.config.display.pipe)
32-
ffStrbufAppendF(&result, "\e[3%dm", i);
33-
for (uint8_t j = 0; j < options->block.width; j++)
34-
ffStrbufAppendS(&result, "█");
35-
}
36-
else
37-
{
38-
ffStrbufAppendF(&result, "\e[4%dm", i);
39-
ffStrbufAppendNC(&result, options->block.width, ' ');
32+
if (options->symbol == FF_COLORS_SYMBOL_BLOCK)
33+
{
34+
if (!instance.config.display.pipe)
35+
ffStrbufAppendF(&result, "\e[3%dm", i);
36+
for (uint8_t j = 0; j < options->block.width; j++)
37+
ffStrbufAppendS(&result, "█");
38+
}
39+
else
40+
{
41+
ffStrbufAppendF(&result, "\e[4%dm", i);
42+
ffStrbufAppendNC(&result, options->block.width, ' ');
43+
}
4044
}
4145
}
4246
if (result.length > 0)
@@ -64,20 +68,23 @@ bool ffPrintColors(FFColorsOptions* options)
6468
}
6569
#endif
6670

67-
// 9%d: Set the foreground to the bright color
68-
for(uint8_t i = max(options->block.range[0], 8); i <= options->block.range[1]; i++)
71+
if (options->brightness != FF_COLORS_BRIGHTNESS_NORMAL)
6972
{
70-
if (options->symbol == FF_COLORS_SYMBOL_BLOCK)
71-
{
72-
if(!instance.config.display.pipe)
73-
ffStrbufAppendF(&result, "\e[9%dm", i - 8);
74-
for (uint8_t j = 0; j < options->block.width; j++)
75-
ffStrbufAppendS(&result, "█");
76-
}
77-
else
73+
// 9%d: Set the foreground to the bright color
74+
for (uint8_t i = max(options->block.range[0], 8); i <= options->block.range[1]; i++)
7875
{
79-
ffStrbufAppendF(&result, "\e[10%dm", i - 8);
80-
ffStrbufAppendNC(&result, options->block.width, ' ');
76+
if (options->symbol == FF_COLORS_SYMBOL_BLOCK)
77+
{
78+
if(!instance.config.display.pipe)
79+
ffStrbufAppendF(&result, "\e[9%dm", i - 8);
80+
for (uint8_t j = 0; j < options->block.width; j++)
81+
ffStrbufAppendS(&result, "█");
82+
}
83+
else
84+
{
85+
ffStrbufAppendF(&result, "\e[10%dm", i - 8);
86+
ffStrbufAppendNC(&result, options->block.width, ' ');
87+
}
8188
}
8289
}
8390
}
@@ -93,11 +100,23 @@ bool ffPrintColors(FFColorsOptions* options)
93100
case FF_COLORS_SYMBOL_STAR: symbol = "★ "; break;
94101
default: symbol = "███ "; break;
95102
}
96-
for (int i = 8; i >= 1; --i)
103+
if (options->brightness == FF_COLORS_BRIGHTNESS_DEFAULT)
97104
{
98-
if (!instance.config.display.pipe)
99-
ffStrbufAppendF(&result, "\e[3%dm", i);
100-
ffStrbufAppendS(&result, symbol);
105+
for (int i = 8; i >= 1; --i)
106+
{
107+
if (!instance.config.display.pipe)
108+
ffStrbufAppendF(&result, "\e[" FF_COLOR_FG_256 "%dm", i);
109+
ffStrbufAppendS(&result, symbol);
110+
}
111+
}
112+
else
113+
{
114+
for (int i = 0; i <= 7; ++i)
115+
{
116+
if (!instance.config.display.pipe)
117+
ffStrbufAppendF(&result, "\e[%c%dm", options->brightness == FF_COLORS_BRIGHTNESS_NORMAL ? '3' : '9', i);
118+
ffStrbufAppendS(&result, symbol);
119+
}
101120
}
102121
ffStrbufTrimRight(&result, ' ');
103122
}
@@ -112,9 +131,9 @@ bool ffPrintColors(FFColorsOptions* options)
112131
flag = true;
113132
}
114133

115-
if(options->paddingLeft > 0)
134+
if (options->paddingLeft > 0)
116135
ffPrintCharTimes(' ', options->paddingLeft);
117-
if(!instance.config.display.pipe || options->symbol == FF_COLORS_SYMBOL_BACKGROUND)
136+
if (!instance.config.display.pipe || options->symbol == FF_COLORS_SYMBOL_BACKGROUND)
118137
ffStrbufAppendS(&result, FASTFETCH_TEXT_MODIFIER_RESET);
119138
ffStrbufPutTo(&result, stdout);
120139
}
@@ -197,6 +216,22 @@ void ffParseColorsJsonObject(FFColorsOptions* options, yyjson_val* module)
197216
continue;
198217
}
199218

219+
if (unsafe_yyjson_equals_str(key, "brightness"))
220+
{
221+
int value;
222+
const char* error = ffJsonConfigParseEnum(val, &value, (FFKeyValuePair[]) {
223+
{ "default", FF_COLORS_BRIGHTNESS_DEFAULT },
224+
{ "normal", FF_COLORS_BRIGHTNESS_NORMAL },
225+
{ "light", FF_COLORS_BRIGHTNESS_LIGHT },
226+
{},
227+
});
228+
if (error)
229+
ffPrintError(FF_COLORS_MODULE_NAME, 0, NULL, FF_PRINT_TYPE_NO_CUSTOM_KEY, "Invalid %s value: %s", unsafe_yyjson_get_str(key), error);
230+
else
231+
options->brightness = (FFColorsBrightness) value;
232+
continue;
233+
}
234+
200235
ffPrintError(FF_COLORS_MODULE_NAME, 0, NULL, FF_PRINT_TYPE_NO_CUSTOM_KEY, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
201236
}
202237
}
@@ -240,6 +275,7 @@ void ffInitColorsOptions(FFColorsOptions* options)
240275
.width = 3,
241276
.range = { 0, 15 },
242277
};
278+
options->brightness = FF_COLORS_BRIGHTNESS_DEFAULT;
243279
}
244280

245281
void ffDestroyColorsOptions(FFColorsOptions* options)

src/modules/colors/option.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,21 @@ typedef struct FFBlockConfig
1919
uint8_t range[2];
2020
} FFBlockConfig;
2121

22+
typedef enum __attribute__((__packed__)) FFColorsBrightness
23+
{
24+
FF_COLORS_BRIGHTNESS_DEFAULT,
25+
FF_COLORS_BRIGHTNESS_NORMAL,
26+
FF_COLORS_BRIGHTNESS_LIGHT,
27+
} FFColorsBrightness;
28+
2229
typedef struct FFColorsOptions
2330
{
2431
FFModuleArgs moduleArgs;
2532

2633
FFColorsSymbol symbol;
2734
uint32_t paddingLeft;
2835
FFBlockConfig block;
36+
FFColorsBrightness brightness;
2937
} FFColorsOptions;
3038

3139
static_assert(sizeof(FFColorsOptions) <= FF_OPTION_MAX_SIZE, "FFColorsOptions size exceeds maximum allowed size");

0 commit comments

Comments
 (0)