-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogressbar.h
71 lines (64 loc) · 1.14 KB
/
progressbar.h
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <unistd.h>
class ProgressBar {
private:
int _emptyVal, _fullVal, _increments;
int _currentVal;
bool _full=false;
public:
ProgressBar(int emptyVal, int fullVal, int incr);
void reset();
void update();
void test();
};
ProgressBar::ProgressBar(int emptyVal=0, int fullVal=100, int incr=1)
{
if(fullVal<=emptyVal || incr<1 || incr>fullVal-emptyVal)
{
std::cerr<<"Incorrect values to progressbar.\n";
}
else
{
_emptyVal = emptyVal;
_fullVal = fullVal;
_increments = incr;
_currentVal = _emptyVal;
}
}
void ProgressBar::reset()
{
_currentVal = _emptyVal;
_full = false;
}
void ProgressBar::update()
{
if(!_full)
{
_currentVal += _increments;
if(_currentVal>_fullVal)
_currentVal = _fullVal;
std::cout<<"[";
for(int i=_emptyVal; i<_currentVal; i+=_increments)
{
std::cout<<'*';
}
if(_currentVal<_fullVal)
std::cout<<"]> "<<_currentVal<<"/"<<_fullVal<<"\r";
else
{
std::cout<<"]> Done.\r\n";
_full = true;
}
std::cout.flush();
}
}
void ProgressBar::test()
{
update();
sleep(1);
for(int i=_emptyVal; i<_fullVal; i+=_increments)
{
update();
usleep(6000);
}
}