feat(pacman): report install origin and size for installed packages
listInstalled only returned name and version, leaving the size and the origin repository empty for installed packages. Enrich each entry with its installed size (parsed from pacman -Qi) and the repository it was installed from (parsed from pacman -Sl), defaulting foreign/AUR packages to "AUR" so the origin is always visible. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
3 files changed
+146
-1
No files matched your search
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "package-manager.h"
|
||||
#include <map>
|
||||
|
||||
// Native Arch Linux package manager, integrated directly into the application
|
||||
class PacmanManager : public IPackageManager {
|
||||
@@ -25,6 +26,14 @@ public:
|
||||
// Exposed as static so the parsing can be unit-tested without invoking pacman.
|
||||
static std::vector<PackageInfo> parseInstalledOutput(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<std::string, std::string> parseInstalledSizes(const std::string& output);
|
||||
|
||||
// Parse `pacman -Sl` output ("repo name version" per line) into a name -> repository map.
|
||||
// Exposed as static so the parsing can be unit-tested without invoking pacman.
|
||||
static std::map<std::string, std::string> parseSyncRepos(const std::string& output);
|
||||
|
||||
std::vector<PackageInfo> search(const std::string& query) override;
|
||||
bool install(const std::string& packageName,
|
||||
std::function<void(const std::string&)> progressCallback) override;
|
||||
|
||||
+92
-1
@@ -117,7 +117,31 @@ bool PacmanManager::update(const std::string& packageName,
|
||||
}
|
||||
|
||||
std::vector<PackageInfo> PacmanManager::listInstalled() {
|
||||
return parseInstalledOutput(exec("pacman -Q 2>&1"));
|
||||
auto packages = parseInstalledOutput(exec("pacman -Q 2>&1"));
|
||||
|
||||
// Enrich each installed package with its installed size (shown as "Taille")
|
||||
// and the repository/origin it was installed from. Both come from a single
|
||||
// subprocess each, which is fast enough to run on page load.
|
||||
auto sizes = parseInstalledSizes(exec("pacman -Qi 2>&1"));
|
||||
auto repos = parseSyncRepos(exec("pacman -Sl 2>&1"));
|
||||
|
||||
for (auto& pkg : packages) {
|
||||
auto sizeIt = sizes.find(pkg.name);
|
||||
if (sizeIt != sizes.end()) {
|
||||
pkg.size = sizeIt->second;
|
||||
pkg.installedSize = sizeIt->second;
|
||||
}
|
||||
|
||||
auto repoIt = repos.find(pkg.name);
|
||||
if (repoIt != repos.end()) {
|
||||
pkg.repository = repoIt->second;
|
||||
} else {
|
||||
// Not present in any sync repository: foreign package (AUR / manual build).
|
||||
pkg.repository = "AUR";
|
||||
}
|
||||
}
|
||||
|
||||
return packages;
|
||||
}
|
||||
|
||||
// Parse `pacman -Qu` output (one "name version" line per outdated package).
|
||||
@@ -194,3 +218,70 @@ bool PacmanManager::isPackageInstalled(const std::string& name) {
|
||||
std::string cmd = "pacman -Qi " + name + " > /dev/null 2>&1";
|
||||
return system(cmd.c_str()) == 0;
|
||||
}
|
||||
|
||||
// Parse `pacman -Qi` output (one blank-line-separated block per package) into a
|
||||
// map of package name -> installed size (e.g. "263.47 MiB").
|
||||
std::map<std::string, std::string> PacmanManager::parseInstalledSizes(
|
||||
const std::string& output) {
|
||||
std::map<std::string, std::string> sizes;
|
||||
std::istringstream iss(output);
|
||||
std::string line;
|
||||
|
||||
std::string currentName;
|
||||
while (std::getline(iss, line)) {
|
||||
// A blank line ends the current package block.
|
||||
if (line.empty()) {
|
||||
currentName.clear();
|
||||
continue;
|
||||
}
|
||||
|
||||
size_t colon = line.find(':');
|
||||
if (colon == std::string::npos) continue;
|
||||
|
||||
std::string field = line.substr(0, colon);
|
||||
// Trim trailing whitespace from the label (labels are padded to align).
|
||||
while (!field.empty() && field.back() == ' ') field.pop_back();
|
||||
|
||||
std::string value = line.substr(colon + 1);
|
||||
// Trim leading whitespace from the value.
|
||||
size_t start = value.find_first_not_of(" \t");
|
||||
if (start != std::string::npos) {
|
||||
value = value.substr(start);
|
||||
}
|
||||
|
||||
if (field == "Name") {
|
||||
currentName = value;
|
||||
} else if (field == "Installed Size" && !currentName.empty()) {
|
||||
sizes[currentName] = value;
|
||||
}
|
||||
}
|
||||
|
||||
return sizes;
|
||||
}
|
||||
|
||||
// Parse `pacman -Sl` output ("repo name version" per sync-DB package) into a map of
|
||||
// package name -> repository, so the origin of each installed package can be shown.
|
||||
std::map<std::string, std::string> PacmanManager::parseSyncRepos(
|
||||
const std::string& output) {
|
||||
std::map<std::string, std::string> repos;
|
||||
std::istringstream iss(output);
|
||||
std::string line;
|
||||
|
||||
bool firstLine = true;
|
||||
while (std::getline(iss, line)) {
|
||||
if (firstLine) {
|
||||
firstLine = false;
|
||||
continue; // Skip the "Repository Name Version" header.
|
||||
}
|
||||
if (line.empty()) continue;
|
||||
|
||||
std::istringstream ls(line);
|
||||
std::string repo;
|
||||
std::string name;
|
||||
if (!(ls >> repo >> name)) continue;
|
||||
|
||||
repos[name] = repo;
|
||||
}
|
||||
|
||||
return repos;
|
||||
}
|
||||
@@ -36,12 +36,57 @@ static void testParseInstalledOutput_emptyInput() {
|
||||
assert(pkgs.empty());
|
||||
}
|
||||
|
||||
static void testParseInstalledSizes_parsesBlocks() {
|
||||
std::string output =
|
||||
"Name : firefox\n"
|
||||
"Version : 128.0-1\n"
|
||||
"Installed Size : 263.47 MiB\n"
|
||||
"Install Date : 2026-01-01\n"
|
||||
"\n"
|
||||
"Name : vim\n"
|
||||
"Installed Size : 12.3 MiB\n";
|
||||
|
||||
auto sizes = PacmanManager::parseInstalledSizes(output);
|
||||
|
||||
assert(sizes.size() == 2);
|
||||
assert(sizes["firefox"] == "263.47 MiB");
|
||||
assert(sizes["vim"] == "12.3 MiB");
|
||||
}
|
||||
|
||||
static void testParseInstalledSizes_emptyInput() {
|
||||
assert(PacmanManager::parseInstalledSizes("").empty());
|
||||
}
|
||||
|
||||
static void testParseSyncRepos_skipsHeader() {
|
||||
std::string output =
|
||||
"Repository Name Version\n"
|
||||
"core linux 6.6-1\n"
|
||||
"extra firefox 128.0-1\n"
|
||||
"multilib gcc-libs 13.2-1\n";
|
||||
|
||||
auto repos = PacmanManager::parseSyncRepos(output);
|
||||
|
||||
assert(repos.size() == 3);
|
||||
assert(repos["linux"] == "core");
|
||||
assert(repos["firefox"] == "extra");
|
||||
assert(repos["gcc-libs"] == "multilib");
|
||||
}
|
||||
|
||||
static void testParseSyncRepos_emptyInput() {
|
||||
assert(PacmanManager::parseSyncRepos("").empty());
|
||||
}
|
||||
|
||||
int main() {
|
||||
testParseInstalledOutput_parsesSimpleLines();
|
||||
testParseInstalledOutput_keepsHyphensInNames();
|
||||
testParseInstalledOutput_skipsEmptyLines();
|
||||
testParseInstalledOutput_emptyInput();
|
||||
|
||||
testParseInstalledSizes_parsesBlocks();
|
||||
testParseInstalledSizes_emptyInput();
|
||||
testParseSyncRepos_skipsHeader();
|
||||
testParseSyncRepos_emptyInput();
|
||||
|
||||
std::cout << "All pacman-manager tests passed." << std::endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user