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/terminaltheme/option.h | 9 +++ src/modules/terminaltheme/terminaltheme.c | 110 ++++++++++++++++++++++++++++++ src/modules/terminaltheme/terminaltheme.h | 11 +++ 3 files changed, 130 insertions(+) create mode 100644 src/modules/terminaltheme/option.h create mode 100644 src/modules/terminaltheme/terminaltheme.c create mode 100644 src/modules/terminaltheme/terminaltheme.h (limited to 'src/modules/terminaltheme') diff --git a/src/modules/terminaltheme/option.h b/src/modules/terminaltheme/option.h new file mode 100644 index 0000000..25a0cc3 --- /dev/null +++ b/src/modules/terminaltheme/option.h @@ -0,0 +1,9 @@ +#pragma once + +#include "common/option.h" + +typedef struct FFTerminalThemeOptions { + FFModuleArgs moduleArgs; +} FFTerminalThemeOptions; + +static_assert(sizeof(FFTerminalThemeOptions) <= FF_OPTION_MAX_SIZE, "FFTerminalThemeOptions size exceeds maximum allowed size"); diff --git a/src/modules/terminaltheme/terminaltheme.c b/src/modules/terminaltheme/terminaltheme.c new file mode 100644 index 0000000..80bcd29 --- /dev/null +++ b/src/modules/terminaltheme/terminaltheme.c @@ -0,0 +1,110 @@ +#include "common/printing.h" +#include "common/jsonconfig.h" +#include "common/strutil.h" +#include "detection/terminaltheme/terminaltheme.h" +#include "modules/terminaltheme/terminaltheme.h" + +#include + +#define FF_TERMINALTHEME_DISPLAY_NAME "Terminal Theme" + +bool ffPrintTerminalTheme(FFTerminalThemeOptions* options) { + FFTerminalThemeResult result = {}; + + if (!ffDetectTerminalTheme(&result, false)) { + ffPrintError(FF_TERMINALTHEME_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Failed to detect terminal theme"); + return false; + } + + if (options->moduleArgs.outputFormat.length == 0) { + ffPrintLogoAndKey(FF_TERMINALTHEME_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); + printf("#%02" PRIX16 "%02" PRIX16 "%02" PRIX16 " (FG) - #%02" PRIX16 "%02" PRIX16 "%02" PRIX16 " (BG) [%s]\n", + result.fg.r, + result.fg.g, + result.fg.b, + result.bg.r, + result.bg.g, + result.bg.b, + result.bg.dark ? "Dark" : "Light"); + } else { + char fg[32], bg[32]; + const char* fgType = result.fg.dark ? "Dark" : "Light"; + const char* bgType = result.bg.dark ? "Dark" : "Light"; + snprintf(fg, ARRAY_SIZE(fg), "#%02" PRIX16 "%02" PRIX16 "%02" PRIX16, result.fg.r, result.fg.g, result.fg.b); + snprintf(bg, ARRAY_SIZE(bg), "#%02" PRIX16 "%02" PRIX16 "%02" PRIX16, result.bg.r, result.bg.g, result.bg.b); + FF_PRINT_FORMAT_CHECKED(FF_TERMINALTHEME_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) { + FF_ARG(fg, "fg-color"), + FF_ARG(fgType, "fg-type"), + FF_ARG(bg, "bg-color"), + FF_ARG(bgType, "bg-type"), + })); + } + + return true; +} + +void ffParseTerminalThemeJsonObject(FFTerminalThemeOptions* 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; + } + + ffPrintError(FF_TERMINALTHEME_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key)); + } +} + +void ffGenerateTerminalThemeJsonConfig(FFTerminalThemeOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { + ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); +} + +bool ffGenerateTerminalThemeJsonResult(FF_A_UNUSED FFTerminalThemeOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { + FFTerminalThemeResult result = {}; + + if (!ffDetectTerminalTheme(&result, false)) { + yyjson_mut_obj_add_str(doc, module, "error", "Failed to detect terminal theme"); + return false; + } + + yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result"); + + yyjson_mut_val* fg = yyjson_mut_obj_add_obj(doc, obj, "fg"); + yyjson_mut_obj_add_uint(doc, fg, "r", result.fg.r); + yyjson_mut_obj_add_uint(doc, fg, "g", result.fg.g); + yyjson_mut_obj_add_uint(doc, fg, "b", result.fg.b); + yyjson_mut_obj_add_bool(doc, fg, "dark", result.fg.dark); + + yyjson_mut_val* bg = yyjson_mut_obj_add_obj(doc, obj, "bg"); + yyjson_mut_obj_add_uint(doc, bg, "r", result.bg.r); + yyjson_mut_obj_add_uint(doc, bg, "g", result.bg.g); + yyjson_mut_obj_add_uint(doc, bg, "b", result.bg.b); + yyjson_mut_obj_add_bool(doc, bg, "dark", result.bg.dark); + + return true; +} + +void ffInitTerminalThemeOptions(FFTerminalThemeOptions* options) { + ffOptionInitModuleArg(&options->moduleArgs, "󰔎"); +} + +void ffDestroyTerminalThemeOptions(FFTerminalThemeOptions* options) { + ffOptionDestroyModuleArg(&options->moduleArgs); +} + +FFModuleBaseInfo ffTerminalThemeModuleInfo = { + .name = FF_TERMINALTHEME_MODULE_NAME, + .description = "Print the current terminal theme (foreground and background colors)", + .initOptions = (void*) ffInitTerminalThemeOptions, + .destroyOptions = (void*) ffDestroyTerminalThemeOptions, + .parseJsonObject = (void*) ffParseTerminalThemeJsonObject, + .printModule = (void*) ffPrintTerminalTheme, + .generateJsonResult = (void*) ffGenerateTerminalThemeJsonResult, + .generateJsonConfig = (void*) ffGenerateTerminalThemeJsonConfig, + .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) { + { "Terminal foreground color", "fg-color" }, + { "Terminal foreground type (Dark / Light)", "fg-type" }, + { "Terminal background color", "bg-color" }, + { "Terminal background type (Dark / Light)", "bg-type" }, + })) +}; diff --git a/src/modules/terminaltheme/terminaltheme.h b/src/modules/terminaltheme/terminaltheme.h new file mode 100644 index 0000000..db120f7 --- /dev/null +++ b/src/modules/terminaltheme/terminaltheme.h @@ -0,0 +1,11 @@ +#pragma once + +#include "option.h" + +#define FF_TERMINALTHEME_MODULE_NAME "TerminalTheme" + +bool ffPrintTerminalTheme(FFTerminalThemeOptions* options); +void ffInitTerminalThemeOptions(FFTerminalThemeOptions* options); +void ffDestroyTerminalThemeOptions(FFTerminalThemeOptions* options); + +extern FFModuleBaseInfo ffTerminalThemeModuleInfo; -- cgit v1.2.3