mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-10-30 00:37:23 +01:00
Formating: support specifying costant strings
This commit is contained in:
@@ -578,6 +578,13 @@
|
||||
"type": "boolean",
|
||||
"description": "Whether to disable the stdout application buffer",
|
||||
"default": false
|
||||
},
|
||||
"constants": {
|
||||
"type": "array",
|
||||
"description": "List of strings to be used in custom format of modules",
|
||||
"items": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
@@ -244,6 +244,27 @@ void ffParseFormatString(FFstrbuf* buffer, const FFstrbuf* formatstr, uint32_t n
|
||||
continue;
|
||||
}
|
||||
|
||||
//test for constant, if so evaluate it
|
||||
if (firstChar == '$')
|
||||
{
|
||||
char* pend = NULL;
|
||||
int32_t indexSigned = (int32_t) strtol(placeholderValue.chars + 1, &pend, 10);
|
||||
uint32_t index = (uint32_t) indexSigned;
|
||||
bool backward = indexSigned < 0;
|
||||
|
||||
if (indexSigned == 0 || *pend != '\0' || instance.config.display.constants.length < index)
|
||||
{
|
||||
appendInvalidPlaceholder(buffer, "{", &placeholderValue, i, formatstr->length);
|
||||
continue;
|
||||
}
|
||||
|
||||
FFstrbuf* item = FF_LIST_GET(FFstrbuf, instance.config.display.constants, backward
|
||||
? instance.config.display.constants.length - index
|
||||
: index - 1);
|
||||
ffStrbufAppend(buffer, item);
|
||||
continue;
|
||||
}
|
||||
|
||||
int32_t truncLength = INT32_MAX;
|
||||
char* pColon = memchr(placeholderValue.chars, ':', placeholderValue.length);
|
||||
if (pColon != NULL)
|
||||
|
||||
@@ -199,6 +199,15 @@ const char* ffOptionsParseDisplayJsonConfig(FFOptionsDisplay* options, yyjson_va
|
||||
options->noBuffer = yyjson_get_bool(val);
|
||||
else if (ffStrEqualsIgnCase(key, "keyWidth"))
|
||||
options->keyWidth = (uint32_t) yyjson_get_uint(val);
|
||||
else if (ffStrEqualsIgnCase(key, "constants"))
|
||||
{
|
||||
if (!yyjson_is_arr(val))
|
||||
return "display.constants must be an array";
|
||||
yyjson_val* item;
|
||||
size_t idx, max;
|
||||
yyjson_arr_foreach(val, idx, max, item)
|
||||
ffStrbufInitS(ffListAdd(&options->constants), yyjson_get_str(item));
|
||||
}
|
||||
else if (ffStrEqualsIgnCase(key, "tsVersion"))
|
||||
return "display.tsVersion has been renamed to general.detectVersion";
|
||||
else
|
||||
@@ -404,6 +413,8 @@ void ffOptionsInitDisplay(FFOptionsDisplay* options)
|
||||
ffStrbufInitStatic(&options->percentColorGreen, FF_COLOR_FG_GREEN);
|
||||
ffStrbufInitStatic(&options->percentColorYellow, instance.state.terminalLightTheme ? FF_COLOR_FG_YELLOW : FF_COLOR_FG_LIGHT_YELLOW);
|
||||
ffStrbufInitStatic(&options->percentColorRed, instance.state.terminalLightTheme ? FF_COLOR_FG_RED : FF_COLOR_FG_LIGHT_RED);
|
||||
|
||||
ffListInit(&options->constants, sizeof(FFstrbuf));
|
||||
}
|
||||
|
||||
void ffOptionsDestroyDisplay(FFOptionsDisplay* options)
|
||||
@@ -415,6 +426,9 @@ void ffOptionsDestroyDisplay(FFOptionsDisplay* options)
|
||||
ffStrbufDestroy(&options->keyValueSeparator);
|
||||
ffStrbufDestroy(&options->barCharElapsed);
|
||||
ffStrbufDestroy(&options->barCharTotal);
|
||||
FF_LIST_FOR_EACH(FFstrbuf, item, options->constants)
|
||||
ffStrbufDestroy(item);
|
||||
ffListDestroy(&options->constants);
|
||||
}
|
||||
|
||||
void ffOptionsGenerateDisplayJsonConfig(FFOptionsDisplay* options, yyjson_mut_doc* doc)
|
||||
|
||||
@@ -53,6 +53,7 @@ typedef struct FFOptionsDisplay
|
||||
FFstrbuf percentColorRed;
|
||||
bool noBuffer;
|
||||
uint32_t keyWidth;
|
||||
FFlist constants; // list of FFstrbuf
|
||||
} FFOptionsDisplay;
|
||||
|
||||
const char* ffOptionsParseDisplayJsonConfig(FFOptionsDisplay* options, yyjson_val* root);
|
||||
|
||||
Reference in New Issue
Block a user