Files
fastfetch/src/modules/theme/theme.c
T

84 lines
2.4 KiB
C
Raw Normal View History

2023-06-09 08:48:46 +08:00
#include "common/printing.h"
2023-06-10 15:01:54 +08:00
#include "common/jsonconfig.h"
2023-06-09 08:48:46 +08:00
#include "detection/theme/theme.h"
#include "modules/theme/theme.h"
#include "util/stringUtils.h"
2023-06-09 08:48:46 +08:00
#define FF_THEME_NUM_FORMAT_ARGS 1
void ffPrintTheme(FFThemeOptions* options)
2023-06-09 08:48:46 +08:00
{
FF_STRBUF_AUTO_DESTROY theme = ffStrbufCreate();
const char* error = ffDetectTheme(&theme);
2023-06-09 08:48:46 +08:00
if(error)
{
ffPrintError(FF_THEME_MODULE_NAME, 0, &options->moduleArgs, "%s", error);
2023-06-09 08:48:46 +08:00
return;
}
if(options->moduleArgs.outputFormat.length == 0)
{
ffPrintLogoAndKey(FF_THEME_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
2023-06-09 08:48:46 +08:00
ffStrbufPutTo(&theme, stdout);
}
else
{
ffPrintFormat(FF_THEME_MODULE_NAME, 0, &options->moduleArgs, FF_THEME_NUM_FORMAT_ARGS, (FFformatarg[]){
2023-06-09 08:48:46 +08:00
{FF_FORMAT_ARG_TYPE_STRBUF, &theme}
});
}
}
void ffInitThemeOptions(FFThemeOptions* options)
{
2023-09-16 20:36:28 +08:00
ffOptionInitModuleBaseInfo(&options->moduleInfo, FF_THEME_MODULE_NAME, ffParseThemeCommandOptions, ffParseThemeJsonObject, ffPrintTheme, ffGenerateThemeJson);
2023-06-09 08:48:46 +08:00
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);
}
2023-08-17 10:56:54 +08:00
void ffParseThemeJsonObject(FFThemeOptions* options, yyjson_val* module)
2023-06-09 08:48:46 +08:00
{
2023-08-17 16:09:41 +08:00
yyjson_val *key_, *val;
size_t idx, max;
yyjson_obj_foreach(module, idx, max, key_, val)
2023-06-09 08:48:46 +08:00
{
2023-08-17 16:09:41 +08:00
const char* key = yyjson_get_str(key_);
if(ffStrEqualsIgnCase(key, "type"))
continue;
2023-06-10 15:01:54 +08:00
2023-08-17 16:09:41 +08:00
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
continue;
2023-06-09 08:48:46 +08:00
2023-08-17 16:09:41 +08:00
ffPrintError(FF_THEME_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key);
2023-06-09 08:48:46 +08:00
}
}
2023-09-12 20:23:58 +08:00
void ffGenerateThemeJson(FF_MAYBE_UNUSED FFThemeOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
{
FF_STRBUF_AUTO_DESTROY theme = ffStrbufCreate();
const char* error = ffDetectTheme(&theme);
if(error)
{
yyjson_mut_obj_add_str(doc, module, "error", error);
return;
}
yyjson_mut_obj_add_strbuf(doc, module, "result", &theme);
}