From 263c54a81ec611a0a91b699744e01143bc5b8b39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Wed, 24 Jul 2024 15:12:25 +0800 Subject: [PATCH] Global: add option `display.key.paddingLeft` Also rename `display.keyWidth` to `display.key.width` --- CHANGELOG.md | 16 ++++++++++ doc/json_schema.json | 45 ++++++++++++++++---------- src/common/printing.c | 2 ++ src/data/help.json | 7 +++++ src/options/display.c | 73 ++++++++++++++++++++++++++++++------------- src/options/display.h | 3 +- 6 files changed, 107 insertions(+), 39 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f89f97f4..de4376403 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,19 @@ +# 2.20.0 + +Changes: +* JSON option `display.keyWidth` has been renamed to `display.key.width` + * Previously: `{ "display": { "keyWidth": 3 } }` + * Now: `{ "display": { "key": { "width": 3 } } }` + +Features +* Add option `display.key.type: ` to print icons in keys + * Supported value `string`, `icon` and `both`. Default to `string` (don't display icons) + * Example: `{ "display": { "key": { "type": "icon" } } }` +* Add option `display.key.paddingLeft: ` to print left padding (whitespaces) in keys + * Example: `{ "display": { "key": { "paddingLeft": 2 } } }` +* Add option `modules.keyIcon` to set icon for specified module + * Example: `{ "modules": { "type": "command", "keyIcon": "🔑" } }` + # 2.19.1 Bugfixes diff --git a/doc/json_schema.json b/doc/json_schema.json index a74b594da..e17e870dc 100644 --- a/doc/json_schema.json +++ b/doc/json_schema.json @@ -422,22 +422,35 @@ "type": "boolean", "default": true }, - "keyWidth": { - "description": "Align the width of keys to number of characters, 0 to disable", - "type": "integer", - "minimum": 0, - "default": 0 - }, - "keyType": { - "type": "string", - "description": "Set the type of keys to display", - "enum": [ - "none", - "string", - "icon", - "botn" - ], - "default": "string" + "key": { + "type": "object", + "additionalProperties": false, + "description": "Set how module keys should be displayed", + "properties": { + "width": { + "description": "Align the width of keys to number of characters, 0 to disable", + "type": "integer", + "minimum": 0, + "default": 0 + }, + "type": { + "type": "string", + "description": "Set the type of keys to display", + "enum": [ + "none", + "string", + "icon", + "both" + ], + "default": "string" + }, + "paddingLeft": { + "type": "integer", + "description": "Set the left padding of keys", + "minimum": 0, + "default": 0 + } + } }, "size": { "type": "object", diff --git a/src/common/printing.c b/src/common/printing.c index 9bde8e414..89725b2c2 100644 --- a/src/common/printing.c +++ b/src/common/printing.c @@ -13,6 +13,8 @@ void ffPrintLogoAndKey(const char* moduleName, uint8_t moduleIndex, const FFModu //This is used as a magic value for hiding keys if (!(moduleArgs && ffStrbufEqualS(&moduleArgs->key, " ")) && instance.config.display.keyType != FF_MODULE_KEY_TYPE_NONE) { + ffPrintCharTimes(' ', instance.config.display.keyPaddingLeft); + if(!instance.config.display.pipe) { fputs(FASTFETCH_TEXT_MODIFIER_RESET, stdout); diff --git a/src/data/help.json b/src/data/help.json index 99a55a6b7..7e7f68401 100644 --- a/src/data/help.json +++ b/src/data/help.json @@ -505,6 +505,13 @@ "type": "num" } }, + { + "long": "key-padding-left", + "desc": "Set the left padding of keys to characters", + "arg": { + "type": "num" + } + }, { "long": "key-type", "desc": "Set the type of keys to display", diff --git a/src/options/display.c b/src/options/display.c index 112cc7e89..cc844b08f 100644 --- a/src/options/display.c +++ b/src/options/display.c @@ -202,19 +202,36 @@ const char* ffOptionsParseDisplayJsonConfig(FFOptionsDisplay* options, yyjson_va else if (ffStrEqualsIgnCase(key, "noBuffer")) options->noBuffer = yyjson_get_bool(val); else if (ffStrEqualsIgnCase(key, "keyWidth")) - options->keyWidth = (uint32_t) yyjson_get_uint(val); - else if (ffStrEqualsIgnCase(key, "keyType")) + return "display.keyWidth has been renamed to display.key.width"; + else if (ffStrEqualsIgnCase(key, "key")) { - int value; - const char* error = ffJsonConfigParseEnum(val, &value, (FFKeyValuePair[]) { - { "none", FF_MODULE_KEY_TYPE_NONE }, - { "string", FF_MODULE_KEY_TYPE_STRING }, - { "icon", FF_MODULE_KEY_TYPE_ICON }, - { "both", FF_MODULE_KEY_TYPE_BOTH }, - {} - }); - if (error) return error; - options->keyType = (uint8_t) value; + if (yyjson_is_obj(val)) + { + yyjson_val* width = yyjson_obj_get(val, "width"); + if (width) + options->keyWidth = (uint16_t) yyjson_get_uint(width); + + yyjson_val* type = yyjson_obj_get(val, "type"); + if (type) + { + int value; + const char* error = ffJsonConfigParseEnum(type, &value, (FFKeyValuePair[]) { + { "none", FF_MODULE_KEY_TYPE_NONE }, + { "string", FF_MODULE_KEY_TYPE_STRING }, + { "icon", FF_MODULE_KEY_TYPE_ICON }, + { "both", FF_MODULE_KEY_TYPE_BOTH }, + {} + }); + if (error) return error; + options->keyType = (uint8_t) value; + } + + yyjson_val* paddingLeft = yyjson_obj_get(val, "paddingLeft"); + if (paddingLeft) + options->keyPaddingLeft = (uint16_t) yyjson_get_uint(paddingLeft); + } + else + return "display.key must be an object"; } else if (ffStrEqualsIgnCase(key, "constants")) { @@ -299,17 +316,25 @@ bool ffOptionsParseDisplayCommandLine(FFOptionsDisplay* options, const char* key else return false; } - else if(ffStrEqualsIgnCase(key, "--key-width")) - options->keyWidth = ffOptionParseUInt32(key, value); - else if(ffStrEqualsIgnCase(key, "--key-type")) + else if(ffStrStartsWithIgnCase(key, "--key-")) { - options->keyType = (FFModuleKeyType) ffOptionParseEnum(key, value, (FFKeyValuePair[]) { - { "none", FF_MODULE_KEY_TYPE_NONE }, - { "string", FF_MODULE_KEY_TYPE_STRING }, - { "icon", FF_MODULE_KEY_TYPE_ICON }, - { "both", FF_MODULE_KEY_TYPE_BOTH }, - {} - }); + const char* subkey = key + strlen("--key-"); + if(ffStrEqualsIgnCase(subkey, "width")) + options->keyWidth = (uint16_t) ffOptionParseUInt32(key, value); + else if(ffStrEqualsIgnCase(subkey, "type")) + { + options->keyType = (FFModuleKeyType) ffOptionParseEnum(key, value, (FFKeyValuePair[]) { + { "none", FF_MODULE_KEY_TYPE_NONE }, + { "string", FF_MODULE_KEY_TYPE_STRING }, + { "icon", FF_MODULE_KEY_TYPE_ICON }, + { "both", FF_MODULE_KEY_TYPE_BOTH }, + {} + }); + } + else if(ffStrEqualsIgnCase(subkey, "padding-left")) + options->keyPaddingLeft = (uint16_t) ffOptionParseUInt32(key, value); + else + return false; } else if(ffStrEqualsIgnCase(key, "--bright-color")) options->brightColor = ffOptionParseBoolean(value); @@ -448,6 +473,7 @@ void ffOptionsInitDisplay(FFOptionsDisplay* options) options->stat = false; options->noBuffer = false; options->keyWidth = 0; + options->keyPaddingLeft = 0; options->keyType = FF_MODULE_KEY_TYPE_STRING; options->tempUnit = FF_TEMPERATURE_UNIT_CELSIUS; @@ -650,6 +676,9 @@ void ffOptionsGenerateDisplayJsonConfig(FFOptionsDisplay* options, yyjson_mut_do if (options->keyType != defaultOptions.keyType) yyjson_mut_obj_add_uint(doc, obj, "keyType", options->keyType); + if (options->keyPaddingLeft != defaultOptions.keyPaddingLeft) + yyjson_mut_obj_add_uint(doc, obj, "keyPaddingLeft", options->keyPaddingLeft); + { yyjson_mut_val* freq = yyjson_mut_obj(doc); if (options->freqNdigits != defaultOptions.freqNdigits) diff --git a/src/options/display.h b/src/options/display.h index 1d38f1449..934c2cac1 100644 --- a/src/options/display.h +++ b/src/options/display.h @@ -52,8 +52,9 @@ typedef struct FFOptionsDisplay FFstrbuf percentColorYellow; FFstrbuf percentColorRed; bool noBuffer; - uint32_t keyWidth; FFModuleKeyType keyType; + uint16_t keyWidth; + uint16_t keyPaddingLeft; int8_t freqNdigits; FFlist constants; // list of FFstrbuf } FFOptionsDisplay;