Files
fastfetch/src/modules/terminaltheme/terminaltheme.c
T

111 lines
5.0 KiB
C
Raw Normal View History

2026-01-06 09:48:38 +08:00
#include "common/printing.h"
#include "common/jsonconfig.h"
2026-05-28 15:58:08 +08:00
#include "common/strutil.h"
2024-01-22 16:03:32 +08:00
#include "detection/terminaltheme/terminaltheme.h"
#include "modules/terminaltheme/terminaltheme.h"
#include <inttypes.h>
#define FF_TERMINALTHEME_DISPLAY_NAME "Terminal Theme"
2026-03-29 09:28:49 +08:00
bool ffPrintTerminalTheme(FFTerminalThemeOptions* options) {
2024-01-22 16:03:32 +08:00
FFTerminalThemeResult result = {};
2026-03-29 09:28:49 +08:00
if (!ffDetectTerminalTheme(&result, false)) {
ffPrintError(FF_TERMINALTHEME_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Failed to detect terminal theme");
return false;
}
2026-03-29 09:28:49 +08:00
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",
2026-03-29 09:28:49 +08:00
result.fg.r,
result.fg.g,
result.fg.b,
result.bg.r,
result.bg.g,
result.bg.b,
result.bg.dark ? "Dark" : "Light");
2026-03-29 09:28:49 +08:00
} 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);
2026-03-29 09:28:49 +08:00
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"),
}));
2024-01-22 16:03:32 +08:00
}
return true;
2024-01-22 16:03:32 +08:00
}
2026-03-29 09:28:49 +08:00
void ffParseTerminalThemeJsonObject(FFTerminalThemeOptions* options, yyjson_val* module) {
yyjson_val *key, *val;
2024-01-22 16:03:32 +08:00
size_t idx, max;
2026-03-29 09:28:49 +08:00
yyjson_obj_foreach (module, idx, max, key, val) {
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
2024-01-22 16:03:32 +08:00
continue;
2026-03-29 09:28:49 +08:00
}
2024-01-22 16:03:32 +08:00
ffPrintError(FF_TERMINALTHEME_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
2024-01-22 16:03:32 +08:00
}
}
2026-03-29 09:28:49 +08:00
void ffGenerateTerminalThemeJsonConfig(FFTerminalThemeOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
2024-01-22 16:03:32 +08:00
}
2026-04-09 16:20:59 +08:00
bool ffGenerateTerminalThemeJsonResult(FF_A_UNUSED FFTerminalThemeOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
2024-01-22 16:03:32 +08:00
FFTerminalThemeResult result = {};
2026-03-29 09:28:49 +08:00
if (!ffDetectTerminalTheme(&result, false)) {
2024-01-22 16:03:32 +08:00
yyjson_mut_obj_add_str(doc, module, "error", "Failed to detect terminal theme");
return false;
2024-01-22 16:03:32 +08:00
}
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;
2024-01-22 16:03:32 +08:00
}
2026-03-29 09:28:49 +08:00
void ffInitTerminalThemeOptions(FFTerminalThemeOptions* options) {
ffOptionInitModuleArg(&options->moduleArgs, "󰔎");
}
2026-03-29 09:28:49 +08:00
void ffDestroyTerminalThemeOptions(FFTerminalThemeOptions* options) {
ffOptionDestroyModuleArg(&options->moduleArgs);
}
2025-08-03 22:50:56 +08:00
FFModuleBaseInfo ffTerminalThemeModuleInfo = {
2024-12-11 10:47:02 +08:00
.name = FF_TERMINALTHEME_MODULE_NAME,
2026-04-16 18:27:11 +08:00
.description = "Print the current terminal theme (foreground and background colors)",
.initOptions = (void*) ffInitTerminalThemeOptions,
.destroyOptions = (void*) ffDestroyTerminalThemeOptions,
2024-12-11 10:47:02 +08:00
.parseJsonObject = (void*) ffParseTerminalThemeJsonObject,
.printModule = (void*) ffPrintTerminalTheme,
.generateJsonResult = (void*) ffGenerateTerminalThemeJsonResult,
.generateJsonConfig = (void*) ffGenerateTerminalThemeJsonConfig,
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
2026-03-30 10:27:23 +08:00
{ "Terminal foreground color", "fg-color" },
{ "Terminal foreground type (Dark / Light)", "fg-type" },
{ "Terminal background color", "bg-color" },
{ "Terminal background type (Dark / Light)", "bg-type" },
}))
};