From f188e9518d3ad1e32dac684bc543dc0b0c7bee75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sun, 22 Mar 2026 13:36:14 +0800 Subject: [PATCH] Colors: add `brightness` option for color display configuration Fixes #2238 --- doc/json_schema.json | 21 +++++++- src/modules/colors/colors.c | 96 +++++++++++++++++++++++++------------ src/modules/colors/option.h | 8 ++++ 3 files changed, 94 insertions(+), 31 deletions(-) diff --git a/doc/json_schema.json b/doc/json_schema.json index c3e1d76bc..1ee1b95b2 100644 --- a/doc/json_schema.json +++ b/doc/json_schema.json @@ -2044,7 +2044,7 @@ "default": 0 }, "block": { - "description": "Set behavior of block printing", + "description": "Set behavior of block printing. Only works when symbol is set to `block` or `background`", "type": "object", "additionalProperties": false, "properties": { @@ -2067,6 +2067,25 @@ } } }, + "brightness": { + "description": "Set the brightness of colors. Works together with the block color range to determine which colors are displayed", + "type": "string", + "oneOf": [ + { + "const": "default", + "description": "Use the default setting (block/background: 0-15, others: 8-1)" + }, + { + "const": "light", + "description": "Use ANSI bright foreground colors (codes 90-97, not color indices 9-15)" + }, + { + "const": "normal", + "description": "Use normal ANSI colors (30-37; standard foreground colors, corresponding to color indices 0-7)" + } + ], + "default": "default" + }, "key": { "$ref": "#/$defs/key" }, diff --git a/src/modules/colors/colors.c b/src/modules/colors/colors.c index fb5aed0f6..c546e0a01 100644 --- a/src/modules/colors/colors.c +++ b/src/modules/colors/colors.c @@ -1,6 +1,7 @@ #include "common/printing.h" #include "common/jsonconfig.h" #include "common/textModifier.h" +#include "common/color.h" #include "common/stringUtils.h" #include "logo/logo.h" #include "modules/colors/colors.h" @@ -23,20 +24,23 @@ bool ffPrintColors(FFColorsOptions* options) if (options->symbol == FF_COLORS_SYMBOL_BLOCK || options->symbol == FF_COLORS_SYMBOL_BACKGROUND) { - // 3%d: Set the foreground color - for(uint8_t i = options->block.range[0]; i <= min(options->block.range[1], 7); i++) + if (options->brightness != FF_COLORS_BRIGHTNESS_LIGHT) { - if (options->symbol == FF_COLORS_SYMBOL_BLOCK) + // 3%d: Set the foreground color + for(uint8_t i = options->block.range[0]; i <= min(options->block.range[1], 7); i++) { - if (!instance.config.display.pipe) - ffStrbufAppendF(&result, "\e[3%dm", i); - for (uint8_t j = 0; j < options->block.width; j++) - ffStrbufAppendS(&result, "█"); - } - else - { - ffStrbufAppendF(&result, "\e[4%dm", i); - ffStrbufAppendNC(&result, options->block.width, ' '); + if (options->symbol == FF_COLORS_SYMBOL_BLOCK) + { + if (!instance.config.display.pipe) + ffStrbufAppendF(&result, "\e[3%dm", i); + for (uint8_t j = 0; j < options->block.width; j++) + ffStrbufAppendS(&result, "█"); + } + else + { + ffStrbufAppendF(&result, "\e[4%dm", i); + ffStrbufAppendNC(&result, options->block.width, ' '); + } } } if (result.length > 0) @@ -64,20 +68,23 @@ bool ffPrintColors(FFColorsOptions* options) } #endif - // 9%d: Set the foreground to the bright color - for(uint8_t i = max(options->block.range[0], 8); i <= options->block.range[1]; i++) + if (options->brightness != FF_COLORS_BRIGHTNESS_NORMAL) { - if (options->symbol == FF_COLORS_SYMBOL_BLOCK) + // 9%d: Set the foreground to the bright color + for (uint8_t i = max(options->block.range[0], 8); i <= options->block.range[1]; i++) { - if(!instance.config.display.pipe) - ffStrbufAppendF(&result, "\e[9%dm", i - 8); - for (uint8_t j = 0; j < options->block.width; j++) - ffStrbufAppendS(&result, "█"); - } - else - { - ffStrbufAppendF(&result, "\e[10%dm", i - 8); - ffStrbufAppendNC(&result, options->block.width, ' '); + if (options->symbol == FF_COLORS_SYMBOL_BLOCK) + { + if(!instance.config.display.pipe) + ffStrbufAppendF(&result, "\e[9%dm", i - 8); + for (uint8_t j = 0; j < options->block.width; j++) + ffStrbufAppendS(&result, "█"); + } + else + { + ffStrbufAppendF(&result, "\e[10%dm", i - 8); + ffStrbufAppendNC(&result, options->block.width, ' '); + } } } } @@ -93,11 +100,23 @@ bool ffPrintColors(FFColorsOptions* options) case FF_COLORS_SYMBOL_STAR: symbol = "★ "; break; default: symbol = "███ "; break; } - for (int i = 8; i >= 1; --i) + if (options->brightness == FF_COLORS_BRIGHTNESS_DEFAULT) { - if (!instance.config.display.pipe) - ffStrbufAppendF(&result, "\e[3%dm", i); - ffStrbufAppendS(&result, symbol); + for (int i = 8; i >= 1; --i) + { + if (!instance.config.display.pipe) + ffStrbufAppendF(&result, "\e[" FF_COLOR_FG_256 "%dm", i); + ffStrbufAppendS(&result, symbol); + } + } + else + { + for (int i = 0; i <= 7; ++i) + { + if (!instance.config.display.pipe) + ffStrbufAppendF(&result, "\e[%c%dm", options->brightness == FF_COLORS_BRIGHTNESS_NORMAL ? '3' : '9', i); + ffStrbufAppendS(&result, symbol); + } } ffStrbufTrimRight(&result, ' '); } @@ -112,9 +131,9 @@ bool ffPrintColors(FFColorsOptions* options) flag = true; } - if(options->paddingLeft > 0) + if (options->paddingLeft > 0) ffPrintCharTimes(' ', options->paddingLeft); - if(!instance.config.display.pipe || options->symbol == FF_COLORS_SYMBOL_BACKGROUND) + if (!instance.config.display.pipe || options->symbol == FF_COLORS_SYMBOL_BACKGROUND) ffStrbufAppendS(&result, FASTFETCH_TEXT_MODIFIER_RESET); ffStrbufPutTo(&result, stdout); } @@ -197,6 +216,22 @@ void ffParseColorsJsonObject(FFColorsOptions* options, yyjson_val* module) continue; } + if (unsafe_yyjson_equals_str(key, "brightness")) + { + int value; + const char* error = ffJsonConfigParseEnum(val, &value, (FFKeyValuePair[]) { + { "default", FF_COLORS_BRIGHTNESS_DEFAULT }, + { "normal", FF_COLORS_BRIGHTNESS_NORMAL }, + { "light", FF_COLORS_BRIGHTNESS_LIGHT }, + {}, + }); + if (error) + ffPrintError(FF_COLORS_MODULE_NAME, 0, NULL, FF_PRINT_TYPE_NO_CUSTOM_KEY, "Invalid %s value: %s", unsafe_yyjson_get_str(key), error); + else + options->brightness = (FFColorsBrightness) value; + continue; + } + ffPrintError(FF_COLORS_MODULE_NAME, 0, NULL, FF_PRINT_TYPE_NO_CUSTOM_KEY, "Unknown JSON key %s", unsafe_yyjson_get_str(key)); } } @@ -240,6 +275,7 @@ void ffInitColorsOptions(FFColorsOptions* options) .width = 3, .range = { 0, 15 }, }; + options->brightness = FF_COLORS_BRIGHTNESS_DEFAULT; } void ffDestroyColorsOptions(FFColorsOptions* options) diff --git a/src/modules/colors/option.h b/src/modules/colors/option.h index 6a0bbb0d4..6155976a3 100644 --- a/src/modules/colors/option.h +++ b/src/modules/colors/option.h @@ -19,6 +19,13 @@ typedef struct FFBlockConfig uint8_t range[2]; } FFBlockConfig; +typedef enum __attribute__((__packed__)) FFColorsBrightness +{ + FF_COLORS_BRIGHTNESS_DEFAULT, + FF_COLORS_BRIGHTNESS_NORMAL, + FF_COLORS_BRIGHTNESS_LIGHT, +} FFColorsBrightness; + typedef struct FFColorsOptions { FFModuleArgs moduleArgs; @@ -26,6 +33,7 @@ typedef struct FFColorsOptions FFColorsSymbol symbol; uint32_t paddingLeft; FFBlockConfig block; + FFColorsBrightness brightness; } FFColorsOptions; static_assert(sizeof(FFColorsOptions) <= FF_OPTION_MAX_SIZE, "FFColorsOptions size exceeds maximum allowed size");