master ohmu/fastfetch-openbsd / src / modules / theme / theme.c
ohmu  ·  2026-10-15
  1#include "common/printing.h"
  2#include "common/jsonconfig.h"
  3#include "common/strutil.h"
  4#include "detection/theme/theme.h"
  5#include "modules/theme/theme.h"
  6
  7bool ffPrintTheme(FFThemeOptions* options) {
  8    FFThemeResult result = {
  9        .theme1 = ffStrbufCreate(),
 10        .theme2 = ffStrbufCreate()
 11    };
 12    const char* error = ffDetectTheme(&result);
 13
 14    if (error) {
 15        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Theme), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
 16        return false;
 17    }
 18
 19    if (options->moduleArgs.outputFormat.length == 0) {
 20        ffPrintLogoAndKey(FF_MODULE_GET_DISPLAY_NAME(Theme), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
 21        if (result.theme1.length) {
 22            ffStrbufWriteTo(&result.theme1, stdout);
 23        }
 24        if (result.theme2.length) {
 25            if (result.theme1.length) {
 26                fputs(", ", stdout);
 27            }
 28            ffStrbufWriteTo(&result.theme2, stdout);
 29        }
 30        putchar('\n');
 31    } else {
 32        FF_PRINT_FORMAT_CHECKED(FF_MODULE_GET_DISPLAY_NAME(Theme), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
 33                                                                                                          FF_ARG(result.theme1, "theme1"),
 34                                                                                                          FF_ARG(result.theme2, "theme2"),
 35                                                                                                      }));
 36    }
 37
 38    ffStrbufDestroy(&result.theme1);
 39    ffStrbufDestroy(&result.theme2);
 40    return true;
 41}
 42
 43void ffParseThemeJsonObject(FFThemeOptions* options, yyjson_val* module) {
 44    yyjson_val *key, *val;
 45    size_t idx, max;
 46    yyjson_obj_foreach (module, idx, max, key, val) {
 47        if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
 48            continue;
 49        }
 50
 51        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Theme), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
 52    }
 53}
 54
 55[[gnu::cold]]
 56void ffGenerateThemeJsonConfig(FFThemeOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
 57    ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
 58}
 59
 60bool ffGenerateThemeJsonResult([[maybe_unused]] FFThemeOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
 61    FFThemeResult result = {
 62        .theme1 = ffStrbufCreate(),
 63        .theme2 = ffStrbufCreate()
 64    };
 65    const char* error = ffDetectTheme(&result);
 66
 67    if (error) {
 68        yyjson_mut_obj_add_str(doc, module, "error", error);
 69        return false;
 70    }
 71
 72    yyjson_mut_val* theme = yyjson_mut_obj_add_obj(doc, module, "result");
 73    yyjson_mut_obj_add_strbuf(doc, theme, "theme1", &result.theme1);
 74    yyjson_mut_obj_add_strbuf(doc, theme, "theme2", &result.theme2);
 75
 76    ffStrbufDestroy(&result.theme1);
 77    ffStrbufDestroy(&result.theme2);
 78
 79    return true;
 80}
 81
 82void ffInitThemeOptions(FFThemeOptions* options) {
 83    ffOptionInitModuleArg(&options->moduleArgs, "󰉼");
 84}
 85
 86void ffDestroyThemeOptions(FFThemeOptions* options) {
 87    ffOptionDestroyModuleArg(&options->moduleArgs);
 88}
 89
 90FFModuleBaseInfo ffThemeModuleInfo = {
 91    .name = "Theme",
 92    .description = "Print the current desktop environment theme",
 93    .displayName = {
 94        .en = "Theme",
 95        .ar = "السمة",
 96        .cs = "Motiv",
 97        .de = "Thema",
 98        .es = "Tema",
 99        .fi = "Teema",
100        .fr = "Thème",
101        .gl = "Tema",
102        .he = "ערכת נושא",
103        .id = "Tema",
104        .it = "Tema",
105        .ja = "テーマ",
106        .ko = "테마",
107        .pl = "Motyw",
108        .pt = "Tema",
109        .ru = "Тема",
110        .tr = "Tema",
111        .uk = "Тема",
112        .vi = "Chủ đề",
113        .zh_CN = "主题",
114        .zh_TW = "主題",
115    },
116    .initOptions = (void*) ffInitThemeOptions,
117    .destroyOptions = (void*) ffDestroyThemeOptions,
118    .parseJsonObject = (void*) ffParseThemeJsonObject,
119    .printModule = (void*) ffPrintTheme,
120    .generateJsonResult = (void*) ffGenerateThemeJsonResult,
121    .generateJsonConfig = (void*) ffGenerateThemeJsonConfig,
122    .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
123        { "Theme part 1", "theme1" },
124        { "Theme part 2", "theme2" },
125    })),
126    .defaultOrder = 24,
127};