Fastfetch: add -h format-json

This commit is contained in:
李通洲 2024-12-11 16:12:03 +08:00
parent 9a36d63d18
commit ebfdeef25c
No known key found for this signature in database
GPG Key ID: 269AD4F5325A22A3
3 changed files with 41 additions and 2 deletions

View File

@ -16,6 +16,43 @@
#include "util/windows/getline.h"
#endif
static void printCommandFormatHelpJson(void)
{
yyjson_mut_doc* doc = yyjson_mut_doc_new(NULL);
yyjson_mut_val* root = yyjson_mut_obj(doc);
yyjson_mut_doc_set_root(doc, root);
for (uint32_t i = 0; i <= 'Z' - 'A'; ++i)
{
for (FFModuleBaseInfo** modules = ffModuleInfos[i]; *modules; ++modules)
{
FFModuleBaseInfo* baseInfo = *modules;
if (!baseInfo->formatArgs.count) continue;
FF_STRBUF_AUTO_DESTROY type = ffStrbufCreateS(baseInfo->name);
ffStrbufLowerCase(&type);
ffStrbufAppendS(&type, "Format");
yyjson_mut_val* obj = yyjson_mut_obj(doc);
if (yyjson_mut_obj_add(root, yyjson_mut_strbuf(doc, &type), obj))
{
FF_STRBUF_AUTO_DESTROY content = ffStrbufCreateF("Output format of the module `%s`. See `-h format` for formatting syntax\n", baseInfo->name);
for (unsigned i = 0; i < baseInfo->formatArgs.count; i++)
{
const FFModuleFormatArg* arg = &baseInfo->formatArgs.args[i];
ffStrbufAppendF(&content, " %u. {%s}: %s\n", i + 1, arg->name, arg->desc);
}
ffStrbufTrimRight(&content, '\n');
yyjson_mut_obj_add_strbuf(doc, obj, "description", &content);
yyjson_mut_obj_add_str(doc, obj, "type", "string");
}
}
}
yyjson_mut_write_fp(stdout, doc, YYJSON_WRITE_PRETTY, NULL, NULL);
putchar('\n');
yyjson_mut_doc_free(doc);
}
static void printCommandFormatHelp(const char* command)
{
FF_STRBUF_AUTO_DESTROY type = ffStrbufCreateNS((uint32_t) (strlen(command) - strlen("-format")), command);
@ -265,6 +302,8 @@ static void printCommandHelp(const char* command)
puts(FASTFETCH_DATATEXT_HELP_COLOR);
else if(ffStrEqualsIgnCase(command, "format"))
puts(FASTFETCH_DATATEXT_HELP_FORMAT);
else if(ffStrEqualsIgnCase(command, "format-json"))
printCommandFormatHelpJson();
else if(ffCharIsEnglishAlphabet(command[0]) && ffStrEndsWithIgnCase(command, "-format")) // <module>-format
printCommandFormatHelp(command);
else if(!printSpecificCommandHelp(command))

View File

@ -218,7 +218,7 @@ void ffGenerateBtrfsJsonResult(FF_MAYBE_UNUSED FFBtrfsOptions* options, yyjson_m
static FFModuleBaseInfo ffModuleInfo = {
.name = FF_BTRFS_MODULE_NAME,
.description = "Print BTRFS volumes",
.description = "Print Linux BTRFS volumes",
.parseCommandOptions = (void*) ffParseBtrfsCommandOptions,
.parseJsonObject = (void*) ffParseBtrfsJsonObject,
.printModule = (void*) ffPrintBtrfs,

View File

@ -104,7 +104,7 @@ void ffGenerateFontJsonResult(FF_MAYBE_UNUSED FFFontOptions* options, yyjson_mut
static FFModuleBaseInfo ffModuleInfo = {
.name = FF_FONT_MODULE_NAME,
.description = "Print system font name",
.description = "Print system font names",
.parseCommandOptions = (void*) ffParseFontCommandOptions,
.parseJsonObject = (void*) ffParseFontJsonObject,
.printModule = (void*) ffPrintFont,