-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProgressBar.cpp
204 lines (143 loc) · 3.82 KB
/
ProgressBar.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// ProgressBar
// Karl Kosack <kosack@hbar.wustl.edu>
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdio>
#include <sys/time.h>
#include <signal.h>
#include "ProgressBar.h"
using std::cout;
using std::cerr;
using std::endl;
ProgressBar::ProgressBar( int total, std::string name )
: _total(total),_last(0),_spin(0),_name(name),_avgpersec(0),
_is_ansi_term(true)
{
const char ESC = 0x1b;
_spinner[0] = '|';
_spinner[1] = '/';
_spinner[2] = '-';
_spinner[3] = '\\';
std::string term = getenv("TERM");
if ( term.find("xterm") == std::string::npos
&& term.find("vt100") == std::string::npos
&& term.find("vt220") == std::string::npos ) {
_is_ansi_term = false;
}
if (_is_ansi_term) {
cerr << ESC << "[?25l"; // disable cursor
}
}
ProgressBar::~ProgressBar() {
const char ESC = 0x1b;
if (_is_ansi_term) {
cerr << ESC << "[?25h"; // enable cursor
cerr << "Cursor reenabled." << endl;
}
}
/**
* Displays the progress bar, at position n
*/
void
ProgressBar::print( int n ) {
int i;
float percent;
int secleft, minleft;
double evtpersec;
int prog;
double delta = n - _last;
const char ESC = 0x1b;
if (n<=0) return;
_spin++; if (_spin>=4) _spin=0;
percent = (float)n/_total;
prog = (int)(percent * 30);
cerr << _name<<": "<< _spinner[_spin] << " [" ;
if (_total == 0) {
cerr << "please wait...] ";
cerr <<" \r" << std::flush;
return;
}
if (_is_ansi_term) {
for (i=0; i<=prog; i++) cerr<<ESC<<"[01;44m "<<ESC<<"[0m";//"#";
for (i=prog+1; i<30; i++) cerr<<"-"; //cerr << "_";
}
else {
for (i=0; i<=prog; i++) cerr<<"#";
for (i=prog+1; i<30; i++) cerr<<"_";
}
_now = getTime();
if (delta > 0) {
evtpersec = (_now-_before > 0)? (delta/((_now-_before))):-1;
_before = _now;
_last = n;
}
_avgpersec = (evtpersec + _avgpersec)/2.0;
secleft = (_avgpersec>0)?(int)((_total-n)/_avgpersec):0;
minleft = secleft/60;
secleft = secleft % 60;
cerr << ESC << "[0m"
<< "] "
<< std::setw(2) << (int)(percent*100) << "% ";
if ( evtpersec > 1e8 || evtpersec < 0)
cerr << "??? per sec, ";
else
cerr << std::setw(6) <<std::setprecision(4)<<evtpersec<<" per sec, ";
if ( minleft < 60 && secleft > 0) {
cerr << std::setw(2) << minleft <<":"
<< std::setw(2) << std::setfill('0')<< secleft << " left"
<< std::setfill(' ');
}
else {
cerr << " ";
}
cerr <<" \r" << std::flush;
}
/**
* Returns the current system time in microseconds
*/
double
ProgressBar::getTime() {
struct timeval tv;
struct timezone tz;
gettimeofday( &tv,&tz);
return (tv.tv_sec + (double)tv.tv_usec/1.0e6);
}
/**
* Print a string of spaces over the progress bar area.
*/
void
ProgressBar::printClear() {
const char ESC = 0x1b;
cerr << " "
" \r"
<<std::flush;
if (_is_ansi_term) {
cerr << ESC << "[?25h"; // enable cursor
}
}
ProgressBarHandler* ProgressBarHandler::pinstance = NULL;
void progress_signal_handler( int n) {
if (ProgressBarHandler::instance()->isRunning() )
ProgressBarHandler::instance()->print();
}
ProgressBarHandler*
ProgressBarHandler::instance() {
if (pinstance==NULL)
pinstance = new ProgressBarHandler();
return pinstance;
}
ProgressBarHandler::ProgressBarHandler() {
_timer.it_interval.tv_sec = 0;
_timer.it_interval.tv_usec = 500000;
_timer.it_value.tv_sec = 0;
_timer.it_value.tv_usec = 40000;
struct sigaction sa;
sigfillset( &sa.sa_mask );
sa.sa_flags = 0;
sa.sa_handler = progress_signal_handler;
if (sigaction(SIGALRM, &sa, NULL) < 0) {
perror( "sigaction SIGALRM" );
exit(1);
}
}