diff --git a/include/pacman-manager.h b/include/pacman-manager.h index 8094c92..1747acd 100644 --- a/include/pacman-manager.h +++ b/include/pacman-manager.h @@ -2,6 +2,7 @@ #include "package-manager.h" #include +#include // Native Arch Linux package manager, integrated directly into the application class PacmanManager : public IPackageManager { @@ -29,6 +30,11 @@ public: // Exposed as static so the parsing can be unit-tested without invoking pacman. static std::vector parseInstalledOutput(const std::string& output); + // Parse `pacman -Q` output into a set of installed package names, used to + // mark search results as installed without one subprocess per result. + // Exposed as static so the parsing can be unit-tested without invoking pacman. + static std::set parseInstalledNames(const std::string& output); + // Parse `pacman -Qi` output (one block per package) into a name -> installed size map. // Exposed as static so the parsing can be unit-tested without invoking pacman. static std::map parseInstalledSizes(const std::string& output); diff --git a/src/pacman-manager.cpp b/src/pacman-manager.cpp index 6f73386..d4867ca 100644 --- a/src/pacman-manager.cpp +++ b/src/pacman-manager.cpp @@ -6,6 +6,7 @@ #include #include #include +#include // Helper to run shell commands and capture output static std::string exec(const char* cmd) { @@ -105,6 +106,11 @@ std::vector PacmanManager::search(const std::string& query) { std::string cmd = "pacman -Ss " + query + " 2>&1"; std::string output = exec(cmd.c_str()); + // Load the installed packages once, so marking search results as + // installed is an in-memory lookup instead of one `pacman -Qi` subprocess + // per result (which made searches with many matches take minutes). + std::set installed = parseInstalledNames(exec("pacman -Q 2>&1")); + std::istringstream iss(output); std::string line; @@ -127,7 +133,7 @@ std::vector PacmanManager::search(const std::string& query) { } } - pkg.installed = isPackageInstalled(pkg.name); + pkg.installed = installed.count(pkg.name) > 0; results.push_back(pkg); } // Description line (starts with spaces) @@ -292,6 +298,25 @@ std::vector PacmanManager::parseInstalledOutput(const std::string& return results; } +std::set PacmanManager::parseInstalledNames(const std::string& output) { + std::set names; + std::istringstream iss(output); + std::string line; + + while (std::getline(iss, line)) { + if (line.empty()) continue; + + // Same split as parseInstalledOutput: the first space separates the + // name from the version (hyphens in names are kept intact). + size_t spacePos = line.find(' '); + if (spacePos == std::string::npos) continue; + + names.insert(line.substr(0, spacePos)); + } + + return names; +} + bool PacmanManager::isPackageInstalled(const std::string& name) { std::string cmd = "pacman -Qi " + name + " > /dev/null 2>&1"; return system(cmd.c_str()) == 0; diff --git a/tests/pacman-manager.test.cpp b/tests/pacman-manager.test.cpp index 64a9237..e169848 100644 --- a/tests/pacman-manager.test.cpp +++ b/tests/pacman-manager.test.cpp @@ -36,6 +36,29 @@ static void testParseInstalledOutput_emptyInput() { assert(pkgs.empty()); } +static void testParseInstalledNames_parsesNames() { + std::string output = "firefox 128.0\npython-pip 24.0-1\nvim 9.1.0000\n"; + auto names = PacmanManager::parseInstalledNames(output); + + assert(names.size() == 3); + assert(names.count("firefox") == 1); + assert(names.count("python-pip") == 1); + assert(names.count("vim") == 1); +} + +static void testParseInstalledNames_skipsEmptyLines() { + std::string output = "firefox 128.0\n\nvim 9.1.0000\n"; + auto names = PacmanManager::parseInstalledNames(output); + + assert(names.size() == 2); + assert(names.count("firefox") == 1); + assert(names.count("vim") == 1); +} + +static void testParseInstalledNames_emptyInput() { + assert(PacmanManager::parseInstalledNames("").empty()); +} + static void testParseInstalledSizes_parsesBlocks() { std::string output = "Name : firefox\n" @@ -96,6 +119,10 @@ int main() { testParseInstalledOutput_skipsEmptyLines(); testParseInstalledOutput_emptyInput(); + testParseInstalledNames_parsesNames(); + testParseInstalledNames_skipsEmptyLines(); + testParseInstalledNames_emptyInput(); + testParseInstalledSizes_parsesBlocks(); testParseInstalledSizes_emptyInput(); testParseSyncRepos_skipsHeader();