-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogress.cpp
86 lines (62 loc) · 1.16 KB
/
progress.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "progress.hpp"
#include <iostream>
#include <cmath>
Progress::Progress():
prefix_(),
postfix_(),
total_(1.0f),
current_(0.0f),
interval_(1000),
lastFlush_()
{
}
void Progress::setPrefix(const std::string& v)
{
prefix_ = v;
}
void Progress::setPostfix(const std::string& v)
{
postfix_ = v;
}
void Progress::setTotal(float v)
{
total_ = v;
}
void Progress::setCurrent(float v)
{
current_ = v;
}
void Progress::update()
{
auto now = clock::now();
if(now - lastFlush_ < interval_)
{
return;
}
flush(now);
}
void Progress::flush()
{
flush(clock::now());
}
void Progress::flush(clock::time_point now)
{
lastFlush_ = now;
float progress;
if(std::fabs(total_) > 0.0001f)
{
progress = current_ / total_ * 100.0f;
}
else
{
progress = 100.0f;
}
std::ostream& out = std::cout;
auto oldFlags = out.setf(std::ios_base::fixed, std::ios_base::floatfield);
auto oldPrecision = out.precision(2);
out
<< "\x1b[2K\r" // erase whole line
<< prefix_ << progress << postfix_ << std::flush;
out.precision(oldPrecision);
out.flags(oldFlags);
}