Skip to content

Commit

Permalink
Fastfetch: add -h format-json
Browse files Browse the repository at this point in the history
  • Loading branch information
CarterLi committed Dec 11, 2024
1 parent 9a36d63 commit ebfdeef
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
39 changes: 39 additions & 0 deletions src/fastfetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion src/modules/btrfs/btrfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/modules/font/font.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit ebfdeef

Please sign in to comment.