Skip to content
Merged
Show file tree
Hide file tree
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
33 changes: 26 additions & 7 deletions lib/visuals/progressbar.hh
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
#include "throughput.hh"

struct ProgressBar {
bool finished = false;
int width = 100;
int total = 0;
float progress = 0;
bool finished = false;
int width = 100;
int total = 0;
float progress = 0;
int last_length = 0;
std::string label;
std::optional<Throughput> tp;

Expand Down Expand Up @@ -40,10 +41,28 @@ struct ProgressBar {
for (int i = 0; i < width; ++i) {
std::print("{}", i < filled ? "■" : " ");
}
std::cout << "] " << (int)(this->progress * 100) << "%\033[33m " << tp_str << "\033[0m "
<< std::flush;

std::string output =
std::format("] {}%\033[33m {}\033[0m", (int)(this->progress * 100), tp_str);

std::cout << output;

if (tp) {
auto current_length = output.length();
if (this->last_length > current_length) {
/* There are some trailing characters left over from the last time
* throughput was printed. Overwrite them with spaces, and then
* backspace back to the current string length. */
auto n = this->last_length - current_length;
std::cout << std::string(n, ' ') << std::string(n, '\b');
}
this->last_length = current_length;
}

std::cout << std::flush;

if (this->finished) {
std::println("");
std::cout << '\n';
}
}
};
2 changes: 1 addition & 1 deletion lib/visuals/throughput.hh
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ struct Throughput {

for (size_t i = 0; i < units.size(); ++i) {
int part = (i < static_cast<int>(units.size()) - 1) ? num % 60 : num;
if (part != 0) {
if (i == 0 || part != 0) {
result = std::format("{}{}", part, units[i]) + result;
}
num /= 60;
Expand Down