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

127 lines
4.2 KiB
C
Raw Normal View History

2026-01-06 09:48:38 +08:00
#include "common/printing.h"
#include "common/jsonconfig.h"
2026-05-28 15:58:08 +08:00
#include "common/strutil.h"
2023-06-09 08:48:46 +08:00
#include "detection/theme/theme.h"
#include "modules/theme/theme.h"
2026-03-29 09:28:49 +08:00
bool ffPrintTheme(FFThemeOptions* options) {
FFThemeResult result = {
.theme1 = ffStrbufCreate(),
.theme2 = ffStrbufCreate()
};
const char* error = ffDetectTheme(&result);
2023-06-09 08:48:46 +08:00
2026-03-29 09:28:49 +08:00
if (error) {
2026-08-14 23:33:54 +08:00
ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Theme), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
return false;
2023-06-09 08:48:46 +08:00
}
2026-03-29 09:28:49 +08:00
if (options->moduleArgs.outputFormat.length == 0) {
2026-08-14 23:33:54 +08:00
ffPrintLogoAndKey(FF_MODULE_GET_DISPLAY_NAME(Theme), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
2026-03-29 09:28:49 +08:00
if (result.theme1.length) {
ffStrbufWriteTo(&result.theme1, stdout);
2026-03-29 09:28:49 +08:00
}
if (result.theme2.length) {
if (result.theme1.length) {
fputs(", ", stdout);
2026-03-29 09:28:49 +08:00
}
ffStrbufWriteTo(&result.theme2, stdout);
}
putchar('\n');
2026-03-29 09:28:49 +08:00
} else {
2026-08-14 23:33:54 +08:00
FF_PRINT_FORMAT_CHECKED(FF_MODULE_GET_DISPLAY_NAME(Theme), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
2026-03-29 09:28:49 +08:00
FF_ARG(result.theme1, "theme1"),
FF_ARG(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);
return true;
2023-06-09 08:48:46 +08:00
}
2026-03-29 09:28:49 +08:00
void ffParseThemeJsonObject(FFThemeOptions* options, yyjson_val* module) {
yyjson_val *key, *val;
2023-08-17 16:09:41 +08:00
size_t idx, max;
2026-03-29 09:28:49 +08:00
yyjson_obj_foreach (module, idx, max, key, val) {
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
2023-08-17 16:09:41 +08:00
continue;
2026-03-29 09:28:49 +08:00
}
2023-06-09 08:48:46 +08:00
2026-08-14 23:33:54 +08:00
ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Theme), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
2023-06-09 08:48:46 +08:00
}
}
2023-09-12 20:23:58 +08:00
[[gnu::cold]]
2026-03-29 09:28:49 +08:00
void ffGenerateThemeJsonConfig(FFThemeOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
2023-10-24 14:41:29 +08:00
}
bool ffGenerateThemeJsonResult([[maybe_unused]] FFThemeOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
FFThemeResult result = {
.theme1 = ffStrbufCreate(),
.theme2 = ffStrbufCreate()
};
const char* error = ffDetectTheme(&result);
2023-09-12 20:23:58 +08:00
2026-03-29 09:28:49 +08:00
if (error) {
2023-09-12 20:23:58 +08:00
yyjson_mut_obj_add_str(doc, module, "error", error);
return false;
2023-09-12 20:23:58 +08:00
}
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);
return true;
2023-09-12 20:23:58 +08:00
}
2023-10-07 13:40:40 +08:00
2026-03-29 09:28:49 +08:00
void ffInitThemeOptions(FFThemeOptions* options) {
ffOptionInitModuleArg(&options->moduleArgs, "󰉼");
}
2026-03-29 09:28:49 +08:00
void ffDestroyThemeOptions(FFThemeOptions* options) {
ffOptionDestroyModuleArg(&options->moduleArgs);
}
2025-08-03 22:50:56 +08:00
FFModuleBaseInfo ffThemeModuleInfo = {
2026-08-14 23:33:54 +08:00
.name = "Theme",
2026-04-16 18:27:11 +08:00
.description = "Print the current desktop environment theme",
2026-08-14 20:38:58 +08:00
.displayName = {
.en = "Theme",
2026-08-16 10:51:42 +08:00
.ar = "السمة",
2026-08-21 10:19:06 +08:00
.cs = "Motiv",
2026-08-14 20:38:58 +08:00
.de = "Thema",
.es = "Tema",
.fr = "Thème",
2026-08-20 16:25:32 +00:00
.gl = "Tema",
2026-08-16 10:51:42 +08:00
.he = "ערכת נושא",
2026-08-21 10:19:06 +08:00
.id = "Tema",
2026-08-14 20:38:58 +08:00
.it = "Tema",
.ja = "テーマ",
.ko = "테마",
.pl = "Motyw",
2026-08-16 10:37:06 +08:00
.pt = "Tema",
2026-08-14 20:38:58 +08:00
.ru = "Тема",
2026-08-21 10:19:06 +08:00
.tr = "Tema",
.uk = "Тема",
.vi = "Chủ đề",
2026-08-14 20:38:58 +08:00
.zh_CN = "主题",
.zh_TW = "主題",
},
.initOptions = (void*) ffInitThemeOptions,
.destroyOptions = (void*) ffDestroyThemeOptions,
2024-12-11 10:47:02 +08:00
.parseJsonObject = (void*) ffParseThemeJsonObject,
.printModule = (void*) ffPrintTheme,
.generateJsonResult = (void*) ffGenerateThemeJsonResult,
.generateJsonConfig = (void*) ffGenerateThemeJsonConfig,
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
2026-03-30 10:27:23 +08:00
{ "Theme part 1", "theme1" },
{ "Theme part 2", "theme2" },
})),
.defaultOrder = 24,
};