progressbar.h
1.81 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
#ifndef RTS_PROGRESSBAR_H
#define RTS_PROGRESSBAR_H
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;
void stimPrintTime(unsigned long long s) {
std::cout << std::fixed << std::showpoint << std::setprecision(1);
if (s >= 60) { //if there are more than 60 seconds
unsigned long long m = s / 60; //get the number of minutes
s = s - m * 60; //get the number of remaining seconds
if (m > 60) { //if more than 60 minutes
unsigned long long h = m / 60;
m = m - h * 60; //get the number of remaining minutes
if (h > 24) { //if there are more than 24 hours
unsigned long long d = h / 24;
h = h - d * 24; //get the number of remaining hours
std::cout << " " << d << "d";
}
std::cout << " " << h << "h";
}
std::cout << " " << m << "m";
}
std::cout << " " << s << "s";
}
static void rtsProgressBar(unsigned int percent, double elapsed_s = 0)
{
//std::cout<<percent<<std::endl;
stringstream bar;
static int x = 0;
string slash[4];
slash[0] = "\\";
slash[1] = "-";
slash[2] = "/";
slash[3] = "|";
bar<<"[";
for(int i=0; i<40; i++)
{
if(percent > (float)i/(float)40 *100)
bar << "*";
else
bar << " ";
}
bar<<"]";
cout << "\r"; // carriage return back to beginning of line
cout << bar.str() << " " << slash[x] << " " << percent << " %"; // print the bars and percentage
if (elapsed_s > 0) {
stimPrintTime((unsigned long long)elapsed_s);
if (percent > 0) {
std::cout << " / ";
double s_per_percent = elapsed_s / percent;
double total = s_per_percent * 100;
stimPrintTime((unsigned long long)total);
}
}
cout.flush();
x++; // increment to make the slash appear to rotate
if(x == 4)
x = 0; // reset slash animation
}
#endif