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:
1 parent
ed58e87520
commit
b26e907221
12 files changed
+590
-138
No files matched your search
+53
-14
@@ -222,8 +222,12 @@ static QString appStyleSheet() {
|
||||
" color: #e2e8f0;"
|
||||
"}"
|
||||
// ---- Progress bar ----
|
||||
"QProgressBar { background: #273449; border: none; border-radius: 3px; }"
|
||||
"QProgressBar::chunk { background: #38bdf8; border-radius: 3px; }"
|
||||
// The value is spelled out beside the bar rather than inside it: the
|
||||
// label Qt centres on the bar would sit half on the fill and half on
|
||||
// the track, where no single colour stays readable.
|
||||
"QLabel#progressLabel { color: #7dd3fc; font-size: 11px; font-weight: 700; padding-right: 8px; }"
|
||||
"QProgressBar { background: #273449; border: none; border-radius: 4px; }"
|
||||
"QProgressBar::chunk { background: #38bdf8; border-radius: 4px; }"
|
||||
// ---- Package table ----
|
||||
// No outer frame: the table blends with the card it sits in, keeping
|
||||
// only the column header and alternating rows as visual separators.
|
||||
@@ -408,14 +412,20 @@ void MainWindow::setupUI() {
|
||||
|
||||
statusBar()->showMessage("Prêt");
|
||||
|
||||
// Tiny indeterminate bar anchored at the bottom of the window: it appears
|
||||
// while install/update/remove/search operations run, without taking space
|
||||
// on the screens.
|
||||
// Progress of the running operation, anchored at the bottom of the window
|
||||
// so it takes no space on the screens. It appears while install/update/
|
||||
// remove/search operations run: indeterminate at first, then filled to the
|
||||
// real percentage reported by the provider (see onCoreActionProgress).
|
||||
m_progressLabel = new QLabel();
|
||||
m_progressLabel->setObjectName("progressLabel");
|
||||
m_progressLabel->setVisible(false);
|
||||
statusBar()->addPermanentWidget(m_progressLabel);
|
||||
|
||||
m_installProgressBar = new QProgressBar();
|
||||
m_installProgressBar->setRange(0, 0);
|
||||
m_installProgressBar->setTextVisible(false);
|
||||
m_installProgressBar->setFixedHeight(4);
|
||||
m_installProgressBar->setFixedWidth(120);
|
||||
m_installProgressBar->setFixedHeight(8);
|
||||
m_installProgressBar->setFixedWidth(160);
|
||||
m_installProgressBar->setVisible(false);
|
||||
statusBar()->addPermanentWidget(m_installProgressBar);
|
||||
|
||||
@@ -1011,11 +1021,17 @@ void MainWindow::onCoreActionProgress(const QString& message, int percent) {
|
||||
if (!message.isEmpty()) statusBar()->showMessage(message);
|
||||
if (percent < 0) return;
|
||||
|
||||
// A real download percentage reported by the provider fills the bottom bar
|
||||
// as a determinate progress.
|
||||
// A provider running several commands (or a second provider after the
|
||||
// first) starts its own count at 0: within one operation the bar only ever
|
||||
// moves forward, so a retry or a second command never sends it backwards.
|
||||
if (percent > m_progressValue) m_progressValue = percent;
|
||||
|
||||
m_installProgressBar->setVisible(true);
|
||||
m_installProgressBar->setRange(0, 100);
|
||||
m_installProgressBar->setValue(percent);
|
||||
m_installProgressBar->setValue(m_progressValue);
|
||||
|
||||
m_progressLabel->setText(QString("%1 %").arg(m_progressValue));
|
||||
m_progressLabel->setVisible(true);
|
||||
}
|
||||
|
||||
void MainWindow::onCoreActionFinished(const QString& action, const QString& name,
|
||||
@@ -1036,13 +1052,36 @@ void MainWindow::onCoreActionFinished(const QString& action, const QString& name
|
||||
}
|
||||
|
||||
void MainWindow::onCoreBusyChanged(bool busy) {
|
||||
m_installProgressBar->setVisible(busy);
|
||||
if (!busy) return;
|
||||
// Any pending "hide the finished bar" timer belongs to the previous
|
||||
// operation and must not touch the new one.
|
||||
++m_progressGeneration;
|
||||
|
||||
// Start indeterminate; a real percentage (onCoreActionProgress) switches the
|
||||
// bar to a determinate fill once the tool reports progress.
|
||||
if (!busy) {
|
||||
if (m_installProgressBar->isVisible() && m_installProgressBar->maximum() > 0) {
|
||||
// Real progress was shown: complete it and let it be seen. A bar
|
||||
// that disappears at 97 % looks like a failure.
|
||||
m_installProgressBar->setValue(100);
|
||||
m_progressLabel->setText(QStringLiteral("100 %"));
|
||||
const int generation = m_progressGeneration;
|
||||
QTimer::singleShot(500, this, [this, generation] {
|
||||
if (generation != m_progressGeneration) return;
|
||||
m_installProgressBar->setVisible(false);
|
||||
m_progressLabel->setVisible(false);
|
||||
});
|
||||
return;
|
||||
}
|
||||
m_installProgressBar->setVisible(false);
|
||||
m_progressLabel->setVisible(false);
|
||||
return;
|
||||
}
|
||||
|
||||
// Start indeterminate; the first real percentage (onCoreActionProgress)
|
||||
// switches the bar to a determinate fill.
|
||||
m_progressValue = 0;
|
||||
m_installProgressBar->setRange(0, 0);
|
||||
m_installProgressBar->setValue(0);
|
||||
m_installProgressBar->setVisible(true);
|
||||
m_progressLabel->setVisible(false);
|
||||
}
|
||||
|
||||
void MainWindow::onCoreStatusMessage(const QString& message) {
|
||||
|
||||
+19
-63
@@ -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:
|
||||
|
||||
+12
-4
@@ -98,19 +98,27 @@ void InstallWorker::run() {
|
||||
return;
|
||||
}
|
||||
|
||||
const int providerCount = static_cast<int>(m_providers.size());
|
||||
bool success = true;
|
||||
for (IPackageManager* pm : m_providers) {
|
||||
for (int index = 0; index < providerCount; ++index) {
|
||||
IPackageManager* pm = m_providers[static_cast<size_t>(index)];
|
||||
// Prefix progress lines with the provider when several run at once.
|
||||
// A real percentage (>= 0) also drives the determinate status bar bar.
|
||||
auto cb = [this, pm](const std::string& msg, int percent) {
|
||||
if (m_providers.size() > 1) {
|
||||
auto cb = [this, pm, index, providerCount](const std::string& msg, int percent) {
|
||||
if (providerCount > 1) {
|
||||
emit progress(QString("[%1] %2")
|
||||
.arg(QString::fromStdString(pm->getName()),
|
||||
QString::fromStdString(msg)));
|
||||
} else {
|
||||
emit progress(QString::fromStdString(msg));
|
||||
}
|
||||
if (percent >= 0) emit progressValue(percent);
|
||||
if (percent < 0) return;
|
||||
|
||||
// Providers run one after the other, so each one owns an equal
|
||||
// slice of the bar. Without this the bar dropped back to 0 when the
|
||||
// second provider (flatpak, after pacman) started its own count.
|
||||
const int scaled = (index * 100 + percent) / providerCount;
|
||||
emit progressValue(std::min(scaled, 99));
|
||||
};
|
||||
|
||||
// Keep running the remaining providers even if one fails.
|
||||
|
||||
Reference in new issue
Block a user