-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogressbar.hpp
More file actions
44 lines (34 loc) · 1016 Bytes
/
progressbar.hpp
File metadata and controls
44 lines (34 loc) · 1016 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#ifndef PROGRESSBAR_HPP
#define PROGRESSBAR_HPP
#include <iostream>
#include <iomanip>
#include <cmath>
class ProgressBar {
public:
ProgressBar(int total, int width = 40, char symbol = '#', char background = '-');
void update(int current);
private:
int total_;
int width_;
char symbol_;
char background_;
};
ProgressBar::ProgressBar(int total, int width, char symbol, char background)
: total_(total), width_(width), symbol_(symbol), background_(background) {}
void ProgressBar::update(int current) {
float progress = static_cast<float>(current) / total_;
int bar_width = static_cast<int>(progress * width_);
std::cout << "\r[";
for (int i = 0; i < bar_width; ++i) {
std::cout << symbol_;
}
for (int i = bar_width; i < width_; ++i) {
std::cout << background_;
}
std::cout << "] " << std::setw(3) << round(progress * 100.0) << "%";
std::cout.flush();
if (current == total_) {
std::cout << std::endl;
}
}
#endif