diff --git a/CMakeLists.txt b/CMakeLists.txt index 752270fae..d375a64bd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,6 +47,7 @@ set(SRCS src/modules/resolution.c src/modules/de.c src/modules/wm.c + src/modules/wmtheme.c src/modules/theme.c src/modules/icons.c src/modules/font.c diff --git a/completions/bash b/completions/bash index 5771dabe7..ff6661bed 100644 --- a/completions/bash +++ b/completions/bash @@ -144,6 +144,8 @@ __fastfetch_completion() "--de-key" "--wm-format" "--wm-key" + "--wm-theme-format" + "--wm-theme-key" "--theme-format" "--theme-key" "--icons-format" diff --git a/src/common.c b/src/common.c index f61007d93..eb96c9c41 100644 --- a/src/common.c +++ b/src/common.c @@ -47,6 +47,8 @@ void ffDefaultConfig(FFconfig* config) ffStrbufInitA(&config->deKey, 1); ffStrbufInitA(&config->wmFormat, 1); ffStrbufInitA(&config->wmKey, 1); + ffStrbufInitA(&config->wmThemeFormat, 1); + ffStrbufInitA(&config->wmThemeKey, 1); ffStrbufInitA(&config->themeFormat, 1); ffStrbufInitA(&config->themeKey, 1); ffStrbufInitA(&config->iconsFormat, 1); diff --git a/src/fastfetch.c b/src/fastfetch.c index 399876cae..6d889b378 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -8,7 +8,7 @@ #include #include -#define FASTFETCH_DEFAULT_STRUCTURE "Title:Seperator:OS:Host:Kernel:Uptime:Packages:Shell:Resolution:DE:WM:Theme:Icons:Font:Terminal:TerminalFont:CPU:GPU:Memory:Disk:Battery:Locale:Break:Colors" +#define FASTFETCH_DEFAULT_STRUCTURE "Title:Seperator:OS:Host:Kernel:Uptime:Packages:Shell:Resolution:DE:WM:WMTheme:Theme:Icons:Font:Terminal:TerminalFont:CPU:GPU:Memory:Disk:Battery:Locale:Break:Colors" #define FASTFETCH_DEFAULT_CONFIG \ "# Fastfetch configuration\n" \ @@ -68,6 +68,7 @@ static inline void printHelp() " --resolution-format \n" " --de-format \n" " --wm-format \n" + " --wm-theme-format \n" " --theme-format \n" " --icons-format \n" " --font-format \n" @@ -84,23 +85,24 @@ static inline void printHelp() " --os-key \n" " --host-key \n" " --kernel-key \n" - " --uptime-format \n" - " --packages-format \n" - " --shell-format \n" - " --resolution-format \n" - " --de-format \n" - " --wm-format \n" - " --theme-format \n" - " --icons-format \n" - " --font-format \n" - " --terminal-format \n" - " --terminal-font-format \n" - " --cpu-format \n" - " --gpu-format : takes the gpu index as format argument\n" - " --memory-format \n" - " --disk-format : takes the mount path as format argument\n" - " --battery-format : takes the battery index as format argument\n" - " --locale-format \n" + " --uptime-key \n" + " --packages-key \n" + " --shell-key \n" + " --resolution-key \n" + " --de-key \n" + " --wm-key \n" + " --wm-theme-key \n" + " --theme-key \n" + " --icons-key \n" + " --font-key \n" + " --terminal-key \n" + " --terminal-font-key \n" + " --cpu-key \n" + " --gpu-key : takes the gpu index as format argument\n" + " --memory-key \n" + " --disk-key : takes the mount path as format argument\n" + " --battery-key : takes the battery index as format argument\n" + " --locale-key \n" "\n" "Library optins: Set the path of a library to load\n" " --lib-PCI \n" @@ -184,6 +186,7 @@ static inline void printAvailableModules() "Title\n" "Uptime\n" "WM\n" + "WMTheme\n" "\n" "+ Additional defined by --set" ); @@ -293,6 +296,12 @@ static inline void printCommandHelp(const char* command) "WM pretty name" ); } + else if(strcasecmp(command, "wm-theme-format") == 0) + { + constructAndPrintCommandHelpFormat("wm-theme", "{}", 1, + "WM theme name" + ); + } else if(strcasecmp(command, "theme-format") == 0) { constructAndPrintCommandHelpFormat("theme", "{} [Plasma], {5}", 5, @@ -565,6 +574,10 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con optionParseString(key, value, &instance->config.wmFormat); else if(strcasecmp(key, "--wm-key") == 0) optionParseString(key, value, &instance->config.wmKey); + else if(strcasecmp(key, "--wm-theme-format") == 0) + optionParseString(key, value, &instance->config.wmThemeFormat); + else if(strcasecmp(key, "--wm-theme-key") == 0) + optionParseString(key, value, &instance->config.wmThemeKey); else if(strcasecmp(key, "--theme-format") == 0) optionParseString(key, value, &instance->config.themeFormat); else if(strcasecmp(key, "--theme-key") == 0) @@ -788,6 +801,8 @@ static void parseStructureCommand(FFinstance* instance, FFdata* data, const char ffPrintWM(instance); else if(strcasecmp(line, "theme") == 0) ffPrintTheme(instance); + else if(strcasecmp(line, "wmtheme") == 0) + ffPrintWMTheme(instance); else if(strcasecmp(line, "icons") == 0) ffPrintIcons(instance); else if(strcasecmp(line, "font") == 0) diff --git a/src/fastfetch.h b/src/fastfetch.h index aeb83ebf4..69b28cc26 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -64,6 +64,8 @@ typedef struct FFconfig FFstrbuf deKey; FFstrbuf wmFormat; FFstrbuf wmKey; + FFstrbuf wmThemeFormat; + FFstrbuf wmThemeKey; FFstrbuf themeFormat; FFstrbuf themeKey; FFstrbuf iconsFormat; @@ -157,7 +159,8 @@ void ffPrintLogos(bool color); //Module functions -void ffGetTerminal(FFinstance* instance, FFstrbuf** exeName, FFstrbuf** processName, FFstrbuf** error); +void ffCalculateTerminal(FFinstance* instance, FFstrbuf** exeNamePtr, FFstrbuf** processNamePtr, FFstrbuf** errorPtr); +void ffCalculateWM(FFinstance* instance, FFstrbuf** prettyNamePtr, FFstrbuf** processNamePtr, FFstrbuf** errorPtr); void ffPrintCustom(FFinstance* instance, const char* key, const char* value); void ffPrintBreak(FFinstance* instance); @@ -172,6 +175,7 @@ void ffPrintShell(FFinstance* instance); void ffPrintResolution(FFinstance* instance); void ffPrintDesktopEnvironment(FFinstance* instance); void ffPrintWM(FFinstance* instance); +void ffPrintWMTheme(FFinstance* instance); void ffPrintTheme(FFinstance* instance); void ffPrintIcons(FFinstance* instance); void ffPrintFont(FFinstance* instance); diff --git a/src/flashfetch.c b/src/flashfetch.c index 5ed3019e1..1153d322b 100644 --- a/src/flashfetch.c +++ b/src/flashfetch.c @@ -22,6 +22,7 @@ int main(int argc, char** argv) ffPrintResolution(&instance); ffPrintDesktopEnvironment(&instance); ffPrintWM(&instance); + ffPrintWMTheme(&instance); ffPrintTheme(&instance); ffPrintIcons(&instance); ffPrintFont(&instance); diff --git a/src/modules/terminal.c b/src/modules/terminal.c index b81d350a6..4b6883a60 100644 --- a/src/modules/terminal.c +++ b/src/modules/terminal.c @@ -47,7 +47,7 @@ static void getTerminalName(FFinstance* instance, const char* pid, FFstrbuf* exe ffStrbufSetS(processName, name); } -void ffGetTerminal(FFinstance* instance, FFstrbuf** exeNamePtr, FFstrbuf** processNamePtr, FFstrbuf** errorPtr) +void ffCalculateTerminal(FFinstance* instance, FFstrbuf** exeNamePtr, FFstrbuf** processNamePtr, FFstrbuf** errorPtr) { static FFstrbuf exeName; static FFstrbuf processName; @@ -65,16 +65,17 @@ void ffGetTerminal(FFinstance* instance, FFstrbuf** exeNamePtr, FFstrbuf** proce if(init) return; + init = true; ffStrbufInit(&exeName); ffStrbufInit(&processName); + ffStrbufInit(&error); char ppid[256]; sprintf(ppid, "%i", getppid()); getTerminalName(instance, ppid, &exeName, &processName, &error); - init = true; } void ffPrintTerminal(FFinstance* instance) @@ -83,7 +84,7 @@ void ffPrintTerminal(FFinstance* instance) FFstrbuf* processName; FFstrbuf* error; - ffGetTerminal(instance, &exeName, &processName, &error); + ffCalculateTerminal(instance, &exeName, &processName, &error); if(error->length > 0) { diff --git a/src/modules/terminalfont.c b/src/modules/terminalfont.c index 2b24769c3..0d1e4cab0 100644 --- a/src/modules/terminalfont.c +++ b/src/modules/terminalfont.c @@ -54,29 +54,19 @@ static void printTTY(FFinstance* instance) void ffPrintTerminalFont(FFinstance* instance) { - FFstrbuf* procName; - FFstrbuf* error; - ffGetTerminal(instance, NULL, &procName, &error); + FFstrbuf* terminalExeName; + ffCalculateTerminal(instance, &terminalExeName, NULL, NULL); - if(error->length > 0) + if(terminalExeName->length == 0) { - if(instance->config.termFontFormat.length == 0) - ffPrintError(instance, &instance->config.termFontKey, "Terminal Font", "Terminal Font needs successfull terminal detection"); - else - printTerminalFont(instance, ""); - + ffPrintError(instance, &instance->config.termFontKey, "Terminal Font", "Terminal font needs successfull terminal detection"); return; } - if(ffStrbufIgnCaseCompS(procName, "konsole") == 0) + if(ffStrbufIgnCaseCompS(terminalExeName, "konsole") == 0) printKonsole(instance); - else if(ffStrbufIgnCaseCompS(procName, "login") == 0) + else if(ffStrbufIgnCaseCompS(terminalExeName, "login") == 0) printTTY(instance); else - { - if(instance->config.termFontFormat.length > 0) - printTerminalFont(instance, ""); - else - ffPrintError(instance, &instance->config.termFontKey, "Terminal Font", "Unknown terminal: %s", error->chars); - } + ffPrintError(instance, &instance->config.termFontKey, "Terminal Font", "Unknown terminal: %s", terminalExeName->chars); } diff --git a/src/modules/wm.c b/src/modules/wm.c index 4c52661b9..ed724ea6e 100644 --- a/src/modules/wm.c +++ b/src/modules/wm.c @@ -2,20 +2,39 @@ #include -void ffPrintWM(FFinstance* instance) +void ffCalculateWM(FFinstance* instance, FFstrbuf** prettyNamePtr, FFstrbuf** processNamePtr, FFstrbuf** errorPtr) { + static FFstrbuf prettyName; + static FFstrbuf processName; + static FFstrbuf error; + static bool init = false; + + if(prettyNamePtr != NULL) + *prettyNamePtr = &prettyName; + + if(processNamePtr != NULL) + *processNamePtr = &processName; + + if(errorPtr != NULL) + *errorPtr = &error; + + if(init) + return; + init = true; + + ffStrbufInit(&prettyName); + ffStrbufInit(&processName); + ffStrbufInit(&error); + DIR* proc = opendir("/proc/"); if(proc == NULL) { - ffPrintError(instance, &instance->config.wmKey, "WM", "opendir(\"/proc/\") == NULL"); + ffStrbufSetS(&error, "opendir(\"/proc/\") == NULL"); return; } struct dirent* dirent; - FF_STRBUF_CREATE(processName); - FF_STRBUF_CREATE(prettyName); - while((dirent = readdir(proc)) != NULL) { if(dirent->d_type != DT_DIR) @@ -37,23 +56,35 @@ void ffPrintWM(FFinstance* instance) if(prettyName.length == 0) { - ffPrintError(instance, &instance->config.wmKey, "WM", "No process name matches the name of known display managers"); + ffStrbufSetS(&error, "No process name matches the name of known display managers"); + return; + } +} + +void ffPrintWM(FFinstance* instance) +{ + FFstrbuf* prettyName; + FFstrbuf* processName; + FFstrbuf* error; + + ffCalculateWM(instance, &prettyName, &processName, &error); + + if(error->length > 0) + { + ffPrintError(instance, &instance->config.wmKey, "WM", error->chars); return; } if(instance->config.wmFormat.length == 0) { ffPrintLogoAndKey(instance, &instance->config.wmKey, "WM"); - ffStrbufPutTo(&prettyName, stdout); + ffStrbufPutTo(prettyName, stdout); } else { ffPrintFormatString(instance, &instance->config.wmKey, "WM", &instance->config.wmFormat, 2, - (FFformatarg){FF_FORMAT_ARG_TYPE_STRBUF, &processName}, - (FFformatarg){FF_FORMAT_ARG_TYPE_STRBUF, &prettyName} + (FFformatarg){FF_FORMAT_ARG_TYPE_STRBUF, processName}, + (FFformatarg){FF_FORMAT_ARG_TYPE_STRBUF, prettyName} ); } - - ffStrbufDestroy(&processName); - ffStrbufDestroy(&prettyName); } diff --git a/src/modules/wmtheme.c b/src/modules/wmtheme.c new file mode 100644 index 000000000..24a24431a --- /dev/null +++ b/src/modules/wmtheme.c @@ -0,0 +1,48 @@ +#include "fastfetch.h" + +static void printWMTheme(FFinstance* instance, const char* theme) +{ + if(instance->config.wmThemeFormat.length == 0) + { + ffPrintLogoAndKey(instance, &instance->config.wmThemeKey, "WM Theme"); + puts(theme); + } + else + { + ffPrintFormatString(instance, &instance->config.wmThemeKey, "WM Theme", &instance->config.wmThemeFormat, 1, + (FFformatarg){FF_FORMAT_ARG_TYPE_STRING, theme} + ); + } +} + +static void printKWin(FFinstance* instance) +{ + char theme[256]; + theme[0] = '\0'; + ffParsePropFileHome(instance, ".config/kwinrc", "theme=%s", theme); + + if(theme[0] == '\0') + { + ffPrintError(instance, &instance->config.wmThemeKey, "WM Theme", "Couldn't find \"theme=\" in \".config/kwinrc\""); + return; + } + + printWMTheme(instance, theme); +} + +void ffPrintWMTheme(FFinstance* instance) +{ + FFstrbuf* wmName; + ffCalculateWM(instance, &wmName, NULL, NULL); + + if(wmName->length == 0) + { + ffPrintError(instance, &instance->config.wmThemeKey, "WM Theme", "WM Theme needs sucessfull wm detection"); + return; + } + + if(ffStrbufIgnCaseCompS(wmName, "KWin") == 0) + printKWin(instance); + else + ffPrintError(instance, &instance->config.wmThemeKey, "WM Theme", "Unknown WM: %s", wmName->chars); +} diff --git a/src/util/FFstrbuf.c b/src/util/FFstrbuf.c index 8c637e1fb..08b7463f4 100644 --- a/src/util/FFstrbuf.c +++ b/src/util/FFstrbuf.c @@ -390,6 +390,7 @@ void ffStrbufSubstrAfter(FFstrbuf* strbuf, uint32_t index) memmove(strbuf->chars, strbuf->chars + index + 1, strbuf->length - index - 1); strbuf->length -= (index + 1); + strbuf->chars[strbuf->length] = '\0'; } void ffStrbufSubstrAfterLastC(FFstrbuf* strbuf, const char c) diff --git a/tests/performance.c b/tests/performance.c index 570dc6d8f..2d1c4a5c0 100644 --- a/tests/performance.c +++ b/tests/performance.c @@ -44,6 +44,7 @@ int main(int argc, char** argv) FASTFETCH_TEST_PERFORMANCE(ffPrintResolution(&instance)) FASTFETCH_TEST_PERFORMANCE(ffPrintDesktopEnvironment(&instance)) FASTFETCH_TEST_PERFORMANCE(ffPrintWM(&instance)) + FASTFETCH_TEST_PERFORMANCE(ffPrintWMTheme(&instance)) FASTFETCH_TEST_PERFORMANCE(ffPrintTheme(&instance)) FASTFETCH_TEST_PERFORMANCE(ffPrintIcons(&instance)) FASTFETCH_TEST_PERFORMANCE(ffPrintFont(&instance))