Theme: add JSON config support

This commit is contained in:
李通洲
2023-06-09 08:48:46 +08:00
committed by 李通洲
parent db98e406c2
commit e3ec136cad
12 changed files with 109 additions and 39 deletions
+1
View File
@@ -133,6 +133,7 @@ static bool parseModuleJsonObject(FFinstance* instance, const char* type, json_o
tryModule(instance, type, module, FF_TERMINAL_MODULE_NAME, ffParseTerminalJsonObject) ||
tryModule(instance, type, module, FF_TERMINALFONT_MODULE_NAME, ffParseTerminalFontJsonObject) ||
tryModule(instance, type, module, FF_TITLE_MODULE_NAME, ffParseTitleJsonObject) ||
tryModule(instance, type, module, FF_THEME_MODULE_NAME, ffParseThemeJsonObject) ||
false;
}
+1
View File
@@ -41,6 +41,7 @@
#include "modules/swap/swap.h"
#include "modules/terminal/terminal.h"
#include "modules/terminalfont/terminalfont.h"
#include "modules/theme/theme.h"
#include "modules/title/title.h"
#include "modules/jsonconfig/jsonconfig.h"
#include "modules/uptime/uptime.h"
+1
View File
@@ -39,6 +39,7 @@
#include "modules/swap/option.h"
#include "modules/terminal/option.h"
#include "modules/terminalfont/option.h"
#include "modules/theme/option.h"
#include "modules/title/option.h"
#include "modules/uptime/option.h"
#include "modules/users/option.h"
-30
View File
@@ -1,30 +0,0 @@
#include "fastfetch.h"
#include "common/printing.h"
#include "detection/theme/theme.h"
#define FF_THEME_MODULE_NAME "Theme"
#define FF_THEME_NUM_FORMAT_ARGS 1
void ffPrintTheme(FFinstance* instance)
{
FF_STRBUF_AUTO_DESTROY theme;
ffStrbufInit(&theme);
const char* error = ffDetectTheme(instance, &theme);
if (error)
{
ffPrintError(instance, FF_THEME_MODULE_NAME, 0, &instance->config.theme, "%s", error);
return;
}
if(instance->config.theme.outputFormat.length == 0)
{
ffPrintLogoAndKey(instance, FF_THEME_MODULE_NAME, 0, &instance->config.theme.key);
ffStrbufPutTo(&theme, stdout);
}
else
{
ffPrintFormat(instance, FF_THEME_MODULE_NAME, 0, &instance->config.theme, FF_THEME_NUM_FORMAT_ARGS, (FFformatarg[]){
{FF_FORMAT_ARG_TYPE_STRBUF, &theme}
});
}
}
+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 FFThemeOptions
{
const char* moduleName;
FFModuleArgs moduleArgs;
} FFThemeOptions;
+72
View File
@@ -0,0 +1,72 @@
#include "fastfetch.h"
#include "common/printing.h"
#include "detection/theme/theme.h"
#include "modules/theme/theme.h"
#define FF_THEME_NUM_FORMAT_ARGS 1
void ffPrintTheme(FFinstance* instance, FFThemeOptions* options)
{
FF_STRBUF_AUTO_DESTROY theme = ffStrbufCreate();
const char* error = ffDetectTheme(instance, &theme);
if(error)
{
ffPrintError(instance, FF_THEME_MODULE_NAME, 0, &options->moduleArgs, "%s", error);
return;
}
if(options->moduleArgs.outputFormat.length == 0)
{
ffPrintLogoAndKey(instance, FF_THEME_MODULE_NAME, 0, &options->moduleArgs.key);
ffStrbufPutTo(&theme, stdout);
}
else
{
ffPrintFormat(instance, FF_THEME_MODULE_NAME, 0, &options->moduleArgs, FF_THEME_NUM_FORMAT_ARGS, (FFformatarg[]){
{FF_FORMAT_ARG_TYPE_STRBUF, &theme}
});
}
}
void ffInitThemeOptions(FFThemeOptions* options)
{
options->moduleName = FF_THEME_MODULE_NAME;
ffOptionInitModuleArg(&options->moduleArgs);
}
bool ffParseThemeCommandOptions(FFThemeOptions* options, const char* key, const char* value)
{
const char* subKey = ffOptionTestPrefix(key, FF_THEME_MODULE_NAME);
if (!subKey) return false;
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
return true;
return false;
}
void ffDestroyThemeOptions(FFThemeOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
}
#ifdef FF_HAVE_JSONC
void ffParseThemeJsonObject(FFinstance* instance, json_object* module)
{
FFThemeOptions __attribute__((__cleanup__(ffDestroyThemeOptions))) options;
ffInitThemeOptions(&options);
if (module)
{
json_object_object_foreach(module, key, val)
{
if (ffJsonConfigParseModuleArgs(key, val, &options.moduleArgs))
continue;
ffPrintError(instance, FF_THEME_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key);
}
}
ffPrintTheme(instance, &options);
}
#endif
+15
View File
@@ -0,0 +1,15 @@
#pragma once
#include "fastfetch.h"
#define FF_THEME_MODULE_NAME "Theme"
void ffPrintTheme(FFinstance* instance, FFThemeOptions* options);
void ffInitThemeOptions(FFThemeOptions* options);
bool ffParseThemeCommandOptions(FFThemeOptions* options, const char* key, const char* value);
void ffDestroyThemeOptions(FFThemeOptions* options);
#ifdef FF_HAVE_JSONC
#include "common/jsonconfig.h"
void ffParseThemeJsonObject(FFinstance* instance, json_object* module);
#endif