Locale: add JSON config support

This commit is contained in:
李通洲
2023-04-04 17:47:42 +08:00
parent 3526092b38
commit 0b96818f6d
12 changed files with 115 additions and 42 deletions
+6
View File
@@ -81,6 +81,12 @@ static bool parseModuleJsonObject(FFinstance* instance, const char* type, json_o
false;
}
case 'L': {
return
tryModule(instance, type, module, FF_LOCALE_MODULE_NAME, ffParseLocaleJsonObject) ||
false;
}
case 'O': {
return
tryModule(instance, type, module, FF_OS_MODULE_NAME, ffParseOSJsonObject) ||
-33
View File
@@ -1,33 +0,0 @@
#include "fastfetch.h"
#include "common/printing.h"
#include "detection/locale/locale.h"
#define FF_LOCALE_MODULE_NAME "Locale"
#define FF_LOCALE_NUM_FORMAT_ARGS 1
void ffPrintLocale(FFinstance* instance)
{
FFstrbuf locale;
ffStrbufInit(&locale);
ffDetectLocale(&locale);
if(locale.length == 0)
{
ffPrintError(instance, FF_LOCALE_MODULE_NAME, 0, &instance->config.locale, "No locale found");
return;
}
if(instance->config.locale.outputFormat.length == 0)
{
ffPrintLogoAndKey(instance, FF_LOCALE_MODULE_NAME, 0, &instance->config.locale.key);
ffStrbufPutTo(&locale, stdout);
}
else
{
ffPrintFormat(instance, FF_LOCALE_MODULE_NAME, 0, &instance->config.locale, FF_LOCALE_NUM_FORMAT_ARGS, (FFformatarg[]){
{FF_FORMAT_ARG_TYPE_STRBUF, &locale}
});
}
ffStrbufDestroy(&locale);
}
+73
View File
@@ -0,0 +1,73 @@
#include "fastfetch.h"
#include "common/printing.h"
#include "detection/locale/locale.h"
#include "modules/locale/locale.h"
#define FF_LOCALE_NUM_FORMAT_ARGS 1
void ffPrintLocale(FFinstance* instance, FFLocaleOptions* options)
{
FF_STRBUF_AUTO_DESTROY locale;
ffStrbufInit(&locale);
ffDetectLocale(&locale);
if(locale.length == 0)
{
ffPrintError(instance, FF_LOCALE_MODULE_NAME, 0, &options->moduleArgs, "No locale found");
return;
}
if(options->moduleArgs.outputFormat.length == 0)
{
ffPrintLogoAndKey(instance, FF_LOCALE_MODULE_NAME, 0, &options->moduleArgs.key);
ffStrbufPutTo(&locale, stdout);
}
else
{
ffPrintFormat(instance, FF_LOCALE_MODULE_NAME, 0, &options->moduleArgs, FF_LOCALE_NUM_FORMAT_ARGS, (FFformatarg[]){
{FF_FORMAT_ARG_TYPE_STRBUF, &locale}
});
}
}
void ffInitLocaleOptions(FFLocaleOptions* options)
{
options->moduleName = FF_LOCALE_MODULE_NAME;
ffOptionInitModuleArg(&options->moduleArgs);
}
bool ffParseLocaleCommandOptions(FFLocaleOptions* options, const char* key, const char* value)
{
const char* subKey = ffOptionTestPrefix(key, FF_LOCALE_MODULE_NAME);
if (!subKey) return false;
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
return true;
return false;
}
void ffDestroyLocaleOptions(FFLocaleOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
}
#ifdef FF_HAVE_JSONC
void ffParseLocaleJsonObject(FFinstance* instance, json_object* module)
{
FFLocaleOptions __attribute__((__cleanup__(ffDestroyLocaleOptions))) options;
ffInitLocaleOptions(&options);
if (module)
{
json_object_object_foreach(module, key, val)
{
if (ffJsonConfigParseModuleArgs(key, val, &options.moduleArgs))
continue;
ffPrintError(instance, FF_LOCALE_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key);
}
}
ffPrintLocale(instance, &options);
}
#endif
+15
View File
@@ -0,0 +1,15 @@
#pragma once
#include "fastfetch.h"
#define FF_LOCALE_MODULE_NAME "Locale"
void ffPrintLocale(FFinstance* instance, FFLocaleOptions* options);
void ffInitLocaleOptions(FFLocaleOptions* options);
bool ffParseLocaleCommandOptions(FFLocaleOptions* options, const char* key, const char* value);
void ffDestroyLocaleOptions(FFLocaleOptions* options);
#ifdef FF_HAVE_JSONC
#include "common/jsonconfig.h"
void ffParseLocaleJsonObject(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 FFLocaleOptions
{
const char* moduleName;
FFModuleArgs moduleArgs;
} FFLocaleOptions;
+1
View File
@@ -23,6 +23,7 @@
#include "modules/gpu/gpu.h"
#include "modules/host/host.h"
#include "modules/kernel/kernel.h"
#include "modules/locale/locale.h"
#include "modules/separator/separator.h"
#include "modules/title/title.h"
#include "modules/jsonconfig/jsonconfig.h"
+1
View File
@@ -20,6 +20,7 @@
#include "modules/gamepad/option.h"
#include "modules/gpu/option.h"
#include "modules/kernel/option.h"
#include "modules/locale/option.h"
#include "modules/os/option.h"
#include "modules/separator/option.h"
#include "modules/title/option.h"