Custom: support JSON module

Note: old command options `--set` and `--set-keyless` were removed. To reuse custom module, JSON config must be used.
This commit is contained in:
李通洲
2023-03-20 15:46:00 +08:00
parent 83dda3e8ab
commit e3e8c689ed
13 changed files with 99 additions and 120 deletions
-10
View File
@@ -1,10 +0,0 @@
#include "fastfetch.h"
#include "common/printing.h"
#include "util/textModifier.h"
void ffPrintCustom(FFinstance* instance, const char* key, const char* value)
{
ffPrintLogoAndKey(instance, key, 0, NULL);
ffPrintUserString(value);
puts(FASTFETCH_TEXT_MODIFIER_RESET);
}
+59
View File
@@ -0,0 +1,59 @@
#include "fastfetch.h"
#include "common/printing.h"
#include "modules/custom/custom.h"
#include "util/textModifier.h"
void ffPrintCustom(FFinstance* instance, FFCustomOptions* options)
{
if (options->moduleArgs.outputFormat.length == 0)
{
ffPrintError(instance, FF_CUSTOM_MODULE_NAME, 0, &options->moduleArgs, "output format must be set for custom module");
return;
}
ffPrintLogoAndKey(instance, options->moduleArgs.key.length == 0 ? NULL : FF_CUSTOM_MODULE_NAME, 0, &options->moduleArgs.key);
ffPrintUserString(options->moduleArgs.outputFormat.chars);
puts(FASTFETCH_TEXT_MODIFIER_RESET);
}
void ffInitCustomOptions(FFCustomOptions* options)
{
options->moduleName = FF_CUSTOM_MODULE_NAME;
ffOptionInitModuleArg(&options->moduleArgs);
}
bool ffParseCustomCommandOptions(FFCustomOptions* options, const char* key, const char* value)
{
const char* subKey = ffOptionTestPrefix(key, FF_CUSTOM_MODULE_NAME);
if (!subKey) return false;
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
return true;
return false;
}
void ffDestroyCustomOptions(FFCustomOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
}
#ifdef FF_HAVE_JSONC
void ffParseCustomJsonObject(FFinstance* instance, json_object* module)
{
FFCustomOptions __attribute__((__cleanup__(ffDestroyCustomOptions))) options;
ffInitCustomOptions(&options);
if (module)
{
json_object_object_foreach(module, key, val)
{
if (ffJsonConfigParseModuleArgs(key, val, &options.moduleArgs))
continue;
ffPrintError(instance, FF_CUSTOM_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key);
}
}
ffPrintCustom(instance, &options);
}
#endif
+15
View File
@@ -0,0 +1,15 @@
#pragma once
#include "fastfetch.h"
#define FF_CUSTOM_MODULE_NAME "Custom"
void ffPrintCustom(FFinstance* instance, FFCustomOptions* options);
void ffInitCustomOptions(FFCustomOptions* options);
bool ffParseCustomCommandOptions(FFCustomOptions* options, const char* key, const char* value);
void ffDestroyCustomOptions(FFCustomOptions* options);
#ifdef FF_HAVE_JSONC
#include "common/jsonconfig.h"
void ffParseCustomJsonObject(FFinstance* instance, json_object* module);
#endif
+11
View File
@@ -0,0 +1,11 @@
#pragma once
// This file will be included in "fastfetch.h", do NOT put unnecessary things here
#include "common/option.h"
typedef struct FFCustomOptions
{
const char* moduleName;
FFModuleArgs moduleArgs;
} FFCustomOptions;
+1
View File
@@ -44,6 +44,7 @@ static bool parseModuleJsonObject(FFinstance* instance, const char* type, json_o
tryModule(instance, type, module, FF_COMMAND_MODULE_NAME, ffParseCommandJsonObject) ||
tryModule(instance, type, module, FF_COLORS_MODULE_NAME, ffParseColorsJsonObject) ||
tryModule(instance, type, module, FF_CURSOR_MODULE_NAME, ffParseCursorJsonObject) ||
tryModule(instance, type, module, FF_CUSTOM_MODULE_NAME, ffParseCustomJsonObject) ||
false;
}
+1
View File
@@ -14,6 +14,7 @@
#include "modules/command/command.h"
#include "modules/colors/colors.h"
#include "modules/cursor/cursor.h"
#include "modules/custom/custom.h"
#include "modules/datetime/datetime.h"
#include "modules/display/display.h"
#include "modules/host/host.h"
+1
View File
@@ -10,6 +10,7 @@
#include "modules/cpu/option.h"
#include "modules/cpuusage/option.h"
#include "modules/cursor/option.h"
#include "modules/custom/option.h"
#include "modules/command/option.h"
#include "modules/datetime/option.h"
#include "modules/display/option.h"