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

133 lines
3.9 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 2
2023-06-09 08:48:46 +08:00
void ffPrintTheme(FFThemeOptions* options)
2023-06-09 08:48:46 +08:00
{
FFThemeResult result = {
.theme1 = ffStrbufCreate(),
.theme2 = ffStrbufCreate()
};
const char* error = ffDetectTheme(&result);
2023-06-09 08:48:46 +08:00
if(error)
{
ffPrintError(FF_THEME_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%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);
if (result.theme1.length)
ffStrbufWriteTo(&result.theme1, stdout);
if (result.theme2.length)
{
if (result.theme1.length)
fputs(", ", stdout);
ffStrbufWriteTo(&result.theme2, stdout);
}
putchar('\n');
2023-06-09 08:48:46 +08:00
}
else
{
FF_PRINT_FORMAT_CHECKED(FF_THEME_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, FF_THEME_NUM_FORMAT_ARGS, ((FFformatarg[]){
{FF_FORMAT_ARG_TYPE_STRBUF, &result.theme1, "theme1"},
{FF_FORMAT_ARG_TYPE_STRBUF, &result.theme2, "theme2"},
}));
2023-06-09 08:48:46 +08:00
}
2024-07-05 23:27:13 +08:00
ffStrbufDestroy(&result.theme1);
ffStrbufDestroy(&result.theme2);
2023-06-09 08:48:46 +08:00
}
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;
}
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
ffPrintError(FF_THEME_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", key);
2023-06-09 08:48:46 +08:00
}
}
2023-09-12 20:23:58 +08:00
2023-10-24 14:41:29 +08:00
void ffGenerateThemeJsonConfig(FFThemeOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
{
__attribute__((__cleanup__(ffDestroyThemeOptions))) FFThemeOptions defaultOptions;
ffInitThemeOptions(&defaultOptions);
ffJsonConfigGenerateModuleArgsConfig(doc, module, &defaultOptions.moduleArgs, &options->moduleArgs);
}
2023-10-23 10:14:22 +08:00
void ffGenerateThemeJsonResult(FF_MAYBE_UNUSED FFThemeOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
2023-09-12 20:23:58 +08:00
{
FFThemeResult result = {
.theme1 = ffStrbufCreate(),
.theme2 = ffStrbufCreate()
};
const char* error = ffDetectTheme(&result);
2023-09-12 20:23:58 +08:00
if(error)
{
yyjson_mut_obj_add_str(doc, module, "error", error);
return;
}
yyjson_mut_val* theme = yyjson_mut_obj_add_obj(doc, module, "result");
yyjson_mut_obj_add_strbuf(doc, theme, "theme1", &result.theme1);
yyjson_mut_obj_add_strbuf(doc, theme, "theme2", &result.theme2);
2024-07-05 23:27:13 +08:00
ffStrbufDestroy(&result.theme1);
ffStrbufDestroy(&result.theme2);
2023-09-12 20:23:58 +08:00
}
2023-10-07 13:40:40 +08:00
void ffPrintThemeHelpFormat(void)
{
FF_PRINT_MODULE_FORMAT_HELP_CHECKED(FF_THEME_MODULE_NAME, "{1}, {2}", FF_THEME_NUM_FORMAT_ARGS, ((const char* []) {
"Theme part 1 - theme1",
"Theme part 2 - theme2",
}));
2023-10-07 13:40:40 +08:00
}
2023-10-23 13:32:18 +08:00
void ffInitThemeOptions(FFThemeOptions* options)
{
ffOptionInitModuleBaseInfo(
&options->moduleInfo,
FF_THEME_MODULE_NAME,
"Print current theme of desktop environment",
ffParseThemeCommandOptions,
ffParseThemeJsonObject,
ffPrintTheme,
ffGenerateThemeJsonResult,
ffPrintThemeHelpFormat,
2023-10-24 14:41:29 +08:00
ffGenerateThemeJsonConfig
);
2024-07-23 23:02:35 +08:00
ffOptionInitModuleArg(&options->moduleArgs, "󰉼");
2023-10-23 13:32:18 +08:00
}
void ffDestroyThemeOptions(FFThemeOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
}