Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
common : add minimalist multi-thread progress bar
I intentionally kept the bar simple without specifying part numbers
(which ultimately don't matter much) the only thing we care about is
tracking progress.

Signed-off-by: Adrien Gallouët <[email protected]>
  • Loading branch information
angt committed Nov 29, 2025
commit 3f490350bd270513c2ccee818af82fb4d96a3f4b
27 changes: 23 additions & 4 deletions common/download.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <filesystem>
#include <fstream>
#include <future>
#include <map>
#include <mutex>
#include <regex>
#include <string>
#include <thread>
Expand Down Expand Up @@ -486,17 +488,36 @@ static void print_progress(size_t current, size_t total) {
return;
}

static std::mutex mutex;
static std::map<std::thread::id, int> lines;
Comment on lines +491 to +492
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a local variable at first glance, but because it's static, that effectively make it a global variable.

IMO we should avoid global variable if possible. Probably a better way is to make a dedicated class to manage threads and implement the mutex as a member of the class.


std::lock_guard<std::mutex> lock(mutex);
std::thread::id id = std::this_thread::get_id();

if (lines.find(id) == lines.end()) {
lines[id] = lines.size();
std::cout << "\n";
}
int lines_up = lines.size() - lines[id];

size_t width = 50;
size_t pct = (100 * current) / total;
size_t pos = (width * current) / total;

std::cout << "["
std::cout << "\033[s";

if (lines_up > 0) {
std::cout << "\033[" << lines_up << "A";
}
std::cout << "\033[2K\r["
<< std::string(pos, '=')
<< (pos < width ? ">" : "")
<< std::string(width - pos, ' ')
<< "] " << std::setw(3) << pct << "% ("
<< current / (1024 * 1024) << " MB / "
<< total / (1024 * 1024) << " MB)\r";
<< total / (1024 * 1024) << " MB) "
<< "\033[u";

std::cout.flush();
}

Expand Down Expand Up @@ -552,8 +573,6 @@ static bool common_pull_file(httplib::Client & cli,
nullptr
);

std::cout << "\n";

if (!res) {
LOG_ERR("%s: error during download. Status: %d\n", __func__, res ? res->status : -1);
return false;
Expand Down
Loading