ohmu
·
2026-10-15
1#include "common/printing.h"
2#include "common/jsonconfig.h"
3#include "common/strutil.h"
4#include "detection/terminalfont/terminalfont.h"
5#include "modules/terminalfont/terminalfont.h"
6
7bool ffPrintTerminalFont(FFTerminalFontOptions* options) {
8 bool success = false;
9 FFTerminalFontResult terminalFont;
10 ffFontInit(&terminalFont.font);
11 ffFontInit(&terminalFont.fallback);
12 ffStrbufInit(&terminalFont.error);
13
14 if (!ffDetectTerminalFont(&terminalFont)) {
15 ffPrintError(FF_MODULE_GET_DISPLAY_NAME(TerminalFont), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", terminalFont.error.chars);
16 } else {
17 if (options->moduleArgs.outputFormat.length == 0) {
18 ffPrintLogoAndKey(FF_MODULE_GET_DISPLAY_NAME(TerminalFont), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
19 ffStrbufWriteTo(&terminalFont.font.pretty, stdout);
20 if (terminalFont.fallback.pretty.length) {
21 fputs(" / ", stdout);
22 ffStrbufWriteTo(&terminalFont.fallback.pretty, stdout);
23 }
24 putchar('\n');
25 } else {
26 FF_PRINT_FORMAT_CHECKED(FF_MODULE_GET_DISPLAY_NAME(TerminalFont), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
27 FF_ARG(terminalFont.font.pretty, "combined"),
28 FF_ARG(terminalFont.font.name, "name"),
29 FF_ARG(terminalFont.font.size, "size"),
30 FF_ARG(terminalFont.font.styles, "styles"),
31 }));
32 }
33 success = true;
34 }
35
36 ffStrbufDestroy(&terminalFont.error);
37 ffFontDestroy(&terminalFont.font);
38 ffFontDestroy(&terminalFont.fallback);
39
40 return success;
41}
42
43void ffParseTerminalFontJsonObject(FFTerminalFontOptions* 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(TerminalFont), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
52 }
53}
54
55[[gnu::cold]]
56void ffGenerateTerminalFontJsonConfig(FFTerminalFontOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
57 ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
58}
59
60bool ffGenerateTerminalFontJsonResult([[maybe_unused]] FFTerminalFontOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
61 bool success = false;
62 FFTerminalFontResult result;
63 ffFontInit(&result.font);
64 ffFontInit(&result.fallback);
65 ffStrbufInit(&result.error);
66
67 if (!ffDetectTerminalFont(&result)) {
68 yyjson_mut_obj_add_strbuf(doc, module, "error", &result.error);
69 } else {
70 yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
71
72 yyjson_mut_val* font = yyjson_mut_obj_add_obj(doc, obj, "font");
73 yyjson_mut_obj_add_strbuf(doc, font, "name", &result.font.name);
74 yyjson_mut_obj_add_strbuf(doc, font, "size", &result.font.size);
75 yyjson_mut_val* fontStyles = yyjson_mut_obj_add_arr(doc, font, "styles");
76 FF_LIST_FOR_EACH (FFstrbuf, style, result.font.styles) {
77 yyjson_mut_arr_add_strbuf(doc, fontStyles, style);
78 }
79 yyjson_mut_obj_add_strbuf(doc, font, "pretty", &result.font.pretty);
80
81 yyjson_mut_val* fallback = yyjson_mut_obj_add_obj(doc, obj, "fallback");
82 yyjson_mut_obj_add_strbuf(doc, fallback, "name", &result.fallback.name);
83 yyjson_mut_obj_add_strbuf(doc, fallback, "size", &result.fallback.size);
84 yyjson_mut_val* fallbackStyles = yyjson_mut_obj_add_arr(doc, fallback, "styles");
85 FF_LIST_FOR_EACH (FFstrbuf, style, result.fallback.styles) {
86 yyjson_mut_arr_add_strbuf(doc, fallbackStyles, style);
87 }
88 yyjson_mut_obj_add_strbuf(doc, fallback, "pretty", &result.fallback.pretty);
89 success = true;
90 }
91
92 ffStrbufDestroy(&result.error);
93 ffFontDestroy(&result.font);
94 ffFontDestroy(&result.fallback);
95 return success;
96}
97
98void ffInitTerminalFontOptions(FFTerminalFontOptions* options) {
99 ffOptionInitModuleArg(&options->moduleArgs, "");
100}
101
102void ffDestroyTerminalFontOptions(FFTerminalFontOptions* options) {
103 ffOptionDestroyModuleArg(&options->moduleArgs);
104}
105
106FFModuleBaseInfo ffTerminalFontModuleInfo = {
107 .name = "TerminalFont",
108 .description = "Print the font name and size used by the current terminal",
109 .displayName = {
110 .en = "Terminal Font",
111 .ar = "خط الطرفية",
112 .cs = "Písmo terminálu",
113 .de = "Terminalschriftart",
114 .es = "Fuente del terminal",
115 .fi = "Terminaalin Fontti",
116 .fr = "Police du terminal",
117 .gl = "Fonte do terminal",
118 .he = "גופן טרמינל",
119 .id = "Font Terminal",
120 .it = "Carattere del terminale",
121 .ja = "ターミナルフォント",
122 .ko = "터미널 글꼴",
123 .pl = "Czcionka terminala",
124 .pt = "Fonte do terminal",
125 .ru = "Шрифт терминала",
126 .tr = "Terminal Yazı Tipi",
127 .uk = "Шрифт терміналу",
128 .vi = "Phông chữ Terminal",
129 .zh_CN = "终端字体",
130 .zh_TW = "終端機字型",
131 },
132 .initOptions = (void*) ffInitTerminalFontOptions,
133 .destroyOptions = (void*) ffDestroyTerminalFontOptions,
134 .parseJsonObject = (void*) ffParseTerminalFontJsonObject,
135 .printModule = (void*) ffPrintTerminalFont,
136 .generateJsonResult = (void*) ffGenerateTerminalFontJsonResult,
137 .generateJsonConfig = (void*) ffGenerateTerminalFontJsonConfig,
138 .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
139 { "Terminal font combined", "combined" },
140 { "Terminal font name", "name" },
141 { "Terminal font size", "size" },
142 { "Terminal font styles", "styles" },
143 })),
144 .defaultOrder = 30,
145};