From 76424950e373d3b04ac3dd13019151bfba3e8423 Mon Sep 17 00:00:00 2001 From: sumuel Date: Mon, 17 Aug 2026 20:44:55 +0000 Subject: Add the files --- src/modules/weather/option.h | 13 ++++++ src/modules/weather/weather.c | 105 ++++++++++++++++++++++++++++++++++++++++++ src/modules/weather/weather.h | 13 ++++++ 3 files changed, 131 insertions(+) create mode 100644 src/modules/weather/option.h create mode 100644 src/modules/weather/weather.c create mode 100644 src/modules/weather/weather.h (limited to 'src/modules/weather') diff --git a/src/modules/weather/option.h b/src/modules/weather/option.h new file mode 100644 index 0000000..136277f --- /dev/null +++ b/src/modules/weather/option.h @@ -0,0 +1,13 @@ +#pragma once + +#include "common/option.h" + +typedef struct FFWeatherOptions { + FFModuleArgs moduleArgs; + + FFstrbuf location; + FFstrbuf outputFormat; + uint32_t timeout; +} FFWeatherOptions; + +static_assert(sizeof(FFWeatherOptions) <= FF_OPTION_MAX_SIZE, "FFWeatherOptions size exceeds maximum allowed size"); diff --git a/src/modules/weather/weather.c b/src/modules/weather/weather.c new file mode 100644 index 0000000..6c15692 --- /dev/null +++ b/src/modules/weather/weather.c @@ -0,0 +1,105 @@ +#include "common/printing.h" +#include "common/jsonconfig.h" +#include "common/strutil.h" +#include "detection/weather/weather.h" +#include "modules/weather/weather.h" + +bool ffPrintWeather(FFWeatherOptions* options) { + FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate(); + const char* error = ffDetectWeather(options, &result); + + if (error) { + ffPrintError(FF_WEATHER_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); + return false; + } + + if (options->moduleArgs.outputFormat.length == 0) { + ffPrintLogoAndKey(FF_WEATHER_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); + ffStrbufPutTo(&result, stdout); + } else { + FF_PRINT_FORMAT_CHECKED(FF_WEATHER_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) { + FF_ARG(result, "result"), + })); + } + + return true; +} + +void ffParseWeatherJsonObject(FFWeatherOptions* options, yyjson_val* module) { + yyjson_val *key, *val; + size_t idx, max; + yyjson_obj_foreach (module, idx, max, key, val) { + if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) { + continue; + } + + if (unsafe_yyjson_equals_str(key, "location")) { + ffStrbufSetJsonVal(&options->location, val); + continue; + } + + if (unsafe_yyjson_equals_str(key, "outputFormat")) { + ffStrbufSetJsonVal(&options->outputFormat, val); + continue; + } + + if (unsafe_yyjson_equals_str(key, "timeout")) { + options->timeout = (uint32_t) yyjson_get_uint(val); + continue; + } + + ffPrintError(FF_WEATHER_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key)); + } +} + +void ffGenerateWeatherJsonConfig(FFWeatherOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { + ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); + + yyjson_mut_obj_add_strbuf(doc, module, "location", &options->location); + + yyjson_mut_obj_add_strbuf(doc, module, "outputFormat", &options->outputFormat); + + yyjson_mut_obj_add_uint(doc, module, "timeout", options->timeout); +} + +bool ffGenerateWeatherJsonResult(FFWeatherOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { + FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate(); + const char* error = ffDetectWeather(options, &result); + + if (error) { + yyjson_mut_obj_add_str(doc, module, "error", error); + return false; + } + + yyjson_mut_obj_add_strbuf(doc, module, "result", &result); + + return true; +} + +void ffInitWeatherOptions(FFWeatherOptions* options) { + ffOptionInitModuleArg(&options->moduleArgs, "󰖙"); + + ffStrbufInit(&options->location); + ffStrbufInitStatic(&options->outputFormat, "%t+-+%C+(%l)"); + options->timeout = 0; +} + +void ffDestroyWeatherOptions(FFWeatherOptions* options) { + ffOptionDestroyModuleArg(&options->moduleArgs); + + ffStrbufDestroy(&options->outputFormat); +} + +FFModuleBaseInfo ffWeatherModuleInfo = { + .name = FF_WEATHER_MODULE_NAME, + .description = "Print weather information", + .initOptions = (void*) ffInitWeatherOptions, + .destroyOptions = (void*) ffDestroyWeatherOptions, + .parseJsonObject = (void*) ffParseWeatherJsonObject, + .printModule = (void*) ffPrintWeather, + .generateJsonResult = (void*) ffGenerateWeatherJsonResult, + .generateJsonConfig = (void*) ffGenerateWeatherJsonConfig, + .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) { + { "Weather result", "result" }, + })) +}; diff --git a/src/modules/weather/weather.h b/src/modules/weather/weather.h new file mode 100644 index 0000000..f28a4c2 --- /dev/null +++ b/src/modules/weather/weather.h @@ -0,0 +1,13 @@ +#pragma once + +#include "option.h" + +#define FF_WEATHER_MODULE_NAME "Weather" + +void ffPrepareWeather(FFWeatherOptions* options); + +bool ffPrintWeather(FFWeatherOptions* options); +void ffInitWeatherOptions(FFWeatherOptions* options); +void ffDestroyWeatherOptions(FFWeatherOptions* options); + +extern FFModuleBaseInfo ffWeatherModuleInfo; -- cgit v1.2.3