perf(progress): report real progress instead of a bouncing bar

pacman restarts its "(1/24)" counter at every phase of a transaction, so the
bar was fed 0 percent, 100 percent, 0 percent again a few times per update —
and Flatpak/AUR buffered their whole output, which left it indeterminate for
the entire run. A shared ProgressTracker now folds every counter, file
percentage and package step into one value that only moves forward, the
providers stream their output as it is produced, and each provider owns a
slice of the bar so a multi-provider update keeps advancing. pacman -Qu is
cached like the other read queries, so the sidebar badge, the updates screen
and the update button share a single process.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
BlackAngelTV committed 2026-09-16 22:40:39 +02:00
1 parent ed58e87520
commit b26e907221
12 files changed
+590 -138

No files matched your search

+19 -63
View File
@@ -1,4 +1,7 @@
#include "pacman-manager.h"
#include "progress-tracker.h"
#include <array>
#include <cctype>
#include <chrono>
@@ -60,61 +63,21 @@ static std::string lastOutputLines(const std::string& output, int keep = 5) {
return out;
}
// Read a (current/Total) package counter out of a pacman line; see definition
// below. Declared first because execPrivileged uses it.
static int percentFromLine(const std::string& line);
// Run a privileged pacman command as root, piping the password to `sudo -S`
// on stdin and capturing combined stdout+stderr. Lines are delivered to the
// progress callback as soon as they are produced (pacman prints the download
// progress as it goes, so this keeps the UI bar live). The full output is
// still returned for auth/error checks.
// on stdin and capturing combined stdout+stderr. Output is delivered to the
// progress callback as pacman produces it — including its in-place download
// meters, which is what makes the bar move during a long download — and the
// full output is still returned for the auth/error checks.
//
// The percentage comes from ProgressTracker, not from the raw counters: pacman
// restarts "(1/24)" at every phase of the transaction, so forwarding those
// numbers made the bar go backwards several times per update.
static std::string execPrivileged(const std::string& pacmanCmd,
const std::string& password,
ProgressCallback progressCallback) {
std::string full = "printf '%s\\n' " + shellQuote(password)
+ " | sudo -S " + pacmanCmd + " 2>&1";
std::array<char, 8192> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(full.c_str(), "r"), pclose);
if (!pipe) throw std::runtime_error("popen() failed");
// Force line-buffered on the read side; pacman flushes its progress lines
// with newlines so each is forwarded as soon as it is emitted.
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
std::string line(buffer.data());
result += line;
// Trim the trailing newline for the callback.
while (!line.empty() && (line.back() == '\n' || line.back() == '\r')) {
line.pop_back();
}
if (progressCallback && !line.empty()) {
progressCallback(line, percentFromLine(line));
}
}
return result;
}
// Read a (current/Total) package counter out of a pacman line, e.g.
// "(3/5) téléchargement de ...". Returns a 0-100 percentage, or -1 when the
// line carries no counter (so the caller keeps the previous value / unknown).
static int percentFromLine(const std::string& line) {
size_t open = line.find('(');
size_t close = line.find(')');
if (open == std::string::npos || close == std::string::npos || open > close) return -1;
std::string inner = line.substr(open + 1, close - open - 1);
size_t slash = inner.find('/');
if (slash == std::string::npos) return -1;
try {
long cur = std::stol(inner.substr(0, slash));
long total = std::stol(inner.substr(slash + 1));
if (total <= 0) return -1;
int pct = static_cast<int>(cur * 100 / total);
return std::max(0, std::min(100, pct));
} catch (...) {
return -1;
}
const std::string full = "printf '%s\\n' " + shellQuote(password)
+ " | sudo -S " + pacmanCmd + " 2>&1";
return runWithProgress(full, progressCallback);
}
// pacman's database lock file. Every manager that goes through libalpm (pacman,
@@ -140,17 +103,6 @@ static constexpr int kKillGraceMs = 3000;
// cache outright.
static constexpr std::chrono::seconds kQueryTtl{10};
// Stream the lines of a command's output (already captured) to the callback —
// used by providers that buffer a whole command first. Percent stays unknown.
static void streamOutput(const std::string& output, ProgressCallback progressCallback) {
if (!progressCallback) return;
std::istringstream iss(output);
std::string line;
while (std::getline(iss, line)) {
if (!line.empty()) progressCallback(line, -1);
}
}
std::string PacmanManager::cachedQuery(PacmanManager::CachedQuery& slot,
const char* command) {
const auto now = std::chrono::steady_clock::now();
@@ -177,6 +129,7 @@ void PacmanManager::invalidateQueries() {
m_installedQuery.valid = false;
m_installedDetailsQuery.valid = false;
m_syncReposQuery.valid = false;
m_updatesQuery.valid = false;
}
std::vector<PackageInfo> PacmanManager::search(const std::string& query) {
@@ -366,7 +319,10 @@ static std::vector<PackageInfo> parseUpdatesOutput(const std::string& output) {
}
std::vector<PackageInfo> PacmanManager::listUpdates() {
return parseUpdatesOutput(exec("pacman -Qu 2>&1"));
// Cached like the other read-only queries: the dashboard badge, the updates
// screen and the "Tout mettre à jour" button all ask for it within the same
// second, and one pacman process answers for all of them.
return parseUpdatesOutput(cachedQuery(m_updatesQuery, "pacman -Qu 2>&1"));
}
// Orphans (-Qdt: no installed package requires them) and legacy packages (-Qm: