51 lines
1.6 KiB
C++
51 lines
1.6 KiB
C++
#include "progressbar.h"
|
|
|
|
#include <iomanip>
|
|
|
|
#include "file.h"
|
|
|
|
const uint_fast8_t ProgressBar::BARLENGTH = 50;
|
|
|
|
ProgressBar::ProgressBar(uint64_t files, uint64_t bytes) : maxFiles(files), maxBytes(bytes) {
|
|
}
|
|
|
|
void ProgressBar::update(uint64_t addFiles, uint64_t newBytes) {
|
|
currentFiles += addFiles;
|
|
currentBytes += newBytes;
|
|
}
|
|
|
|
std::chrono::duration<uint64_t, std::milli> ProgressBar::getDuration() const {
|
|
auto currenttime = std::chrono::high_resolution_clock::now();
|
|
return std::chrono::duration_cast<std::chrono::milliseconds>(currenttime - start);
|
|
}
|
|
|
|
std::ostream& operator<<(std::ostream& str, const ProgressBar& pb) {
|
|
double progress = (pb.currentBytes / (double) pb.maxBytes);
|
|
|
|
auto usedtime = pb.getDuration();
|
|
std::chrono::duration<uint64_t, std::ratio<60>> eta = std::chrono::duration_cast<std::chrono::minutes>((usedtime / (double) pb.currentBytes) * pb.maxBytes);
|
|
|
|
// speed
|
|
double speed = (pb.currentBytes / (double) usedtime.count()); // B/ms
|
|
speed /= 1000; // B/s
|
|
uint64_t bytespersecond = (int) speed; // floor
|
|
|
|
// generate bar
|
|
str << "\r[";
|
|
for(uint_fast8_t i = 0; i < ProgressBar::BARLENGTH; ++i) {
|
|
if( (i / (double) ProgressBar::BARLENGTH) < progress ) {
|
|
str << '#';
|
|
} else {
|
|
str << ' ';
|
|
}
|
|
}
|
|
|
|
// print info
|
|
str << "] " << std::setprecision(2) << std::setfill('0') << std::fixed << std::setw(5) << (progress * 100) << "% "
|
|
<< std::setprecision(6) << std::defaultfloat
|
|
<< FileSize(pb.currentBytes) << '/' << FileSize(pb.maxBytes) << " " << pb.currentFiles << '/' << pb.maxFiles << " Files "
|
|
<< FileSize(bytespersecond) << "/s ETA: " << eta.count() << "s " << std::flush;
|
|
|
|
return str;
|
|
}
|