Separator: add option --separator-length

This commit is contained in:
Carter Li 2024-07-17 09:16:25 +08:00
parent d869164d6b
commit d1609ea96b
4 changed files with 51 additions and 4 deletions

View File

@ -1948,12 +1948,19 @@
"const": "separator"
},
"string": {
"description": "Set the string to be printed",
"description": "Set the string to be printed by the separator line",
"type": "string",
"default": "-"
},
"outputColor": {
"description": "Set the color of the separator line",
"$ref": "#/$defs/outputColor"
},
"length": {
"description": "Set the length of the separator line, or 0 to auto-detect",
"type": "integer",
"minimum": 0,
"default": 0
}
}
},

View File

@ -947,7 +947,7 @@
},
{
"long": "separator-string",
"desc": "Set the string printed by the separator module",
"desc": "Set the string to be printed by the separator line",
"arg": {
"type": "str",
"default": "-"
@ -955,12 +955,21 @@
},
{
"long": "separator-output-color",
"desc": "Set the color of the separator module",
"desc": "Set the color of the separator line",
"arg": {
"type": "color",
"default": "default"
}
},
{
"long": "separator-length",
"desc": "Set the length of the separator line",
"remark": "Set to 0 to automatically calculate it with the title length",
"arg": {
"type": "num",
"default": "0"
}
},
{
"long": "disk-folders",
"desc": "A colon (semicolon on Windows) separated list of folder paths to be detected",

View File

@ -10,4 +10,5 @@ typedef struct FFSeparatorOptions
FFstrbuf string;
FFstrbuf outputColor;
uint32_t length;
} FFSeparatorOptions;

View File

@ -34,6 +34,23 @@ static inline uint32_t getWcsWidth(const FFstrbuf* mbstr, wchar_t* wstr, mbstate
void ffPrintSeparator(FFSeparatorOptions* options)
{
ffLogoPrintLine();
if (options->length > 0)
{
if(__builtin_expect(options->string.length == 1, 1))
ffPrintCharTimes(options->string.chars[0], options->length);
else
{
for (uint32_t i = 0; i < options->length; i++)
{
fputs(options->string.chars, stdout);
}
}
putchar('\n');
return;
}
setlocale(LC_CTYPE, "");
mbstate_t state = {};
bool fqdn = instance.config.modules.title.fqdn;
@ -45,7 +62,6 @@ void ffPrintSeparator(FFSeparatorOptions* options)
uint32_t titleLength = 1 // @
+ getWcsWidth(&platform->userName, wstr, &state) // user name
+ (fqdn ? platform->hostName.length : ffStrbufFirstIndexC(&platform->hostName, '.')); // host name
ffLogoPrintLine();
if(options->outputColor.length && !instance.config.display.pipe)
ffPrintColor(&options->outputColor);
@ -112,6 +128,12 @@ bool ffParseSeparatorCommandOptions(FFSeparatorOptions* options, const char* key
return true;
}
if (ffStrEqualsIgnCase(subKey, "length"))
{
options->length = ffOptionParseUInt32(key, value);
return true;
}
return false;
}
@ -137,6 +159,12 @@ void ffParseSeparatorJsonObject(FFSeparatorOptions* options, yyjson_val* module)
continue;
}
if (ffStrEndsWithIgnCase(key, "length"))
{
options->length = (uint32_t) yyjson_get_uint(val);
continue;
}
ffPrintError(FF_SEPARATOR_MODULE_NAME, 0, NULL, FF_PRINT_TYPE_NO_CUSTOM_KEY, "Unknown JSON key %s", key);
}
}
@ -164,6 +192,8 @@ void ffInitSeparatorOptions(FFSeparatorOptions* options)
ffGenerateSeparatorJsonConfig
);
ffStrbufInitStatic(&options->string, "-");
ffStrbufInit(&options->outputColor);
options->length = 0;
}
void ffDestroySeparatorOptions(FFSeparatorOptions* options)