Files
FWS-PacManage/include/package-manager.h
T
BlackAngelTV cd5bed0f4d feat(ui): stream search results and show real download progress
Search now publishes provider results in small batches as they arrive, so
the first matches appear in the table while slower providers keep running.

The bottom status-bar bar is now a determinate indicator: pacman output is
streamed live and its (n/Total) package counter is parsed into a percentage
that fills the bar during install/update, instead of an indeterminate spinner.

Generated with Codebuff 🤖
Co-Authored-By: Codebuff <noreply@codebuff.com>
2026-08-29 20:17:45 +02:00

159 lines
6.1 KiB
C++

#pragma once
#include <string>
#include <vector>
#include <functional>
#include <map>
#include <algorithm>
#include <cctype>
// Information about a single package
struct PackageInfo {
std::string name;
std::string version;
std::string description;
std::string repository;
std::string size;
std::string installedSize;
// Id of the provider that owns this package (e.g. "pacman", "flatpak").
// Filled by the aggregating workers so operations can be routed to the
// right manager without the user picking one.
std::string provider;
// Every provider offering this package, after duplicate rows have been
// merged (used for display). The routing id stays in `provider`.
std::vector<std::string> providers;
bool installed = false;
bool updateAvailable = false;
};
// Merge packages that share the same name (offered by several providers) into
// a single row. The first occurrence keeps its metadata and provider id (so
// operations keep a deterministic default); later occurrences are folded in:
// installed/updateAvailable become true if any provider reports them, and
// their provider id is added to pkg.providers for display.
inline void mergeProviderDuplicates(std::vector<PackageInfo>& packages) {
// Every tagged package advertises at least its own provider.
for (auto& pkg : packages) {
if (pkg.providers.empty() && !pkg.provider.empty()) {
pkg.providers.push_back(pkg.provider);
}
}
std::map<std::string, std::size_t> firstIndex;
std::vector<std::size_t> toDrop;
for (std::size_t i = 0; i < packages.size(); ++i) {
const std::string& name = packages[i].name;
if (name.empty()) continue;
auto it = firstIndex.find(name);
if (it == firstIndex.end()) {
firstIndex.emplace(name, i);
continue;
}
PackageInfo& first = packages[it->second];
const PackageInfo& dup = packages[i];
first.installed = first.installed || dup.installed;
first.updateAvailable = first.updateAvailable || dup.updateAvailable;
if (!dup.provider.empty()) {
bool known = false;
for (const auto& p : first.providers) {
if (p == dup.provider) {
known = true;
break;
}
}
if (!known) first.providers.push_back(dup.provider);
}
toDrop.push_back(i);
}
for (auto it = toDrop.rbegin(); it != toDrop.rend(); ++it) {
packages.erase(packages.begin() + static_cast<std::ptrdiff_t>(*it));
}
}
// Sort aggregated results by provider, then by name (case-insensitive), so
// packages from the same manager stay grouped. Providers are ordered by their
// position in providerOrder (query order); providers absent from it sort last
// and are compared by id.
inline void sortByProviderThenName(std::vector<PackageInfo>& packages,
const std::vector<std::string>& providerOrder = {}) {
auto ciLess = [](const std::string& a, const std::string& b) {
std::size_t i = 0;
for (; i < a.size() && i < b.size(); ++i) {
unsigned char ca =
static_cast<unsigned char>(std::tolower(static_cast<unsigned char>(a[i])));
unsigned char cb =
static_cast<unsigned char>(std::tolower(static_cast<unsigned char>(b[i])));
if (ca != cb) return ca < cb;
}
return a.size() < b.size();
};
auto rank = [&](const std::string& id) {
for (std::size_t i = 0; i < providerOrder.size(); ++i) {
if (providerOrder[i] == id) return static_cast<int>(i);
}
return static_cast<int>(providerOrder.size());
};
std::stable_sort(packages.begin(), packages.end(),
[&](const PackageInfo& a, const PackageInfo& b) {
int ra = rank(a.provider);
int rb = rank(b.provider);
if (ra != rb) return ra < rb;
if (a.provider != b.provider) return a.provider < b.provider;
return ciLess(a.name, b.name);
});
}
// Progress reported during an operation: a human-readable message plus an
// optional percentage (0-100) when the tool exposes real progress, or -1 when
// the exact position is unknown. -1 lets the UI keep an indeterminate bar.
using ProgressCallback = std::function<void(const std::string& message, int percent)>;
// Abstract interface for the package manager
class IPackageManager {
public:
virtual ~IPackageManager() = default;
// Identity
virtual std::string getName() const = 0;
virtual std::string getVersion() const = 0;
virtual std::string getDescription() const = 0;
// Capabilities
virtual bool canSearch() const = 0;
virtual bool canInstall() const = 0;
virtual bool canRemove() const = 0;
virtual bool canUpdate() const = 0;
virtual bool canListInstalled() const = 0;
// Operations
virtual std::vector<PackageInfo> search(const std::string& query) = 0;
virtual bool install(const std::string& packageName,
ProgressCallback progressCallback = nullptr) = 0;
virtual bool remove(const std::string& packageName,
ProgressCallback progressCallback = nullptr) = 0;
virtual bool update(const std::string& packageName = "",
ProgressCallback progressCallback = nullptr) = 0;
virtual std::vector<PackageInfo> listInstalled() = 0;
virtual std::vector<PackageInfo> listUpdates() = 0;
virtual bool refresh(ProgressCallback progressCallback = nullptr) = 0;
// Check if the package manager is available on the system
virtual bool isAvailable() const = 0;
// Whether privileged operations need a password (e.g. sudo for pacman).
// Providers that run per-user (e.g. flatpak) can keep the default.
virtual bool requiresPassword() const { return false; }
// Last error reported by the most recent failed operation, if any. Used
// to surface the real command output to the user instead of a generic
// failure message.
virtual std::string lastError() const { return {}; }
};