From ebfdeef25c3a3725d29a75e2ee6666d15cd82454 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Wed, 11 Dec 2024 16:12:03 +0800 Subject: [PATCH] Fastfetch: add `-h format-json` --- src/fastfetch.c | 39 +++++++++++++++++++++++++++++++++++++++ src/modules/btrfs/btrfs.c | 2 +- src/modules/font/font.c | 2 +- 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/fastfetch.c b/src/fastfetch.c index 2733bd2c..3c8245ab 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -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")) // -format printCommandFormatHelp(command); else if(!printSpecificCommandHelp(command)) diff --git a/src/modules/btrfs/btrfs.c b/src/modules/btrfs/btrfs.c index 59113957..6d6f1a7d 100644 --- a/src/modules/btrfs/btrfs.c +++ b/src/modules/btrfs/btrfs.c @@ -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, diff --git a/src/modules/font/font.c b/src/modules/font/font.c index f5d56947..242b685e 100644 --- a/src/modules/font/font.c +++ b/src/modules/font/font.c @@ -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,