ohmu
·
2026-10-15
1#include "common/printing.h"
2#include "common/jsonconfig.h"
3#include "common/strutil.h"
4#include "detection/terminaltheme/terminaltheme.h"
5#include "modules/terminaltheme/terminaltheme.h"
6
7#include <inttypes.h>
8
9bool ffPrintTerminalTheme(FFTerminalThemeOptions* options) {
10 FFTerminalThemeResult result = {};
11
12 if (!ffDetectTerminalTheme(&result, false)) {
13 ffPrintError(FF_MODULE_GET_DISPLAY_NAME(TerminalTheme), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Failed to detect terminal theme");
14 return false;
15 }
16
17 if (options->moduleArgs.outputFormat.length == 0) {
18 ffPrintLogoAndKey(FF_MODULE_GET_DISPLAY_NAME(TerminalTheme), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
19 printf("#%02" PRIX16 "%02" PRIX16 "%02" PRIX16 " (FG) - #%02" PRIX16 "%02" PRIX16 "%02" PRIX16 " (BG) [%s]\n",
20 result.fg.r,
21 result.fg.g,
22 result.fg.b,
23 result.bg.r,
24 result.bg.g,
25 result.bg.b,
26 result.bg.dark ? "Dark" : "Light");
27 } else {
28 char fg[32], bg[32];
29 const char* fgType = result.fg.dark ? "Dark" : "Light";
30 const char* bgType = result.bg.dark ? "Dark" : "Light";
31 snprintf(fg, ARRAY_SIZE(fg), "#%02" PRIX16 "%02" PRIX16 "%02" PRIX16, result.fg.r, result.fg.g, result.fg.b);
32 snprintf(bg, ARRAY_SIZE(bg), "#%02" PRIX16 "%02" PRIX16 "%02" PRIX16, result.bg.r, result.bg.g, result.bg.b);
33 FF_PRINT_FORMAT_CHECKED(FF_MODULE_GET_DISPLAY_NAME(TerminalTheme), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
34 FF_ARG(fg, "fg-color"),
35 FF_ARG(fgType, "fg-type"),
36 FF_ARG(bg, "bg-color"),
37 FF_ARG(bgType, "bg-type"),
38 }));
39 }
40
41 return true;
42}
43
44void ffParseTerminalThemeJsonObject(FFTerminalThemeOptions* options, yyjson_val* module) {
45 yyjson_val *key, *val;
46 size_t idx, max;
47 yyjson_obj_foreach (module, idx, max, key, val) {
48 if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
49 continue;
50 }
51
52 ffPrintError(FF_MODULE_GET_DISPLAY_NAME(TerminalTheme), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
53 }
54}
55
56[[gnu::cold]]
57void ffGenerateTerminalThemeJsonConfig(FFTerminalThemeOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
58 ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
59}
60
61bool ffGenerateTerminalThemeJsonResult([[maybe_unused]] FFTerminalThemeOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
62 FFTerminalThemeResult result = {};
63
64 if (!ffDetectTerminalTheme(&result, false)) {
65 yyjson_mut_obj_add_str(doc, module, "error", "Failed to detect terminal theme");
66 return false;
67 }
68
69 yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
70
71 yyjson_mut_val* fg = yyjson_mut_obj_add_obj(doc, obj, "fg");
72 yyjson_mut_obj_add_uint(doc, fg, "r", result.fg.r);
73 yyjson_mut_obj_add_uint(doc, fg, "g", result.fg.g);
74 yyjson_mut_obj_add_uint(doc, fg, "b", result.fg.b);
75 yyjson_mut_obj_add_bool(doc, fg, "dark", result.fg.dark);
76
77 yyjson_mut_val* bg = yyjson_mut_obj_add_obj(doc, obj, "bg");
78 yyjson_mut_obj_add_uint(doc, bg, "r", result.bg.r);
79 yyjson_mut_obj_add_uint(doc, bg, "g", result.bg.g);
80 yyjson_mut_obj_add_uint(doc, bg, "b", result.bg.b);
81 yyjson_mut_obj_add_bool(doc, bg, "dark", result.bg.dark);
82
83 return true;
84}
85
86void ffInitTerminalThemeOptions(FFTerminalThemeOptions* options) {
87 ffOptionInitModuleArg(&options->moduleArgs, "");
88}
89
90void ffDestroyTerminalThemeOptions(FFTerminalThemeOptions* options) {
91 ffOptionDestroyModuleArg(&options->moduleArgs);
92}
93
94FFModuleBaseInfo ffTerminalThemeModuleInfo = {
95 .name = "TerminalTheme",
96 .description = "Print the current terminal theme (foreground and background colors)",
97 .displayName = {
98 .en = "Terminal Theme",
99 .ar = "سمة الطرفية",
100 .cs = "Motiv terminálu",
101 .de = "Terminalthema",
102 .es = "Tema del terminal",
103 .fi = "Terminaalin Teema",
104 .fr = "Thème du terminal",
105 .gl = "Tema do terminal",
106 .he = "ערכת נושא של טרמינל",
107 .id = "Tema Terminal",
108 .it = "Tema del terminale",
109 .ja = "ターミナルテーマ",
110 .ko = "터미널 테마",
111 .pl = "Motyw terminala",
112 .pt = "Tema do terminal",
113 .ru = "Тема терминала",
114 .tr = "Terminal Teması",
115 .uk = "Тема терміналу",
116 .vi = "Chủ đề Terminal",
117 .zh_CN = "终端主题",
118 .zh_TW = "終端機主題",
119 },
120 .initOptions = (void*) ffInitTerminalThemeOptions,
121 .destroyOptions = (void*) ffDestroyTerminalThemeOptions,
122 .parseJsonObject = (void*) ffParseTerminalThemeJsonObject,
123 .printModule = (void*) ffPrintTerminalTheme,
124 .generateJsonResult = (void*) ffGenerateTerminalThemeJsonResult,
125 .generateJsonConfig = (void*) ffGenerateTerminalThemeJsonConfig,
126 .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
127 { "Terminal foreground color", "fg-color" },
128 { "Terminal foreground type (Dark / Light)", "fg-type" },
129 { "Terminal background color", "bg-color" },
130 { "Terminal background type (Dark / Light)", "bg-type" },
131 })),
132 .defaultOrder = 32,
133};