From ed7e186f4b45fa5d27fef9bd646d9a6903bc9728 Mon Sep 17 00:00:00 2001 From: Linus Dierheimer Date: Wed, 7 Apr 2021 12:55:11 +0200 Subject: [PATCH] faster plasma detection --- CMakeLists.txt | 1 + src/fastfetch.h | 1 + src/helpers/calculateGtk.c | 2 +- src/helpers/calculatePlasma.c | 67 +++++++++++++++++++++++++++++++++++ src/modules/font.c | 39 +++++--------------- src/modules/icons.c | 18 ++++++---- src/modules/theme.c | 16 ++++++--- 7 files changed, 101 insertions(+), 43 deletions(-) create mode 100644 src/helpers/calculatePlasma.c diff --git a/CMakeLists.txt b/CMakeLists.txt index e51ac9536..3c642658f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,6 +48,7 @@ set(SRCS src/common.c src/logo.c src/util/FFstrbuf.c + src/helpers/calculatePlasma.c src/helpers/calculateGtk.c src/helpers/calculateTerminal.c src/helpers/calculateWM.c diff --git a/src/fastfetch.h b/src/fastfetch.h index cf6006891..50f015361 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -161,6 +161,7 @@ void ffPrintLogos(bool color); //Helper functions +void ffCalculatePlasma(FFinstance* instance, FFstrbuf** themeNamePtr, FFstrbuf** iconsNamePtr, FFstrbuf** fontNamePtr); void ffCalculateGTK2(FFinstance* instance, FFstrbuf** themeNamePtr, FFstrbuf** iconsNamePtr, FFstrbuf** fontNamePtr); void ffCalculateGTK4(FFinstance* instance, FFstrbuf** themeNamePtr, FFstrbuf** iconsNamePtr, FFstrbuf** fontNamePtr); void ffCalculateGTK3(FFinstance* instance, FFstrbuf** themeNamePtr, FFstrbuf** iconsNamePtr, FFstrbuf** fontNamePtr); diff --git a/src/helpers/calculateGtk.c b/src/helpers/calculateGtk.c index 8b259fb52..fba4654c4 100644 --- a/src/helpers/calculateGtk.c +++ b/src/helpers/calculateGtk.c @@ -165,7 +165,7 @@ static void calculateGTKFromConfigDir(const char* configDir, const char* version FFstrbuf file4; ffStrbufInitA(&file4, 64); ffStrbufAppendS(&file4, configDir); - ffStrbufAppendS(&file4, "gtkrc-"); + ffStrbufAppendS(&file4, ".gtkrc-"); ffStrbufAppendS(&file4, version); ffStrbufAppendS(&file4, ".0"); parseGTKConfigFile(&file4, themeNamePtr, iconsNamePtr, fontNamePtr); diff --git a/src/helpers/calculatePlasma.c b/src/helpers/calculatePlasma.c new file mode 100644 index 000000000..0ba8c422e --- /dev/null +++ b/src/helpers/calculatePlasma.c @@ -0,0 +1,67 @@ +#include "fastfetch.h" + +void ffCalculatePlasma(FFinstance* instance, FFstrbuf** themeNamePtr, FFstrbuf** iconsNamePtr, FFstrbuf** fontNamePtr) +{ + static FFstrbuf themeName; + static FFstrbuf iconsName; + static FFstrbuf fontName; + + if(themeNamePtr != NULL) + *themeNamePtr = &themeName; + + if(iconsNamePtr != NULL) + *iconsNamePtr = &iconsName; + + if(fontNamePtr != NULL) + *fontNamePtr = &fontName; + + static bool init = false; + if(init) + return; + init = true; + + ffStrbufInit(&themeName); + ffStrbufInit(&iconsName); + ffStrbufInit(&fontName); + + FF_STRBUF_CREATE(kdeglobalsFile); + ffStrbufAppendS(&kdeglobalsFile, instance->state.passwd->pw_dir); + ffStrbufAppendS(&kdeglobalsFile, "/.config/kdeglobals"); + + FILE* kdeglobals = fopen(kdeglobalsFile.chars, "r"); + if(kdeglobals == NULL) + { + ffStrbufDestroy(&kdeglobalsFile); + return; + } + + char* line = NULL; + size_t len = 0; + + while(getline(&line, &len, kdeglobals) != -1) + { + sscanf(line, "widgetStyle=%[^\n]", themeName.chars); + sscanf(line, "widgetStyle=\"%[^\"]+", themeName.chars); + + sscanf(line, "Theme=%[^\n]", iconsName.chars); + sscanf(line, "Theme=\"%[^\"]+", iconsName.chars); + + sscanf(line, "font=%[^\n]", fontName.chars); + sscanf(line, "font=\"%[^\"]+", fontName.chars); + } + + ffStrbufRecalculateLength(&themeName); + ffStrbufRecalculateLength(&iconsName); + ffStrbufRecalculateLength(&fontName); + + if(line != NULL) + free(line); + + fclose(kdeglobals); + ffStrbufDestroy(&kdeglobalsFile); + + //When using Noto Sans 10, the default font in KDE Plasma, the font entry is deleted from the config file instead of set. + //So the pure existence of the file sets this font value if not set other in the file itself. + if(fontName.length == 0) + ffStrbufAppendS(&fontName, "Noto Sans, 10"); +} diff --git a/src/modules/font.c b/src/modules/font.c index 283c772aa..5e68a02af 100644 --- a/src/modules/font.c +++ b/src/modules/font.c @@ -4,20 +4,8 @@ void ffPrintFont(FFinstance* instance) { - FF_STRBUF_CREATE(plasmaFile); - ffStrbufAppendS(&plasmaFile, instance->state.passwd->pw_dir); - ffStrbufAppendS(&plasmaFile, "/.config/kdeglobals"); - - //When using Noto Sans 10, the default font in KDE Plasma, the font entry is deleted from the config file instead of set. - //So the pure existence of the file sets this font value if not set other in the file itself. - bool plasmaFileExists = access(plasmaFile.chars, F_OK) == 0; - - char plasma[256]; - plasma[0] = '\0'; - if(plasmaFileExists) - ffParsePropFile(plasmaFile.chars, "font=%[^\n]", plasma); - - ffStrbufDestroy(&plasmaFile); + FFstrbuf* plasma; + ffCalculatePlasma(instance, NULL, NULL, &plasma); FFstrbuf* gtk2; ffCalculateGTK2(instance, NULL, NULL, >k2); @@ -28,28 +16,17 @@ void ffPrintFont(FFinstance* instance) FFstrbuf* gtk4; ffCalculateGTK4(instance, NULL, NULL, >k4); - if(plasma[0] == '\0' && !plasmaFileExists && gtk2->length == 0 && gtk3->length == 0 && gtk4->length == 0) + if(plasma->length == 0 && gtk2->length == 0 && gtk3->length == 0 && gtk4->length == 0) { ffPrintError(instance, &instance->config.fontKey, "Font", "No fonts found"); return; } FF_STRBUF_CREATE(plasmaName); - FF_STRBUF_CREATE(plasmaPretty); double plasmaSize; - - if(plasma[0] == '\0' && plasmaFileExists) - { - strcpy(plasma, "Noto Sans, 10"); - ffStrbufSetS(&plasmaName, "Noto Sans"); - plasmaSize = 10; - ffStrbufSetS(&plasmaPretty, "Noto Sans (10pt)"); - } - else - { - ffParseFont(plasma, &plasmaName, &plasmaSize); - ffFontPretty(&plasmaPretty, &plasmaName, plasmaSize); - } + ffParseFont(plasma->chars, &plasmaName, &plasmaSize); + FF_STRBUF_CREATE(plasmaPretty); + ffFontPretty(&plasmaPretty, &plasmaName, plasmaSize); FF_STRBUF_CREATE(gtk2Name); double gtk2Size; @@ -76,7 +53,7 @@ void ffPrintFont(FFinstance* instance) { ffPrintLogoAndKey(instance, &instance->config.fontKey, "Font"); - if(plasma[0] != '\0') + if(plasma->length > 0) { ffStrbufWriteTo(&plasmaPretty, stdout); fputs(" [Plasma]", stdout); @@ -120,4 +97,6 @@ void ffPrintFont(FFinstance* instance) ffStrbufDestroy(>k3Name); ffStrbufDestroy(>k3Pretty); + + ffStrbufDestroy(>kPretty); } diff --git a/src/modules/icons.c b/src/modules/icons.c index 5a6fc8210..81841fded 100644 --- a/src/modules/icons.c +++ b/src/modules/icons.c @@ -2,8 +2,8 @@ void ffPrintIcons(FFinstance* instance) { - char plasma[256]; - ffParsePropFileHome(instance, ".config/kdeglobals", "Theme=%[^\n]", plasma); + FFstrbuf* plasma; + ffCalculatePlasma(instance, NULL, &plasma, NULL); FFstrbuf* gtk2; ffCalculateGTK2(instance, NULL, >k2, NULL); @@ -14,7 +14,7 @@ void ffPrintIcons(FFinstance* instance) FFstrbuf* gtk4; ffCalculateGTK4(instance, NULL, >k4, NULL); - if(plasma[0] == '\0' && gtk2->length == 0 && gtk3->length == 0 && gtk4->length == 0) + if(plasma->length == 0 && gtk2->length == 0 && gtk3->length == 0 && gtk4->length == 0) { ffPrintError(instance, &instance->config.iconsKey, "Icons", "No icons found"); return; @@ -27,10 +27,14 @@ void ffPrintIcons(FFinstance* instance) { ffPrintLogoAndKey(instance, &instance->config.iconsKey, "Icons"); - if(plasma[0] == '\0') - printf("Breeze [Plasma], "); - else - printf("%s [Plasma], ", plasma); + if(plasma->length > 0) + { + ffStrbufWriteTo(plasma, stdout); + fputs(" [Plasma]", stdout); + + if(gtkPretty.length > 0) + fputs(", ", stdout); + } ffStrbufPutTo(>kPretty, stdout); } diff --git a/src/modules/theme.c b/src/modules/theme.c index bb990699f..ca60af729 100644 --- a/src/modules/theme.c +++ b/src/modules/theme.c @@ -2,8 +2,8 @@ void ffPrintTheme(FFinstance* instance) { - char plasma[256]; - ffParsePropFileHome(instance, ".config/kdeglobals", "Name=%[^\n]", plasma); + FFstrbuf* plasma; + ffCalculatePlasma(instance, &plasma, NULL, NULL); FFstrbuf* gtk2; ffCalculateGTK2(instance, >k2, NULL, NULL); @@ -14,7 +14,7 @@ void ffPrintTheme(FFinstance* instance) FFstrbuf* gtk4; ffCalculateGTK4(instance, >k4, NULL, NULL); - if(plasma[0] == '\0' && gtk2->length == 0 && gtk3->length == 0 && gtk4->length == 0) + if(plasma->length == 0 && gtk2->length == 0 && gtk3->length == 0 && gtk4->length == 0) { ffPrintError(instance, &instance->config.themeKey, "Theme", "No themes found"); return; @@ -27,8 +27,14 @@ void ffPrintTheme(FFinstance* instance) { ffPrintLogoAndKey(instance, &instance->config.themeKey, "Theme"); - if(plasma[0] != '\0') - printf("%s [Plasma], ", plasma); + if(plasma->length > 0) + { + ffStrbufWriteTo(plasma, stdout); + fputs(" [Plasma]", stdout); + + if(gtkPretty.length > 0) + fputs(", ", stdout); + } ffStrbufPutTo(>kPretty, stdout); }