From f24f6756b120022ebb6bfadb900f41839789b16e Mon Sep 17 00:00:00 2001 From: DarN Date: Sat, 15 May 2021 13:21:59 +0200 Subject: [PATCH 1/4] Added branch detection for Manjaro --- src/modules/packages.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/modules/packages.c b/src/modules/packages.c index 5eb0050b0..f5ff8da9e 100644 --- a/src/modules/packages.c +++ b/src/modules/packages.c @@ -53,6 +53,37 @@ static uint32_t getNumStrings(const char* filename, const char* needle) return count; } +static void getActiveBranch() +{ + FILE* file = fopen("/etc/pacman-mirrors.conf", "r"); + if(file == NULL) + return; // No file or not Manjaro/based + + char* line = NULL; + size_t len = 0, strip; + uint_fast8_t match = 0; + + while (getline(&line, &len, file) != EOF) + { + if((strip = strspn(line, "\t ") > 0)) { + if(strip != len) + memmove(line, line + strip, len + 1 - strip); + } + if((match = sscanf(line, "Branch = %s", line)) > 0) + break; + } + + fclose(file); + + if(match > 0) + printf("[%s]", line); + else + printf("[stable]"); // defaults to stable + + if(line != NULL) + free(line); +} + void ffPrintPackages(FFinstance* instance) { uint32_t pacman = getNumElements("/var/lib/pacman/local", DT_DIR); @@ -77,6 +108,8 @@ void ffPrintPackages(FFinstance* instance) if(name > 0) \ { \ printf("%u ("#name")", name); \ + if(name == pacman) \ + getActiveBranch(); \ if((all = all - name) > 0) \ printf(", "); \ }; From e400ee4490cef47b9139b25de8a76dceaba488ba Mon Sep 17 00:00:00 2001 From: DarN Date: Sat, 15 May 2021 13:44:16 +0200 Subject: [PATCH 2/4] Tweak --- src/modules/packages.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/modules/packages.c b/src/modules/packages.c index f5ff8da9e..7c34472a5 100644 --- a/src/modules/packages.c +++ b/src/modules/packages.c @@ -57,7 +57,7 @@ static void getActiveBranch() { FILE* file = fopen("/etc/pacman-mirrors.conf", "r"); if(file == NULL) - return; // No file or not Manjaro/based + return; // No file or not Manjaro[based] char* line = NULL; size_t len = 0, strip; @@ -75,10 +75,7 @@ static void getActiveBranch() fclose(file); - if(match > 0) - printf("[%s]", line); - else - printf("[stable]"); // defaults to stable + match > 0 ? printf("[%s]", line) : printf("[stable]"); if(line != NULL) free(line); From 0e2deb3534f6124a1c0652ae764b48425a16f8d3 Mon Sep 17 00:00:00 2001 From: DarN Date: Sat, 15 May 2021 17:21:02 +0200 Subject: [PATCH 3/4] Rewrote with suggestions --- src/modules/packages.c | 44 +++++++++++++++--------------------------- 1 file changed, 16 insertions(+), 28 deletions(-) diff --git a/src/modules/packages.c b/src/modules/packages.c index 7c34472a5..618b4b220 100644 --- a/src/modules/packages.c +++ b/src/modules/packages.c @@ -53,32 +53,10 @@ static uint32_t getNumStrings(const char* filename, const char* needle) return count; } -static void getActiveBranch() +static void getActiveBranchManjaro(FFstrbuf buffer) { - FILE* file = fopen("/etc/pacman-mirrors.conf", "r"); - if(file == NULL) - return; // No file or not Manjaro[based] - - char* line = NULL; - size_t len = 0, strip; - uint_fast8_t match = 0; - - while (getline(&line, &len, file) != EOF) - { - if((strip = strspn(line, "\t ") > 0)) { - if(strip != len) - memmove(line, line + strip, len + 1 - strip); - } - if((match = sscanf(line, "Branch = %s", line)) > 0) - break; - } - - fclose(file); - - match > 0 ? printf("[%s]", line) : printf("[stable]"); - - if(line != NULL) - free(line); + if(ffParsePropFile("/etc/pacman-mirrors.conf", "Branch = ", &buffer) && buffer.length == 0) + ffStrbufSetS(&buffer, "stable"); } void ffPrintPackages(FFinstance* instance) @@ -96,6 +74,10 @@ void ffPrintPackages(FFinstance* instance) ffPrintError(instance, FF_PACKAGES_MODULE_NAME, 0, &instance->config.packagesKey, &instance->config.packagesFormat, FF_PACKAGES_NUM_FORMAT_ARGS, "No packages from known package managers found"); return; } + FFstrbuf manjaroBranch; + ffStrbufInit(&manjaroBranch); + getActiveBranchManjaro(manjaroBranch); + ffStrbufRecalculateLength(&manjaroBranch); // needed for later length comparison... if(instance->config.packagesFormat.length == 0) { @@ -105,13 +87,18 @@ void ffPrintPackages(FFinstance* instance) if(name > 0) \ { \ printf("%u ("#name")", name); \ - if(name == pacman) \ - getActiveBranch(); \ if((all = all - name) > 0) \ printf(", "); \ }; - FF_PRINT_PACKAGE(pacman) + if(pacman > 0) + { + printf("%u (pacman)", pacman); + if(manjaroBranch.length > 0) + printf("[%s]", manjaroBranch.chars); + if((all = all - pacman) > 0) + printf(", "); + }; FF_PRINT_PACKAGE(xbps) FF_PRINT_PACKAGE(dpkg) FF_PRINT_PACKAGE(flatpak) @@ -135,4 +122,5 @@ void ffPrintPackages(FFinstance* instance) {FF_FORMAT_ARG_TYPE_UINT, &snap} }); } + ffStrbufDestroy(&manjaroBranch); } From 0dbcce84f8cb4f68f009d74dee37b27bd5c7aec6 Mon Sep 17 00:00:00 2001 From: DarN Date: Sat, 15 May 2021 18:12:10 +0200 Subject: [PATCH 4/4] Implemented changes --- src/modules/packages.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/modules/packages.c b/src/modules/packages.c index 618b4b220..db8ee36b8 100644 --- a/src/modules/packages.c +++ b/src/modules/packages.c @@ -53,12 +53,6 @@ static uint32_t getNumStrings(const char* filename, const char* needle) return count; } -static void getActiveBranchManjaro(FFstrbuf buffer) -{ - if(ffParsePropFile("/etc/pacman-mirrors.conf", "Branch = ", &buffer) && buffer.length == 0) - ffStrbufSetS(&buffer, "stable"); -} - void ffPrintPackages(FFinstance* instance) { uint32_t pacman = getNumElements("/var/lib/pacman/local", DT_DIR); @@ -76,8 +70,9 @@ void ffPrintPackages(FFinstance* instance) } FFstrbuf manjaroBranch; ffStrbufInit(&manjaroBranch); - getActiveBranchManjaro(manjaroBranch); - ffStrbufRecalculateLength(&manjaroBranch); // needed for later length comparison... + + if(ffParsePropFile("/etc/pacman-mirrors.conf", "Branch =", &manjaroBranch) && manjaroBranch.length == 0) + ffStrbufSetS(&manjaroBranch, "stable"); if(instance->config.packagesFormat.length == 0) { @@ -99,6 +94,9 @@ void ffPrintPackages(FFinstance* instance) if((all = all - pacman) > 0) printf(", "); }; + + ffStrbufDestroy(&manjaroBranch); + FF_PRINT_PACKAGE(xbps) FF_PRINT_PACKAGE(dpkg) FF_PRINT_PACKAGE(flatpak) @@ -122,5 +120,4 @@ void ffPrintPackages(FFinstance* instance) {FF_FORMAT_ARG_TYPE_UINT, &snap} }); } - ffStrbufDestroy(&manjaroBranch); }