-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathProgressBar.cpp
More file actions
77 lines (68 loc) · 2.39 KB
/
ProgressBar.cpp
File metadata and controls
77 lines (68 loc) · 2.39 KB
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
#include "crpropa/ProgressBar.h"
#include <cstdio>
#include <iostream>
namespace crpropa {
/// Initialize a ProgressBar with [steps] number of steps, updated at [updateSteps] intervalls
ProgressBar::ProgressBar(unsigned long steps, unsigned long updateSteps) :
_steps(steps), _currentCount(0), _maxbarLength(10), _updateSteps(
updateSteps), _nextStep(1), _startTime(0) {
if (_updateSteps > _steps)
_updateSteps = _steps;
arrow.append(">");
}
void ProgressBar::start(const std::string &title) {
_startTime = time(NULL);
std::string s = ctime(&_startTime);
s.erase(s.end() - 1, s.end());
stringTmpl = " Started ";
stringTmpl.append(s);
stringTmpl.append(" : [%-10s] %3i%% %s: %02i:%02i:%02i %s\r");
std::cout << title << std::endl;
}
/// update the progressbar
/// should be called steps times in a loop
void ProgressBar::update() {
_currentCount++;
if (_currentCount == _nextStep || _currentCount == _steps
|| _currentCount == 1000) {
_nextStep += long(_steps / float(_updateSteps));
setPosition(_currentCount);
}
}
void ProgressBar::setPosition(unsigned long position) {
int percentage = int(100 * (position / float(_steps)));
time_t currentTime = time(NULL);
if (position < _steps) {
if (arrow.size() <= (_maxbarLength) * (position) / (_steps))
arrow.insert(0, "=");
float tElapsed = currentTime - _startTime;
float tToGo = (_steps - position) * tElapsed / position;
std::printf(stringTmpl.c_str(), arrow.c_str(), percentage, "Finish in",
int(tToGo / 3600), (int(tToGo) % 3600) / 60,
int(tToGo) % 60, "");
fflush(stdout);
} else {
float tElapsed = currentTime - _startTime;
std::string s = " - Finished at ";
s.append(ctime(¤tTime));
char fs[255];
std::snprintf(fs, 255, "%c[%d;%dm Finished %c[%dm", 27, 1, 32, 27, 0);
std::printf(stringTmpl.c_str(), fs, 100, "Needed",
int(tElapsed / 3600), (int(tElapsed) % 3600) / 60,
int(tElapsed) % 60, s.c_str());
}
}
/// Mark the progressbar with an error
void ProgressBar::setError() {
time_t currentTime = time(NULL);
_currentCount++;
float tElapsed = currentTime - _startTime;
std::string s = " - Finished at ";
s.append(ctime(¤tTime));
char fs[255];
std::snprintf(fs, 255, "%c[%d;%dm ERROR %c[%dm", 27, 1, 31, 27, 0);
std::printf(stringTmpl.c_str(), fs, _currentCount, "Needed",
int(tElapsed / 3600), (int(tElapsed) % 3600) / 60,
int(tElapsed) % 60, s.c_str());
}
} // namespace crpropa