Skip to content

Commit d50b4df

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

3 files changed

Lines changed: 94 additions & 32 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 & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "common/printing.h"
22
#include "common/jsonconfig.h"
33
#include "common/textModifier.h"
4-
#include "common/stringUtils.h"
4+
#include "common/color.h"
55
#include "logo/logo.h"
66
#include "modules/colors/colors.h"
77

@@ -23,20 +23,23 @@ bool ffPrintColors(FFColorsOptions* options)
2323

2424
if (options->symbol == FF_COLORS_SYMBOL_BLOCK || options->symbol == FF_COLORS_SYMBOL_BACKGROUND)
2525
{
26-
// 3%d: Set the foreground color
27-
for(uint8_t i = options->block.range[0]; i <= min(options->block.range[1], 7); i++)
26+
if (options->brightness != FF_COLORS_BRIGHTNESS_LIGHT)
2827
{
29-
if (options->symbol == FF_COLORS_SYMBOL_BLOCK)
28+
// 3%d: Set the foreground color
29+
for(uint8_t i = options->block.range[0]; i <= min(options->block.range[1], 7); i++)
3030
{
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, ' ');
31+
if (options->symbol == FF_COLORS_SYMBOL_BLOCK)
32+
{
33+
if (!instance.config.display.pipe)
34+
ffStrbufAppendF(&result, "\e[3%dm", i);
35+
for (uint8_t j = 0; j < options->block.width; j++)
36+
ffStrbufAppendS(&result, "█");
37+
}
38+
else
39+
{
40+
ffStrbufAppendF(&result, "\e[4%dm", i);
41+
ffStrbufAppendNC(&result, options->block.width, ' ');
42+
}
4043
}
4144
}
4245
if (result.length > 0)
@@ -64,20 +67,23 @@ bool ffPrintColors(FFColorsOptions* options)
6467
}
6568
#endif
6669

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++)
70+
if (options->brightness != FF_COLORS_BRIGHTNESS_NORMAL)
6971
{
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
72+
// 9%d: Set the foreground to the bright color
73+
for (uint8_t i = max(options->block.range[0], 8); i <= options->block.range[1]; i++)
7874
{
79-
ffStrbufAppendF(&result, "\e[10%dm", i - 8);
80-
ffStrbufAppendNC(&result, options->block.width, ' ');
75+
if (options->symbol == FF_COLORS_SYMBOL_BLOCK)
76+
{
77+
if(!instance.config.display.pipe)
78+
ffStrbufAppendF(&result, "\e[9%dm", i - 8);
79+
for (uint8_t j = 0; j < options->block.width; j++)
80+
ffStrbufAppendS(&result, "█");
81+
}
82+
else
83+
{
84+
ffStrbufAppendF(&result, "\e[10%dm", i - 8);
85+
ffStrbufAppendNC(&result, options->block.width, ' ');
86+
}
8187
}
8288
}
8389
}
@@ -93,11 +99,23 @@ bool ffPrintColors(FFColorsOptions* options)
9399
case FF_COLORS_SYMBOL_STAR: symbol = "★ "; break;
94100
default: symbol = "███ "; break;
95101
}
96-
for (int i = 8; i >= 1; --i)
102+
if (options->brightness == FF_COLORS_BRIGHTNESS_DEFAULT)
97103
{
98-
if (!instance.config.display.pipe)
99-
ffStrbufAppendF(&result, "\e[3%dm", i);
100-
ffStrbufAppendS(&result, symbol);
104+
for (int i = 8; i >= 1; --i)
105+
{
106+
if (!instance.config.display.pipe)
107+
ffStrbufAppendF(&result, "\e[" FF_COLOR_FG_256 "%dm", i);
108+
ffStrbufAppendS(&result, symbol);
109+
}
110+
}
111+
else
112+
{
113+
for (int i = 0; i <= 7; ++i)
114+
{
115+
if (!instance.config.display.pipe)
116+
ffStrbufAppendF(&result, "\e[%c%dm", options->brightness == FF_COLORS_BRIGHTNESS_NORMAL ? '3' : '9', i);
117+
ffStrbufAppendS(&result, symbol);
118+
}
101119
}
102120
ffStrbufTrimRight(&result, ' ');
103121
}
@@ -112,9 +130,9 @@ bool ffPrintColors(FFColorsOptions* options)
112130
flag = true;
113131
}
114132

115-
if(options->paddingLeft > 0)
133+
if (options->paddingLeft > 0)
116134
ffPrintCharTimes(' ', options->paddingLeft);
117-
if(!instance.config.display.pipe || options->symbol == FF_COLORS_SYMBOL_BACKGROUND)
135+
if (!instance.config.display.pipe || options->symbol == FF_COLORS_SYMBOL_BACKGROUND)
118136
ffStrbufAppendS(&result, FASTFETCH_TEXT_MODIFIER_RESET);
119137
ffStrbufPutTo(&result, stdout);
120138
}
@@ -197,6 +215,22 @@ void ffParseColorsJsonObject(FFColorsOptions* options, yyjson_val* module)
197215
continue;
198216
}
199217

218+
if (unsafe_yyjson_equals_str(key, "brightness"))
219+
{
220+
int value;
221+
const char* error = ffJsonConfigParseEnum(val, &value, (FFKeyValuePair[]) {
222+
{ "default", FF_COLORS_BRIGHTNESS_DEFAULT },
223+
{ "normal", FF_COLORS_BRIGHTNESS_NORMAL },
224+
{ "light", FF_COLORS_BRIGHTNESS_LIGHT },
225+
{},
226+
});
227+
if (error)
228+
ffPrintError(FF_COLORS_MODULE_NAME, 0, NULL, FF_PRINT_TYPE_NO_CUSTOM_KEY, "Invalid %s value: %s", unsafe_yyjson_get_str(key), error);
229+
else
230+
options->brightness = (FFColorsBrightness) value;
231+
continue;
232+
}
233+
200234
ffPrintError(FF_COLORS_MODULE_NAME, 0, NULL, FF_PRINT_TYPE_NO_CUSTOM_KEY, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
201235
}
202236
}
@@ -240,6 +274,7 @@ void ffInitColorsOptions(FFColorsOptions* options)
240274
.width = 3,
241275
.range = { 0, 15 },
242276
};
277+
options->brightness = FF_COLORS_BRIGHTNESS_DEFAULT;
243278
}
244279

245280
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)