Icons: add JSON config support

This commit is contained in:
李通洲
2023-06-07 09:49:38 +08:00
committed by 李通洲
parent 823f773e26
commit a3524c3af1
12 changed files with 114 additions and 39 deletions
-30
View File
@@ -1,30 +0,0 @@
#include "fastfetch.h"
#include "common/printing.h"
#include "detection/icons/icons.h"
#define FF_ICONS_MODULE_NAME "Icons"
#define FF_ICONS_NUM_FORMAT_ARGS 1
void ffPrintIcons(FFinstance* instance)
{
FF_STRBUF_AUTO_DESTROY icons = ffStrbufCreate();
const char* error = ffDetectIcons(instance, &icons);
if(error)
{
ffPrintError(instance, FF_ICONS_MODULE_NAME, 0, &instance->config.icons, "%s", error);
return;
}
if(instance->config.icons.outputFormat.length == 0)
{
ffPrintLogoAndKey(instance, FF_ICONS_MODULE_NAME, 0, &instance->config.icons.key);
ffStrbufPutTo(&icons, stdout);
}
else
{
ffPrintFormat(instance, FF_ICONS_MODULE_NAME, 0, &instance->config.icons, FF_ICONS_NUM_FORMAT_ARGS, (FFformatarg[]){
{FF_FORMAT_ARG_TYPE_STRBUF, &icons}
});
}
}
+72
View File
@@ -0,0 +1,72 @@
#include "fastfetch.h"
#include "common/printing.h"
#include "detection/icons/icons.h"
#include "modules/icons/icons.h"
#define FF_ICONS_NUM_FORMAT_ARGS 1
void ffPrintIcons(FFinstance* instance, FFIconsOptions* options)
{
FF_STRBUF_AUTO_DESTROY icons = ffStrbufCreate();
const char* error = ffDetectIcons(instance, &icons);
if(error)
{
ffPrintError(instance, FF_ICONS_MODULE_NAME, 0, &options->moduleArgs, "%s", error);
return;
}
if(options->moduleArgs.outputFormat.length == 0)
{
ffPrintLogoAndKey(instance, FF_ICONS_MODULE_NAME, 0, &options->moduleArgs.key);
ffStrbufPutTo(&icons, stdout);
}
else
{
ffPrintFormat(instance, FF_ICONS_MODULE_NAME, 0, &options->moduleArgs, FF_ICONS_NUM_FORMAT_ARGS, (FFformatarg[]){
{FF_FORMAT_ARG_TYPE_STRBUF, &icons}
});
}
}
void ffInitIconsOptions(FFIconsOptions* options)
{
options->moduleName = FF_ICONS_MODULE_NAME;
ffOptionInitModuleArg(&options->moduleArgs);
}
bool ffParseIconsCommandOptions(FFIconsOptions* options, const char* key, const char* value)
{
const char* subKey = ffOptionTestPrefix(key, FF_ICONS_MODULE_NAME);
if (!subKey) return false;
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
return true;
return false;
}
void ffDestroyIconsOptions(FFIconsOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
}
#ifdef FF_HAVE_JSONC
void ffParseIconsJsonObject(FFinstance* instance, json_object* module)
{
FFIconsOptions __attribute__((__cleanup__(ffDestroyIconsOptions))) options;
ffInitIconsOptions(&options);
if (module)
{
json_object_object_foreach(module, key, val)
{
if (ffJsonConfigParseModuleArgs(key, val, &options.moduleArgs))
continue;
ffPrintError(instance, FF_ICONS_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key);
}
}
ffPrintIcons(instance, &options);
}
#endif
+15
View File
@@ -0,0 +1,15 @@
#pragma once
#include "fastfetch.h"
#define FF_ICONS_MODULE_NAME "Icons"
void ffPrintIcons(FFinstance* instance, FFIconsOptions* options);
void ffInitIconsOptions(FFIconsOptions* options);
bool ffParseIconsCommandOptions(FFIconsOptions* options, const char* key, const char* value);
void ffDestroyIconsOptions(FFIconsOptions* options);
#ifdef FF_HAVE_JSONC
#include "common/jsonconfig.h"
void ffParseIconsJsonObject(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 FFIconsOptions
{
const char* moduleName;
FFModuleArgs moduleArgs;
} FFIconsOptions;
+6
View File
@@ -75,6 +75,12 @@ static bool parseModuleJsonObject(FFinstance* instance, const char* type, json_o
false;
}
case 'I': {
return
tryModule(instance, type, module, FF_ICONS_MODULE_NAME, ffParseIconsJsonObject) ||
false;
}
case 'K': {
return
tryModule(instance, type, module, FF_KERNEL_MODULE_NAME, ffParseKernelJsonObject) ||
+1
View File
@@ -21,6 +21,7 @@
#include "modules/gamepad/gamepad.h"
#include "modules/gpu/gpu.h"
#include "modules/host/host.h"
#include "modules/icons/icons.h"
#include "modules/kernel/kernel.h"
#include "modules/locale/locale.h"
#include "modules/localip/localip.h"
+1
View File
@@ -19,6 +19,7 @@
#include "modules/host/option.h"
#include "modules/gamepad/option.h"
#include "modules/gpu/option.h"
#include "modules/icons/option.h"
#include "modules/kernel/option.h"
#include "modules/locale/option.h"
#include "modules/localip/option.h"