master ohmu/fastfetch-openbsd / src / modules / custom / custom.c
ohmu  ·  2026-10-15
 1#include "common/printing.h"
 2#include "common/jsonconfig.h"
 3#include "common/textModifier.h"
 4#include "common/strutil.h"
 5#include "modules/custom/custom.h"
 6
 7bool ffPrintCustom(FFCustomOptions* options) {
 8    ffPrintFormat(FF_MODULE_GET_DISPLAY_NAME(Custom), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, 0, ((FFformatarg[]) {}));
 9    return true;
10}
11
12[[gnu::cold]]
13void ffGenerateCustomJsonConfig(FFCustomOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
14    ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
15}
16
17bool ffGenerateCustomJsonResult(FFCustomOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
18    FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate();
19
20    // This module's output *is* its format string, so the JSON result is that string rendered —
21    // for a `lua:` format that means the script's output, not the script. `ffPrintCustom()`
22    // gets the same value through `ffPrintFormat()`. Without this the module had no JSON result at
23    // all, so a script consuming `--format json` could not read a custom line's value.
24    if (!ffParseFormatString(&result, &options->moduleArgs.outputFormat, 0, (FFformatarg[]) {})) {
25        // `yyjson_mut_obj_add_str()` only copies strings that need escaping; for anything else it
26        // stores the pointer as-is. `result` is freed when this function returns, long before the
27        // document is serialised, so the error has to be copied into the document explicitly.
28        yyjson_mut_obj_add_strcpy(doc, module, "error", result.chars);
29        return false;
30    }
31
32    yyjson_mut_obj_add_strbuf(doc, module, "result", &result);
33    return true;
34}
35
36void ffParseCustomJsonObject(FFCustomOptions* options, yyjson_val* module) {
37    yyjson_val *key, *val;
38    size_t idx, max;
39    yyjson_obj_foreach (module, idx, max, key, val) {
40        if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
41            continue;
42        }
43
44        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Custom), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
45    }
46}
47
48void ffInitCustomOptions(FFCustomOptions* options) {
49    ffOptionInitModuleArg(&options->moduleArgs, "");
50    ffStrbufSetStatic(&options->moduleArgs.key, " ");
51}
52
53void ffDestroyCustomOptions(FFCustomOptions* options) {
54    ffOptionDestroyModuleArg(&options->moduleArgs);
55}
56
57FFModuleBaseInfo ffCustomModuleInfo = {
58    .name = "Custom",
59    .description = "Print a custom string, with or without key",
60    .displayName = {
61        .en = "Custom",
62        .ar = "مخصص",
63        .cs = "Vlastní",
64        .de = "Benutzerdefiniert",
65        .es = "Personalizado",
66        .fi = "Mukautettu",
67        .fr = "Personnalisé",
68        .gl = "Personalizado",
69        .he = "מותאם אישית",
70        .id = "Kustom",
71        .it = "Personalizzato",
72        .ja = "カスタム",
73        .ko = "사용자 정의",
74        .pl = "Niestandardowy",
75        .pt = "Personalizado",
76        .ru = "Пользовательский",
77        .tr = "Özel",
78        .uk = "Власний",
79        .vi = "Tùy chỉnh",
80        .zh_CN = "自定义",
81        .zh_TW = "自訂",
82    },
83    .initOptions = (void*) ffInitCustomOptions,
84    .destroyOptions = (void*) ffDestroyCustomOptions,
85    .parseJsonObject = (void*) ffParseCustomJsonObject,
86    .printModule = (void*) ffPrintCustom,
87    .generateJsonResult = (void*) ffGenerateCustomJsonResult,
88    .generateJsonConfig = (void*) ffGenerateCustomJsonConfig,
89};