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

107 lines
3.2 KiB
C
Raw Normal View History

2023-04-28 19:11:55 +08:00
#include "common/printing.h"
2023-06-10 15:01:54 +08:00
#include "common/jsonconfig.h"
2023-04-28 19:11:55 +08:00
#include "detection/wmtheme/wmtheme.h"
#include "modules/wmtheme/wmtheme.h"
#include "util/stringUtils.h"
2023-04-28 19:11:55 +08:00
#define FF_WMTHEME_DISPLAY_NAME "WM Theme"
#define FF_WMTHEME_NUM_FORMAT_ARGS 1
void ffPrintWMTheme(FFWMThemeOptions* options)
2023-04-28 19:11:55 +08:00
{
FF_STRBUF_AUTO_DESTROY themeOrError = ffStrbufCreate();
if(ffDetectWmTheme(&themeOrError))
2023-04-28 19:11:55 +08:00
{
if(options->moduleArgs.outputFormat.length == 0)
{
ffPrintLogoAndKey(FF_WMTHEME_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
2023-04-28 19:11:55 +08:00
puts(themeOrError.chars);
}
else
{
ffPrintFormat(FF_WMTHEME_DISPLAY_NAME, 0, &options->moduleArgs, FF_WMTHEME_NUM_FORMAT_ARGS, (FFformatarg[]){
2023-04-28 19:11:55 +08:00
{FF_FORMAT_ARG_TYPE_STRBUF, &themeOrError}
});
}
}
else
{
ffPrintError(FF_WMTHEME_DISPLAY_NAME, 0, &options->moduleArgs, "%*s", themeOrError.length, themeOrError.chars);
2023-04-28 19:11:55 +08:00
}
}
bool ffParseWMThemeCommandOptions(FFWMThemeOptions* options, const char* key, const char* value)
{
const char* subKey = ffOptionTestPrefix(key, FF_WMTHEME_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 ffParseWMThemeJsonObject(FFWMThemeOptions* options, yyjson_val* module)
2023-04-28 19:11:55 +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-04-28 19:11:55 +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-04-28 19:11:55 +08:00
2023-08-17 16:09:41 +08:00
ffPrintError(FF_WMTHEME_DISPLAY_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key);
2023-04-28 19:11:55 +08:00
}
}
2023-09-13 14:25:00 +08:00
2023-10-24 14:41:29 +08:00
void ffGenerateWMThemeJsonConfig(FFWMThemeOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
{
__attribute__((__cleanup__(ffDestroyWMThemeOptions))) FFWMThemeOptions defaultOptions;
ffInitWMThemeOptions(&defaultOptions);
ffJsonConfigGenerateModuleArgsConfig(doc, module, &defaultOptions.moduleArgs, &options->moduleArgs);
}
2023-10-23 10:14:22 +08:00
void ffGenerateWMThemeJsonResult(FF_MAYBE_UNUSED FFWMThemeOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
2023-09-13 14:25:00 +08:00
{
FF_STRBUF_AUTO_DESTROY themeOrError = ffStrbufCreate();
if(!ffDetectWmTheme(&themeOrError))
{
yyjson_mut_obj_add_strbuf(doc, module, "error", &themeOrError);
return;
}
yyjson_mut_obj_add_strbuf(doc, module, "result", &themeOrError);
}
2023-10-07 13:40:40 +08:00
void ffPrintWMthemeHelpFormat(void)
{
ffPrintModuleFormatHelp(FF_WMTHEME_MODULE_NAME, "{1}", FF_WMTHEME_NUM_FORMAT_ARGS, (const char* []) {
"WM theme"
});
}
2023-10-23 13:32:18 +08:00
void ffInitWMThemeOptions(FFWMThemeOptions* options)
{
ffOptionInitModuleBaseInfo(
&options->moduleInfo,
FF_WMTHEME_MODULE_NAME,
"Print current theme of window manager",
ffParseWMThemeCommandOptions,
ffParseWMThemeJsonObject,
ffPrintWMTheme,
ffGenerateWMThemeJsonResult,
ffPrintWMthemeHelpFormat,
2023-10-24 14:41:29 +08:00
ffGenerateWMThemeJsonConfig
);
2023-10-23 13:32:18 +08:00
ffOptionInitModuleArg(&options->moduleArgs);
}
void ffDestroyWMThemeOptions(FFWMThemeOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
}