mirror of
https://github.com/davidallendj/gdpm.git
synced 2025-12-20 03:27:02 -07:00
Initial commit
First commit with most of the main features implemented. May still need some bug fixes here and there.
This commit is contained in:
commit
1893c7c36b
26 changed files with 2839 additions and 0 deletions
59
include/progress_bar.hpp
Normal file
59
include/progress_bar.hpp
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
#include <cmath>
|
||||
#include <iomanip>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
|
||||
class progress_bar
|
||||
{
|
||||
static const auto overhead = sizeof " [100%]";
|
||||
|
||||
std::ostream& os;
|
||||
const std::size_t bar_width;
|
||||
std::string message;
|
||||
const std::string full_bar;
|
||||
|
||||
public:
|
||||
progress_bar(std::ostream& os, std::size_t line_width,
|
||||
std::string message_, const char symbol = '.')
|
||||
: os{os},
|
||||
bar_width{line_width - overhead},
|
||||
message{std::move(message_)},
|
||||
full_bar{std::string(bar_width, symbol) + std::string(bar_width, ' ')}
|
||||
{
|
||||
if (message.size()+1 >= bar_width || message.find('\n') != message.npos) {
|
||||
os << message << '\n';
|
||||
message.clear();
|
||||
} else {
|
||||
message += ' ';
|
||||
}
|
||||
write(0.0);
|
||||
}
|
||||
|
||||
// not copyable
|
||||
progress_bar(const progress_bar&) = delete;
|
||||
progress_bar& operator=(const progress_bar&) = delete;
|
||||
|
||||
~progress_bar()
|
||||
{
|
||||
write(1.0);
|
||||
os << '\n';
|
||||
}
|
||||
|
||||
void write(double fraction);
|
||||
};
|
||||
|
||||
void progress_bar::write(double fraction)
|
||||
{
|
||||
// clamp fraction to valid range [0,1]
|
||||
if (fraction < 0)
|
||||
fraction = 0;
|
||||
else if (fraction > 1)
|
||||
fraction = 1;
|
||||
|
||||
auto width = bar_width - message.size();
|
||||
auto offset = bar_width - static_cast<unsigned>(width * fraction);
|
||||
|
||||
os << '\r' << message;
|
||||
os.write(full_bar.data() + offset, width);
|
||||
os << " [" << std::setw(3) << static_cast<int>(100*fraction) << "%] " << std::flush;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue