ohmu
·
2026-10-15
1#include "common/printing.h"
2#include "common/jsonconfig.h"
3#include "common/strutil.h"
4#include "detection/weather/weather.h"
5#include "modules/weather/weather.h"
6
7bool ffPrintWeather(FFWeatherOptions* options) {
8 FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate();
9 const char* error = ffDetectWeather(options, &result);
10
11 if (error) {
12 ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Weather), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
13 return false;
14 }
15
16 if (options->moduleArgs.outputFormat.length == 0) {
17 ffPrintLogoAndKey(FF_MODULE_GET_DISPLAY_NAME(Weather), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
18 ffStrbufPutTo(&result, stdout);
19 } else {
20 FF_PRINT_FORMAT_CHECKED(FF_MODULE_GET_DISPLAY_NAME(Weather), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
21 FF_ARG(result, "result"),
22 }));
23 }
24
25 return true;
26}
27
28void ffParseWeatherJsonObject(FFWeatherOptions* options, yyjson_val* module) {
29 yyjson_val *key, *val;
30 size_t idx, max;
31 yyjson_obj_foreach (module, idx, max, key, val) {
32 if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
33 continue;
34 }
35
36 if (unsafe_yyjson_equals_str(key, "location")) {
37 ffStrbufSetJsonVal(&options->location, val);
38 continue;
39 }
40
41 if (unsafe_yyjson_equals_str(key, "outputFormat")) {
42 ffStrbufSetJsonVal(&options->outputFormat, val);
43 continue;
44 }
45
46 if (unsafe_yyjson_equals_str(key, "timeout")) {
47 options->timeout = (uint32_t) yyjson_get_uint(val);
48 continue;
49 }
50
51 ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Weather), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
52 }
53}
54
55[[gnu::cold]]
56void ffGenerateWeatherJsonConfig(FFWeatherOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
57 ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
58
59 yyjson_mut_obj_add_strbuf(doc, module, "location", &options->location);
60
61 yyjson_mut_obj_add_strbuf(doc, module, "outputFormat", &options->outputFormat);
62
63 yyjson_mut_obj_add_uint(doc, module, "timeout", options->timeout);
64}
65
66bool ffGenerateWeatherJsonResult(FFWeatherOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
67 FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate();
68 const char* error = ffDetectWeather(options, &result);
69
70 if (error) {
71 yyjson_mut_obj_add_str(doc, module, "error", error);
72 return false;
73 }
74
75 yyjson_mut_obj_add_strbuf(doc, module, "result", &result);
76
77 return true;
78}
79
80void ffInitWeatherOptions(FFWeatherOptions* options) {
81 ffOptionInitModuleArg(&options->moduleArgs, "");
82
83 ffStrbufInit(&options->location);
84 ffStrbufInitStatic(&options->outputFormat, "%t+-+%C+(%l)");
85 options->timeout = 0;
86}
87
88void ffDestroyWeatherOptions(FFWeatherOptions* options) {
89 ffOptionDestroyModuleArg(&options->moduleArgs);
90
91 ffStrbufDestroy(&options->location);
92 ffStrbufDestroy(&options->outputFormat);
93}
94
95FFModuleBaseInfo ffWeatherModuleInfo = {
96 .name = "Weather",
97 .description = "Print weather information",
98 .displayName = {
99 .en = "Weather",
100 .ar = "الطقس",
101 .cs = "Počasí",
102 .de = "Wetter",
103 .es = "Tiempo",
104 .fi = "Sää",
105 .fr = "Météo",
106 .gl = "Tempo",
107 .he = "מזג אוויר",
108 .id = "Cuaca",
109 .it = "Meteo",
110 .ja = "天気",
111 .ko = "날씨",
112 .pl = "Pogoda",
113 .pt = "Clima",
114 .ru = "Погода",
115 .tr = "Hava Durumu",
116 .uk = "Погода",
117 .vi = "Thời tiết",
118 .zh_CN = "天气",
119 .zh_TW = "天氣",
120 },
121 .initOptions = (void*) ffInitWeatherOptions,
122 .destroyOptions = (void*) ffDestroyWeatherOptions,
123 .parseJsonObject = (void*) ffParseWeatherJsonObject,
124 .printModule = (void*) ffPrintWeather,
125 .generateJsonResult = (void*) ffGenerateWeatherJsonResult,
126 .generateJsonConfig = (void*) ffGenerateWeatherJsonConfig,
127 .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
128 { "Weather result", "result" },
129 })),
130 .defaultOrder = 66,
131};