Theme: split detection code

This commit is contained in:
李通洲 2023-06-08 14:32:15 +08:00
parent 4af44a40bf
commit ee876c54bf
No known key found for this signature in database
GPG Key ID: 269AD4F5325A22A3
6 changed files with 101 additions and 91 deletions

View File

@ -359,6 +359,7 @@ if(LINUX)
src/detection/temps/temps_linux.c
src/detection/terminalfont/terminalfont_linux.c
src/detection/terminalshell/terminalshell_linux.c
src/detection/theme/theme_linux.c
src/detection/uptime/uptime_linux.c
src/detection/users/users_linux.c
src/detection/wallpaper/wallpaper_linux.c
@ -400,6 +401,7 @@ elseif(ANDROID)
src/detection/temps/temps_linux.c
src/detection/terminalfont/terminalfont_android.c
src/detection/terminalshell/terminalshell_linux.c
src/detection/theme/theme_nosupport.c
src/detection/uptime/uptime_linux.c
src/detection/users/users_linux.c
src/detection/wallpaper/wallpaper_nosupport.c
@ -449,6 +451,7 @@ elseif(BSD)
src/detection/temps/temps_linux.c
src/detection/terminalfont/terminalfont_linux.c
src/detection/terminalshell/terminalshell_linux.c
src/detection/theme/theme_linux.c
src/detection/uptime/uptime_bsd.c
src/detection/users/users_linux.c
src/detection/wallpaper/wallpaper_linux.c
@ -492,6 +495,7 @@ elseif(APPLE)
src/detection/temps/temps_apple.c
src/detection/terminalfont/terminalfont_apple.m
src/detection/terminalshell/terminalshell_linux.c
src/detection/theme/theme_nosupport.c
src/detection/uptime/uptime_bsd.c
src/detection/users/users_linux.c
src/detection/wallpaper/wallpaper_apple.c
@ -535,6 +539,7 @@ elseif(WIN32)
src/detection/terminalfont/terminalfont_windows.c
src/detection/terminalshell/terminalshell_windows.c
src/detection/temps/temps_windows.cpp
src/detection/theme/theme_nosupport.c
src/detection/uptime/uptime_windows.c
src/detection/users/users_windows.c
src/detection/wallpaper/wallpaper_windows.c

View File

@ -0,0 +1,10 @@
#pragma once
#ifndef FF_INCLUDED_detection_theme
#define FF_INCLUDED_detection_theme
#include "fastfetch.h"
const char* ffDetectTheme(const FFinstance* instance, FFstrbuf* result);
#endif

View File

@ -0,0 +1,69 @@
#include "theme.h"
#include "common/parsing.h"
#include "detection/gtk_qt/gtk_qt.h"
#include "detection/displayserver/displayserver.h"
const char* ffDetectTheme(const FFinstance* instance, FFstrbuf* result)
{
const FFDisplayServerResult* wmde = ffConnectDisplayServer(instance);
if(ffStrbufIgnCaseCompS(&wmde->wmProtocolName, FF_WM_PROTOCOL_TTY) == 0)
return "Theme isn't supported in TTY";
const FFQtResult* plasma = ffDetectQt(instance);
const FFstrbuf* gtk2 = &ffDetectGTK2(instance)->theme;
const FFstrbuf* gtk3 = &ffDetectGTK3(instance)->theme;
const FFstrbuf* gtk4 = &ffDetectGTK4(instance)->theme;
if(plasma->widgetStyle.length == 0 && plasma->colorScheme.length == 0 && gtk2->length == 0 && gtk3->length == 0 && gtk4->length == 0)
return "No themes found";
FF_STRBUF_AUTO_DESTROY plasmaColorPretty;
ffStrbufInit(&plasmaColorPretty);
if(ffStrbufStartsWithIgnCase(&plasma->colorScheme, &plasma->widgetStyle))
ffStrbufAppendNS(&plasmaColorPretty, plasma->colorScheme.length - plasma->widgetStyle.length, &plasma->colorScheme.chars[plasma->widgetStyle.length]);
else
ffStrbufAppend(&plasmaColorPretty, &plasma->colorScheme);
ffStrbufTrim(&plasmaColorPretty, ' ');
FF_STRBUF_AUTO_DESTROY gtkPretty;
ffStrbufInit(&gtkPretty);
ffParseGTK(&gtkPretty, gtk2, gtk3, gtk4);
if(plasma->widgetStyle.length > 0)
{
ffStrbufAppend(result, &plasma->widgetStyle);
if(plasma->colorScheme.length > 0)
{
ffStrbufAppendS(result, " (");
if(plasmaColorPretty.length > 0)
ffStrbufAppend(result, &plasmaColorPretty);
else
ffStrbufAppend(result, &plasma->colorScheme);
ffStrbufAppendC(result, ')');
}
}
else if(plasma->colorScheme.length > 0)
{
if(plasmaColorPretty.length > 0)
ffStrbufAppend(result, &plasmaColorPretty);
else
ffStrbufAppend(result, &plasma->colorScheme);
}
if(plasma->widgetStyle.length > 0 || plasma->colorScheme.length > 0)
{
ffStrbufAppendS(result, " [QT]");
if(gtkPretty.length > 0)
ffStrbufAppendS(result, ", ");
}
ffStrbufAppend(result, &gtkPretty);
return NULL;
}

View File

@ -0,0 +1,6 @@
#include "theme.h"
const char* ffDetectTheme(FF_MAYBE_UNUSED const FFinstance* instance, FF_MAYBE_UNUSED FFstrbuf* result)
{
return "Not supported on this platform";
}

View File

@ -207,14 +207,8 @@ static inline void printCommandHelp(const char* command)
}
else if(strcasecmp(command, "theme-format") == 0)
{
constructAndPrintCommandHelpFormat("theme", "{} ({3}) [Plasma], {7}", 7,
"Plasma theme",
"Plasma color scheme",
"Plasma color scheme pretty",
"GTK2 theme",
"GTK3 theme",
"GTK4 theme",
"Combined GTK themes"
constructAndPrintCommandHelpFormat("theme", "{}", 1,
"Combined themes"
);
}
else if(strcasecmp(command, "icons-format") == 0)

View File

@ -1,104 +1,30 @@
#include "fastfetch.h"
#include "common/printing.h"
#include "common/parsing.h"
#include "detection/gtk_qt/gtk_qt.h"
#include "detection/displayserver/displayserver.h"
#include "detection/theme/theme.h"
#define FF_THEME_MODULE_NAME "Theme"
#define FF_THEME_NUM_FORMAT_ARGS 7
#define FF_THEME_NUM_FORMAT_ARGS 1
void ffPrintTheme(FFinstance* instance)
{
#if defined(__ANDROID__) || defined(__APPLE__) || defined(_WIN32)
FF_UNUSED(instance);
ffPrintError(instance, FF_THEME_MODULE_NAME, 0, &instance->config.theme, "Theme detection is not supported");
return;
#else
const FFDisplayServerResult* wmde = ffConnectDisplayServer(instance);
if(ffStrbufIgnCaseCompS(&wmde->wmProtocolName, FF_WM_PROTOCOL_TTY) == 0)
FF_STRBUF_AUTO_DESTROY theme;
ffStrbufInit(&theme);
const char* error = ffDetectTheme(instance, &theme);
if (error)
{
ffPrintError(instance, FF_THEME_MODULE_NAME, 0, &instance->config.theme, "Theme isn't supported in TTY");
ffPrintError(instance, FF_THEME_MODULE_NAME, 0, &instance->config.theme, "%s", error);
return;
}
const FFQtResult* plasma = ffDetectQt(instance);
const FFstrbuf* gtk2 = &ffDetectGTK2(instance)->theme;
const FFstrbuf* gtk3 = &ffDetectGTK3(instance)->theme;
const FFstrbuf* gtk4 = &ffDetectGTK4(instance)->theme;
if(plasma->widgetStyle.length == 0 && plasma->colorScheme.length == 0 && gtk2->length == 0 && gtk3->length == 0 && gtk4->length == 0)
{
ffPrintError(instance, FF_THEME_MODULE_NAME, 0, &instance->config.theme, "No themes found");
return;
}
FF_STRBUF_AUTO_DESTROY plasmaColorPretty;
ffStrbufInit(&plasmaColorPretty);
if(ffStrbufStartsWithIgnCase(&plasma->colorScheme, &plasma->widgetStyle))
ffStrbufAppendNS(&plasmaColorPretty, plasma->colorScheme.length - plasma->widgetStyle.length, &plasma->colorScheme.chars[plasma->widgetStyle.length]);
else
ffStrbufAppend(&plasmaColorPretty, &plasma->colorScheme);
ffStrbufTrim(&plasmaColorPretty, ' ');
FF_STRBUF_AUTO_DESTROY gtkPretty;
ffStrbufInit(&gtkPretty);
ffParseGTK(&gtkPretty, gtk2, gtk3, gtk4);
if(instance->config.theme.outputFormat.length == 0)
{
ffPrintLogoAndKey(instance, FF_THEME_MODULE_NAME, 0, &instance->config.theme.key);
if(plasma->widgetStyle.length > 0)
{
ffStrbufWriteTo(&plasma->widgetStyle, stdout);
if(plasma->colorScheme.length > 0)
{
fputs(" (", stdout);
if(plasmaColorPretty.length > 0)
ffStrbufWriteTo(&plasmaColorPretty, stdout);
else
ffStrbufWriteTo(&plasma->colorScheme, stdout);
putchar(')');
}
}
else if(plasma->colorScheme.length > 0)
{
if(plasmaColorPretty.length > 0)
ffStrbufWriteTo(&plasmaColorPretty, stdout);
else
ffStrbufWriteTo(&plasma->colorScheme, stdout);
}
if(plasma->widgetStyle.length > 0 || plasma->colorScheme.length > 0)
{
fputs(" [QT]", stdout);
if(gtkPretty.length > 0)
fputs(", ", stdout);
}
ffStrbufPutTo(&gtkPretty, stdout);
ffStrbufPutTo(&theme, stdout);
}
else
{
ffPrintFormat(instance, FF_THEME_MODULE_NAME, 0, &instance->config.theme, FF_THEME_NUM_FORMAT_ARGS, (FFformatarg[]){
{FF_FORMAT_ARG_TYPE_STRBUF, &plasma->widgetStyle},
{FF_FORMAT_ARG_TYPE_STRBUF, &plasma->colorScheme},
{FF_FORMAT_ARG_TYPE_STRBUF, &plasmaColorPretty},
{FF_FORMAT_ARG_TYPE_STRBUF, gtk2},
{FF_FORMAT_ARG_TYPE_STRBUF, gtk3},
{FF_FORMAT_ARG_TYPE_STRBUF, gtk4},
{FF_FORMAT_ARG_TYPE_STRBUF, &gtkPretty}
{FF_FORMAT_ARG_TYPE_STRBUF, &theme}
});
}
#endif
}