-
Notifications
You must be signed in to change notification settings - Fork 0
/
Counter.cpp
136 lines (120 loc) · 2.95 KB
/
Counter.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
#include "Counter_i.h"
Counter::Counter(std::string pInitString, double pMaximum, int pColor)
:
mInitString(pInitString),
mMaximumInTicks(pMaximum),
mTicksPartialBlock(0),
mTicks(0),
//
mTicksPerBlock(mMaximumInTicks / 50),
mTicksPerBlockPart(mMaximumInTicks / 400),
mColor(pColor),
mBold(0)
{
// starting time
time(&mCBegin);
// convert color
if (mColor != 42){
if (mColor > 8){
mColor = 30 + mColor - 8;
mBold = 1;
} else {
mColor = 30 + mColor;
mBold = 1;
}
}
}
Counter::~Counter()
{
}
void Counter::printini()
{
cout << mInitString;
// Space between the brackets is exactly 50 characters long.
cout << "[0 50 100]" << endl;
for (unsigned int i=0; i<mInitString.length(); i++) cout << " ";
cout << " \033[A\033[s\033[0,0H" << flush;
setcolor();
}
void Counter::setcolor()
{
if (mColor != 42) cout << "\033[" << mColor << "m";
cout << flush;
}
void Counter::quit()
{
// reset color
cout << "\033[u";
if (mColor != 42)
cout << "\033[m";
cout << endl;
}
void Counter::update()
{
mTicksPartialBlock += 1;
mTicks++;
time(&mCNow);
double seconds = difftime(mCNow, mCBegin);
int seconds2go = (int)(seconds * (mMaximumInTicks - mTicks) / mTicks) + 1;
int minutes2go = seconds2go / 60;
seconds2go %= 60;
int hours2go = minutes2go / 60;
minutes2go %= 60;
// Move the cursor
cout << "\033[u\033[D\033[s\033[0,0H" << flush;
// Output full bars
while (mTicksPartialBlock >= mTicksPerBlock) {
mTicksPartialBlock -= mTicksPerBlock;
std::cout << "\033[u";
setcolor();
cout << "█\033[s\033[0,0H" << std::flush;
}
double qq = mTicksPartialBlock;
// ticks not yet in a full block (from 0 to 7)
int ticksLeft = 0;
while (qq > mTicksPerBlockPart){
ticksLeft++;
qq -= mTicksPerBlockPart;
}
std::cout << "\033[u";
// Different strings to potentially use
// "▁▂▃▄▅▆▇█" "▏▎▍▌▋▊▉█" "|/-\\|/-\\"
switch (ticksLeft){
case 0:
// empty
cout << "\033[C";
break;
case 1:
cout << "▏";
break;
case 2:
cout << "▎";
break;
case 3:
cout << "▍";
break;
case 4:
cout << "▌";
break;
case 5:
cout << "▋";
break;
case 6:
cout << "▊";
break;
case 7:
cout << "▉";
break;
case 8: // should not happen
default:
cout << "█";
break;
}
// Show time to go below counter
cout << "\033[s" << flush;
cout << "\n\033[" << mInitString.length() + 1;
cout << "C\033[m";
cout << hours2go << "h" << minutes2go << "m" <<seconds2go;
cout << "s\033[K";
cout << "\033[0;0H" << flush;
}