From 4328508e210f8c84765211ebb67f2e652819dbe1 Mon Sep 17 00:00:00 2001 From: Carter Li Date: Mon, 3 Jun 2024 09:24:52 +0800 Subject: [PATCH] Theme: make it possible to shorten the theme name Fix #724 --- src/detection/theme/theme.h | 8 +++++- src/detection/theme/theme_linux.c | 30 ++++++++------------- src/detection/theme/theme_nosupport.c | 2 +- src/modules/theme/theme.c | 38 ++++++++++++++++++++------- 4 files changed, 47 insertions(+), 31 deletions(-) diff --git a/src/detection/theme/theme.h b/src/detection/theme/theme.h index 2536c6fb7..aa70f05ba 100644 --- a/src/detection/theme/theme.h +++ b/src/detection/theme/theme.h @@ -2,4 +2,10 @@ #include "fastfetch.h" -const char* ffDetectTheme(FFstrbuf* result); +typedef struct FFThemeResult +{ + FFstrbuf theme1; + FFstrbuf theme2; +} FFThemeResult; + +const char* ffDetectTheme(FFThemeResult* result); diff --git a/src/detection/theme/theme_linux.c b/src/detection/theme/theme_linux.c index 1a97f49f4..4b4637b88 100644 --- a/src/detection/theme/theme_linux.c +++ b/src/detection/theme/theme_linux.c @@ -3,7 +3,7 @@ #include "detection/gtk_qt/gtk_qt.h" #include "detection/displayserver/displayserver.h" -const char* ffDetectTheme(FFstrbuf* result) +const char* ffDetectTheme(FFThemeResult* result) { const FFDisplayServerResult* wmde = ffConnectDisplayServer(); @@ -18,6 +18,8 @@ const char* ffDetectTheme(FFstrbuf* result) if(plasma->widgetStyle.length == 0 && plasma->colorScheme.length == 0 && gtk2->length == 0 && gtk3->length == 0 && gtk4->length == 0) return "No themes found"; + ffParseGTK(&result->theme2, gtk2, gtk3, gtk4); + FF_STRBUF_AUTO_DESTROY plasmaColorPretty = ffStrbufCreate(); if(ffStrbufStartsWithIgnCase(&plasma->colorScheme, &plasma->widgetStyle)) ffStrbufAppendNS(&plasmaColorPretty, plasma->colorScheme.length - plasma->widgetStyle.length, &plasma->colorScheme.chars[plasma->widgetStyle.length]); @@ -26,42 +28,32 @@ const char* ffDetectTheme(FFstrbuf* result) ffStrbufTrim(&plasmaColorPretty, ' '); - FF_STRBUF_AUTO_DESTROY gtkPretty = ffStrbufCreate(); - ffParseGTK(>kPretty, gtk2, gtk3, gtk4); - if(plasma->widgetStyle.length > 0) { - ffStrbufAppend(result, &plasma->widgetStyle); + ffStrbufAppend(&result->theme1, &plasma->widgetStyle); if(plasma->colorScheme.length > 0) { - ffStrbufAppendS(result, " ("); + ffStrbufAppendS(&result->theme1, " ("); if(plasmaColorPretty.length > 0) - ffStrbufAppend(result, &plasmaColorPretty); + ffStrbufAppend(&result->theme1, &plasmaColorPretty); else - ffStrbufAppend(result, &plasma->colorScheme); + ffStrbufAppend(&result->theme1, &plasma->colorScheme); - ffStrbufAppendC(result, ')'); + ffStrbufAppendC(&result->theme1, ')'); } } else if(plasma->colorScheme.length > 0) { if(plasmaColorPretty.length > 0) - ffStrbufAppend(result, &plasmaColorPretty); + ffStrbufAppend(&result->theme1, &plasmaColorPretty); else - ffStrbufAppend(result, &plasma->colorScheme); + ffStrbufAppend(&result->theme1, &plasma->colorScheme); } if(plasma->widgetStyle.length > 0 || plasma->colorScheme.length > 0) - { - ffStrbufAppendS(result, " [QT]"); - - if(gtkPretty.length > 0) - ffStrbufAppendS(result, ", "); - } - - ffStrbufAppend(result, >kPretty); + ffStrbufAppendS(&result->theme1, " [QT]"); return NULL; } diff --git a/src/detection/theme/theme_nosupport.c b/src/detection/theme/theme_nosupport.c index c946833e6..caddd724d 100644 --- a/src/detection/theme/theme_nosupport.c +++ b/src/detection/theme/theme_nosupport.c @@ -1,6 +1,6 @@ #include "theme.h" -const char* ffDetectTheme(FF_MAYBE_UNUSED FFstrbuf* result) +const char* ffDetectTheme(FF_MAYBE_UNUSED FFThemeResult* result) { return "Not supported on this platform"; } diff --git a/src/modules/theme/theme.c b/src/modules/theme/theme.c index 8e8ea78d5..b6eda5f06 100644 --- a/src/modules/theme/theme.c +++ b/src/modules/theme/theme.c @@ -4,12 +4,15 @@ #include "modules/theme/theme.h" #include "util/stringUtils.h" -#define FF_THEME_NUM_FORMAT_ARGS 1 +#define FF_THEME_NUM_FORMAT_ARGS 2 void ffPrintTheme(FFThemeOptions* options) { - FF_STRBUF_AUTO_DESTROY theme = ffStrbufCreate(); - const char* error = ffDetectTheme(&theme); + FFThemeResult result = { + .theme1 = ffStrbufCreate(), + .theme2 = ffStrbufCreate() + }; + const char* error = ffDetectTheme(&result); if(error) { @@ -20,12 +23,21 @@ void ffPrintTheme(FFThemeOptions* options) if(options->moduleArgs.outputFormat.length == 0) { ffPrintLogoAndKey(FF_THEME_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); - ffStrbufPutTo(&theme, stdout); + if (result.theme1.length) + ffStrbufWriteTo(&result.theme1, stdout); + if (result.theme2.length) + { + if (result.theme1.length) + fputs(", ", stdout); + ffStrbufWriteTo(&result.theme2, stdout); + } + putchar('\n'); } else { FF_PRINT_FORMAT_CHECKED(FF_THEME_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, FF_THEME_NUM_FORMAT_ARGS, ((FFformatarg[]){ - {FF_FORMAT_ARG_TYPE_STRBUF, &theme, "combined"}, + {FF_FORMAT_ARG_TYPE_STRBUF, &result.theme1, "theme1"}, + {FF_FORMAT_ARG_TYPE_STRBUF, &result.theme2, "theme2"}, })); } } @@ -67,8 +79,11 @@ void ffGenerateThemeJsonConfig(FFThemeOptions* options, yyjson_mut_doc* doc, yyj void ffGenerateThemeJsonResult(FF_MAYBE_UNUSED FFThemeOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { - FF_STRBUF_AUTO_DESTROY theme = ffStrbufCreate(); - const char* error = ffDetectTheme(&theme); + FFThemeResult result = { + .theme1 = ffStrbufCreate(), + .theme2 = ffStrbufCreate() + }; + const char* error = ffDetectTheme(&result); if(error) { @@ -76,13 +91,16 @@ void ffGenerateThemeJsonResult(FF_MAYBE_UNUSED FFThemeOptions* options, yyjson_m return; } - yyjson_mut_obj_add_strbuf(doc, module, "result", &theme); + yyjson_mut_val* theme = yyjson_mut_obj_add_obj(doc, module, "result"); + yyjson_mut_obj_add_strbuf(doc, theme, "theme1", &result.theme1); + yyjson_mut_obj_add_strbuf(doc, theme, "theme2", &result.theme2); } void ffPrintThemeHelpFormat(void) { - FF_PRINT_MODULE_FORMAT_HELP_CHECKED(FF_THEME_MODULE_NAME, "{1}", FF_THEME_NUM_FORMAT_ARGS, ((const char* []) { - "Combined themes - combined", + FF_PRINT_MODULE_FORMAT_HELP_CHECKED(FF_THEME_MODULE_NAME, "{1}, {2}", FF_THEME_NUM_FORMAT_ARGS, ((const char* []) { + "Theme part 1 - theme1", + "Theme part 2 - theme2", })); }