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>
This commit is contained in:
8 files changed
+246
-112
No files matched your search
+73
-25
@@ -31,23 +31,71 @@ static std::string shellQuote(const std::string& s) {
|
||||
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 returning the combined stdout+stderr.
|
||||
// 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.
|
||||
static std::string execPrivileged(const std::string& pacmanCmd,
|
||||
const std::string& password) {
|
||||
const std::string& password,
|
||||
ProgressCallback progressCallback) {
|
||||
std::string full = "printf '%s\\n' " + shellQuote(password)
|
||||
+ " | sudo -S " + pacmanCmd + " 2>&1";
|
||||
return exec(full.c_str());
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// Stream the lines of a command's output to the progress callback, if any.
|
||||
static void streamOutput(const std::string& output,
|
||||
std::function<void(const std::string&)> progressCallback) {
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
// 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);
|
||||
if (!line.empty()) progressCallback(line, -1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,11 +146,11 @@ std::vector<PackageInfo> PacmanManager::search(const std::string& query) {
|
||||
}
|
||||
|
||||
bool PacmanManager::install(const std::string& packageName,
|
||||
std::function<void(const std::string&)> progressCallback) {
|
||||
if (progressCallback) progressCallback("Installation de " + packageName + "...");
|
||||
ProgressCallback progressCallback) {
|
||||
if (progressCallback) progressCallback("Installation de " + packageName + "...", -1);
|
||||
|
||||
std::string output = execPrivileged("pacman -S --noconfirm " + packageName, m_password);
|
||||
streamOutput(output, progressCallback);
|
||||
std::string output =
|
||||
execPrivileged("pacman -S --noconfirm " + packageName, m_password, progressCallback);
|
||||
|
||||
// A wrong/expired password must not be silently reused: drop it so the
|
||||
// next attempt prompts again.
|
||||
@@ -115,11 +163,11 @@ bool PacmanManager::install(const std::string& packageName,
|
||||
}
|
||||
|
||||
bool PacmanManager::remove(const std::string& packageName,
|
||||
std::function<void(const std::string&)> progressCallback) {
|
||||
if (progressCallback) progressCallback("Suppression de " + packageName + "...");
|
||||
ProgressCallback progressCallback) {
|
||||
if (progressCallback) progressCallback("Suppression de " + packageName + "...", -1);
|
||||
|
||||
std::string output = execPrivileged("pacman -R --noconfirm " + packageName, m_password);
|
||||
streamOutput(output, progressCallback);
|
||||
std::string output =
|
||||
execPrivileged("pacman -R --noconfirm " + packageName, m_password, progressCallback);
|
||||
|
||||
if (authFailed(output)) {
|
||||
clearPassword();
|
||||
@@ -130,16 +178,16 @@ bool PacmanManager::remove(const std::string& packageName,
|
||||
}
|
||||
|
||||
bool PacmanManager::update(const std::string& packageName,
|
||||
std::function<void(const std::string&)> progressCallback) {
|
||||
ProgressCallback progressCallback) {
|
||||
std::string output;
|
||||
if (packageName.empty()) {
|
||||
if (progressCallback) progressCallback("Mise à jour de tous les paquets...");
|
||||
output = execPrivileged("pacman -Syu --noconfirm", m_password);
|
||||
if (progressCallback) progressCallback("Mise à jour de tous les paquets...", -1);
|
||||
output = execPrivileged("pacman -Syu --noconfirm", m_password, progressCallback);
|
||||
} else {
|
||||
if (progressCallback) progressCallback("Mise à jour de " + packageName + "...");
|
||||
output = execPrivileged("pacman -S --noconfirm " + packageName, m_password);
|
||||
if (progressCallback) progressCallback("Mise à jour de " + packageName + "...", -1);
|
||||
output = execPrivileged("pacman -S --noconfirm " + packageName,
|
||||
m_password, progressCallback);
|
||||
}
|
||||
streamOutput(output, progressCallback);
|
||||
|
||||
if (authFailed(output)) {
|
||||
clearPassword();
|
||||
@@ -205,11 +253,11 @@ std::vector<PackageInfo> PacmanManager::listUpdates() {
|
||||
return parseUpdatesOutput(exec("pacman -Qu 2>&1"));
|
||||
}
|
||||
|
||||
bool PacmanManager::refresh(std::function<void(const std::string&)> progressCallback) {
|
||||
if (progressCallback) progressCallback("Rafraîchissement des bases de données...");
|
||||
bool PacmanManager::refresh(ProgressCallback progressCallback) {
|
||||
if (progressCallback) progressCallback("Rafraîchissement des bases de données...", -1);
|
||||
|
||||
std::string output = execPrivileged("pacman -Sy --noconfirm", m_password);
|
||||
streamOutput(output, progressCallback);
|
||||
std::string output =
|
||||
execPrivileged("pacman -Sy --noconfirm", m_password, progressCallback);
|
||||
|
||||
if (authFailed(output)) {
|
||||
clearPassword();
|
||||
|
||||
Reference in new issue
Block a user