master ohmu/fastfetch-openbsd / src / modules / icons / icons.c
ohmu  ·  2026-10-15
  1#include "common/printing.h"
  2#include "common/jsonconfig.h"
  3#include "common/strutil.h"
  4#include "detection/icons/icons.h"
  5#include "modules/icons/icons.h"
  6
  7bool ffPrintIcons(FFIconsOptions* options) {
  8    bool success = false;
  9    FFIconsResult result = {
 10        .icons1 = ffStrbufCreate(),
 11        .icons2 = ffStrbufCreate(),
 12    };
 13    const char* error = ffDetectIcons(&result);
 14
 15    if (error) {
 16        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Icons), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
 17        goto exit;
 18    }
 19
 20    if (options->moduleArgs.outputFormat.length == 0) {
 21        ffPrintLogoAndKey(FF_MODULE_GET_DISPLAY_NAME(Icons), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
 22        if (result.icons1.length) {
 23            ffStrbufWriteTo(&result.icons1, stdout);
 24        }
 25        if (result.icons2.length) {
 26            if (result.icons1.length) {
 27                fputs(", ", stdout);
 28            }
 29            ffStrbufWriteTo(&result.icons2, stdout);
 30        }
 31        putchar('\n');
 32    } else {
 33        FF_PRINT_FORMAT_CHECKED(FF_MODULE_GET_DISPLAY_NAME(Icons), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
 34                                                                                                          FF_ARG(result.icons1, "icons1"),
 35                                                                                                          FF_ARG(result.icons2, "icons2"),
 36                                                                                                      }));
 37    }
 38    success = true;
 39
 40exit:
 41    ffStrbufDestroy(&result.icons1);
 42    ffStrbufDestroy(&result.icons2);
 43
 44    return success;
 45}
 46
 47void ffParseIconsJsonObject(FFIconsOptions* options, yyjson_val* module) {
 48    yyjson_val *key, *val;
 49    size_t idx, max;
 50    yyjson_obj_foreach (module, idx, max, key, val) {
 51        if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
 52            continue;
 53        }
 54
 55        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Icons), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
 56    }
 57}
 58
 59[[gnu::cold]]
 60void ffGenerateIconsJsonConfig(FFIconsOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
 61    ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
 62}
 63
 64bool ffGenerateIconsJsonResult([[maybe_unused]] FFIconsOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
 65    bool success = false;
 66    FFIconsResult result = {
 67        .icons1 = ffStrbufCreate(),
 68        .icons2 = ffStrbufCreate()
 69    };
 70    const char* error = ffDetectIcons(&result);
 71
 72    if (error) {
 73        yyjson_mut_obj_add_str(doc, module, "error", error);
 74        goto exit;
 75    }
 76
 77    yyjson_mut_val* icons = yyjson_mut_obj_add_obj(doc, module, "result");
 78    yyjson_mut_obj_add_strbuf(doc, icons, "icons1", &result.icons1);
 79    yyjson_mut_obj_add_strbuf(doc, icons, "icons2", &result.icons2);
 80    success = true;
 81
 82exit:
 83    ffStrbufDestroy(&result.icons1);
 84    ffStrbufDestroy(&result.icons2);
 85
 86    return success;
 87}
 88
 89void ffInitIconsOptions(FFIconsOptions* options) {
 90    ffOptionInitModuleArg(&options->moduleArgs, "");
 91}
 92
 93void ffDestroyIconsOptions(FFIconsOptions* options) {
 94    ffOptionDestroyModuleArg(&options->moduleArgs);
 95}
 96
 97FFModuleBaseInfo ffIconsModuleInfo = {
 98    .name = "Icons",
 99    .description = "Print icon style name",
100    .displayName = {
101        .en = "Icons",
102        .ar = "الأيقونات",
103        .cs = "Ikony",
104        .de = "Symbole",
105        .es = "Iconos",
106        .fi = "Kuvakkeet",
107        .fr = "Icônes",
108        .gl = "Iconas",
109        .he = "סמלים",
110        .id = "Ikon",
111        .it = "Icone",
112        .ja = "アイコン",
113        .ko = "아이콘",
114        .pl = "Ikony",
115        .pt = "Ícones",
116        .ru = "Иконки",
117        .tr = "Simgeler",
118        .uk = "Піктограми",
119        .vi = "Biểu tượng",
120        .zh_CN = "图标",
121        .zh_TW = "圖示",
122    },
123    .initOptions = (void*) ffInitIconsOptions,
124    .destroyOptions = (void*) ffDestroyIconsOptions,
125    .parseJsonObject = (void*) ffParseIconsJsonObject,
126    .printModule = (void*) ffPrintIcons,
127    .generateJsonResult = (void*) ffGenerateIconsJsonResult,
128    .generateJsonConfig = (void*) ffGenerateIconsJsonConfig,
129    .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
130        { "Icons part 1", "icons1" },
131        { "Icons part 2", "icons2" },
132    })),
133    .defaultOrder = 25,
134};