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
+1
View File
@@ -10,4 +10,5 @@ typedef struct FFSeparatorOptions
FFstrbuf string;
FFstrbuf outputColor;
uint32_t length;
} FFSeparatorOptions;
+31 -1
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)