From 5c783e64c008917f06bf5ed1c5589ed10391f695 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 8 Jan 2026 16:29:24 +0800 Subject: [PATCH] Logo: adds new module for querying raw builtin logo data Fixes #2124 --- CHANGELOG.md | 2 + CMakeLists.txt | 1 + src/modules/logo/logo.c | 91 +++++++++++++++++++++++++++++++++++++++ src/modules/logo/logo.h | 11 +++++ src/modules/logo/option.h | 10 +++++ src/modules/modules.c | 1 + src/modules/modules.h | 1 + 7 files changed, 117 insertions(+) create mode 100644 src/modules/logo/logo.c create mode 100644 src/modules/logo/logo.h create mode 100644 src/modules/logo/option.h diff --git a/CHANGELOG.md b/CHANGELOG.md index b5a9915ca..4004e929e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,8 @@ Features: * In `display.color`: "@" (e.g., "@34" for color index 34) * In `format` strings: "#@" (e.g., "#@34" for color index 34) * Improves uptime accuracy on Windows 10+ (Uptime, Windows) +* Adds a new module `Logo` to query built-in logo raw data in JSON output (Logo) + * Usage: `fastfetch -s logo -l -j # Supported in JSON format only` Bugfixes: * Skips local / loopback routes when detecting network interfaces (LocalIP, Linux) (#2127) diff --git a/CMakeLists.txt b/CMakeLists.txt index 905e4405f..e42ec5080 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -468,6 +468,7 @@ set(LIBFASTFETCH_SRC src/modules/loadavg/loadavg.c src/modules/locale/locale.c src/modules/localip/localip.c + src/modules/logo/logo.c src/modules/memory/memory.c src/modules/monitor/monitor.c src/modules/netio/netio.c diff --git a/src/modules/logo/logo.c b/src/modules/logo/logo.c new file mode 100644 index 000000000..1d47a52ca --- /dev/null +++ b/src/modules/logo/logo.c @@ -0,0 +1,91 @@ +#include "3rdparty/yyjson/yyjson.h" +#include "common/printing.h" +#include "logo/logo.h" +#include "fastfetch.h" +#include "modules/logo/logo.h" +#include "options/logo.h" + +bool ffPrintLogo(FF_MAYBE_UNUSED FFLogoOptions* options) +{ + ffPrintError(FF_LOGO_MODULE_NAME, 0, NULL, FF_PRINT_TYPE_DEFAULT, "Supported in JSON format only"); + return false; +} + +void ffParseLogoJsonObject(FF_MAYBE_UNUSED FFLogoOptions* options, FF_MAYBE_UNUSED yyjson_val* module) +{ + yyjson_val *key, *val; + size_t idx, max; + yyjson_obj_foreach(module, idx, max, key, val) + { + if (unsafe_yyjson_equals_str(key, "type") || unsafe_yyjson_equals_str(key, "condition")) + continue; + + ffPrintError(FF_LOGO_MODULE_NAME, 0, NULL, FF_PRINT_TYPE_NO_CUSTOM_KEY, "Unknown JSON key %s", unsafe_yyjson_get_str(key)); + } +} + +bool ffGenerateLogoJsonResult(FF_MAYBE_UNUSED FFLogoOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +{ + FFLogoSize size = FF_LOGO_SIZE_UNKNOWN; + FFOptionsLogo* logoOptions = &instance.config.logo; + if (logoOptions->type == FF_LOGO_TYPE_SMALL) + size = FF_LOGO_SIZE_SMALL; + else if (logoOptions->type != FF_LOGO_TYPE_BUILTIN && logoOptions->type != FF_LOGO_TYPE_AUTO) + { + yyjson_mut_obj_add_str(doc, module, "error", "Only 'builtin' and 'small' logo types are supported"); + return false; + } + + const FFlogo* logo = logoOptions->source.length > 0 + ? ffLogoGetBuiltinForName(&logoOptions->source, size) + : ffLogoGetBuiltinDetected(size); + + if (!logo) + { + yyjson_mut_obj_add_str(doc, module, "error", "No built-in logo found for the specified name/size"); + return false; + } + + yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result"); + + yyjson_mut_obj_add_str(doc, obj, "lines", logo->lines); + + yyjson_mut_val* namesArr = yyjson_mut_obj_add_arr(doc, obj, "names"); + for (size_t i = 0; i < FASTFETCH_LOGO_MAX_NAMES && logo->names[i]; i++) + yyjson_mut_arr_add_str(doc, namesArr, logo->names[i]); + + yyjson_mut_val* colorsArr = yyjson_mut_obj_add_arr(doc, obj, "colors"); + for (size_t i = 0; i < FASTFETCH_LOGO_MAX_COLORS && logo->colors[i]; i++) + yyjson_mut_arr_add_str(doc, colorsArr, logo->colors[i]); + + yyjson_mut_obj_add_str(doc, obj, "colorKeys", logo->colorKeys); + yyjson_mut_obj_add_str(doc, obj, "colorTitle", logo->colorTitle); + + yyjson_mut_val* typeArr = yyjson_mut_obj_add_arr(doc, obj, "type"); + if (logo->type & FF_LOGO_LINE_TYPE_NORMAL) + yyjson_mut_arr_add_str(doc, typeArr, "normal"); + if (logo->type & FF_LOGO_LINE_TYPE_SMALL_BIT) + yyjson_mut_arr_add_str(doc, typeArr, "small"); + if (logo->type & FF_LOGO_LINE_TYPE_ALTER_BIT) + yyjson_mut_arr_add_str(doc, typeArr, "alter"); + + return true; +} + +void ffInitLogoOptions(FF_MAYBE_UNUSED FFLogoOptions* options) +{ +} + +void ffDestroyLogoOptions(FF_MAYBE_UNUSED FFLogoOptions* options) +{ +} + +FFModuleBaseInfo ffLogoModuleInfo = { + .name = FF_LOGO_MODULE_NAME, + .description = "Query built-in logo for JSON output", + .initOptions = (void*) ffInitLogoOptions, + .destroyOptions = (void*) ffDestroyLogoOptions, + .parseJsonObject = (void*) ffParseLogoJsonObject, + .printModule = (void*) ffPrintLogo, + .generateJsonResult = (void*) ffGenerateLogoJsonResult, +}; diff --git a/src/modules/logo/logo.h b/src/modules/logo/logo.h new file mode 100644 index 000000000..7f48e2dd6 --- /dev/null +++ b/src/modules/logo/logo.h @@ -0,0 +1,11 @@ +#pragma once + +#include "option.h" + +#define FF_LOGO_MODULE_NAME "Logo" + +bool ffPrintLogo(FFLogoOptions* options); +void ffInitLogoOptions(FFLogoOptions* options); +void ffDestroyLogoOptions(FFLogoOptions* options); + +extern FFModuleBaseInfo ffLogoModuleInfo; diff --git a/src/modules/logo/option.h b/src/modules/logo/option.h new file mode 100644 index 000000000..0abfff55d --- /dev/null +++ b/src/modules/logo/option.h @@ -0,0 +1,10 @@ +#pragma once + +#include "common/option.h" + +typedef struct FFLogoOptions +{ + FFModuleArgs moduleArgs; +} FFLogoOptions; + +static_assert(sizeof(FFLogoOptions) <= FF_OPTION_MAX_SIZE, "FFLogoOptions size exceeds maximum allowed size"); diff --git a/src/modules/modules.c b/src/modules/modules.c index 7ad221005..cae08a06e 100644 --- a/src/modules/modules.c +++ b/src/modules/modules.c @@ -82,6 +82,7 @@ static FFModuleBaseInfo* L[] = { &ffLoadavgModuleInfo, &ffLocaleModuleInfo, &ffLocalIPModuleInfo, + &ffLogoModuleInfo, NULL, }; diff --git a/src/modules/modules.h b/src/modules/modules.h index adc128f94..2579ad90e 100644 --- a/src/modules/modules.h +++ b/src/modules/modules.h @@ -37,6 +37,7 @@ #include "modules/loadavg/loadavg.h" #include "modules/locale/locale.h" #include "modules/localip/localip.h" +#include "modules/logo/logo.h" #include "modules/media/media.h" #include "modules/memory/memory.h" #include "modules/monitor/monitor.h"