-
Notifications
You must be signed in to change notification settings - Fork 2
/
zeitkatze.cpp
244 lines (201 loc) · 6.83 KB
/
zeitkatze.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <atomic>
#include <chrono>
#include <cmath>
#include <csignal>
#include <fcntl.h>
#include <iomanip>
#include <iostream>
#include <poll.h>
#include <sstream>
#include <termios.h>
#include <unistd.h>
#include <vector>
using std::chrono::duration;
using std::chrono::duration_cast;
using std::chrono::steady_clock;
using std::setfill;
using std::setw;
typedef std::vector<const char*> CatVector;
typedef std::vector<const char*>::size_type CatIndex;
enum class Color {
Normal,
Cat,
Cat_hold,
Total,
Running,
Running_lap,
Split,
Split_lap
};
bool color_enabled = true;
std::ostream& operator<<(std::ostream& oss, Color c) {
if (color_enabled) {
switch (c) {
case Color::Normal: oss << "\033[0m"; break;
case Color::Cat: oss << "\033[34m"; break;
case Color::Cat_hold: oss << "\033[34;1m"; break;
case Color::Total: oss << "\033[37;1m"; break;
case Color::Running: oss << "\033[32m"; break;
case Color::Running_lap: oss << "\033[33m"; break;
case Color::Split: oss << "\033[32;1m"; break;
case Color::Split_lap: oss << "\033[33;1m"; break;
default: break;
}
}
return oss;
}
class Zeitkatze {
public:
Zeitkatze() : split_printed(false), start(steady_clock::now()), last_lap(start), last_line_len(0) { }
void print_split_time() {
print_time(some_cat_index(), Color::Split);
}
void print_end_time() {
print_time(cats.size() - 1, Color::Total);
}
void print_time(const CatIndex cat_index, const Color color) {
steady_clock::time_point now(steady_clock::now());
std::stringstream sbuf;
sbuf << Color::Cat_hold << cats[cat_index] << Color::Cat_hold << " " << color << format_seconds(elapsed(), 4) << Color::Normal
<< " (" << Color::Split_lap << format_seconds(duration_cast<duration<double>>(now - last_lap).count(), 4) << Color::Normal
<< ")";
std::string&& line = sbuf.str();
std::cout << "\r" << std::string(last_line_len, ' ')
<< "\r" << line << std::flush;
last_lap = now;
split_printed = true;
had_lap = true;
last_line_len = line.size();
}
void print_current_time() {
if (split_printed) {
std::cout << std::endl;
split_printed = false;
}
std::stringstream sbuf;
sbuf << Color::Cat << cats[0] << " " << Color::Running << format_seconds(elapsed(), 2) << Color::Normal;
if (had_lap) {
auto current_lap = duration_cast<duration<double>>(steady_clock::now() - last_lap);
sbuf << " (" << Color::Running_lap << format_seconds(current_lap.count(), 2) << Color::Normal << ")";
}
std::string&& line = sbuf.str();
std::cout << "\r" << std::string(last_line_len, ' ')
<< "\r" << line << std::flush;
last_line_len = line.size();
}
double elapsed() {
duration<double> time_span = duration_cast<duration<double>>(steady_clock::now() - start);
return time_span.count();
}
std::string format_seconds(double seconds, unsigned precision = 2) {
double full_seconds = floor(seconds);
double fractional_seconds = seconds - full_seconds;
double minutes = floor(full_seconds / 60.0);
unsigned min = static_cast<unsigned>(minutes);
unsigned sec = static_cast<unsigned>(full_seconds) % 60;
unsigned frs = static_cast<unsigned>(fractional_seconds * pow(10, precision));
std::ostringstream oss;
if (min > 0)
oss << min << ":";
oss << setfill('0') << setw(2) << sec << "." << setw(precision) << frs;
return oss.str();
}
CatIndex some_cat_index() {
return static_cast<CatIndex>(elapsed() * 100) % (cats.size() - 2) + 1;
}
void reset_laps() {
last_lap = steady_clock::now();
had_lap = false;
}
static const CatVector cats;
private:
bool split_printed, had_lap;
steady_clock::time_point start, last_lap;
unsigned last_line_len;
};
const CatVector Zeitkatze::cats({ "=(^.^)=", "=(o.o)=", "=(^.^)\"", "=(x.x)=",
"=(o.o)m", " (o,o) ", "=(0.0)=", "=(@.@)=", "=(*.*)=", "=(-.-)=", "=(v.v)=", "=(o.O)=",
"=[˙.˙]=", "=(~.~)=", "=(ˇ.ˇ)=", "=(=.=)=" });
// oh so globally
std::atomic<bool> interrupted(false);
void interrupt(int) {
interrupted = true;
}
int main(int argc, char** argv) {
const double EXIT_TIMEOUT = 0.8;
Zeitkatze z;
bool running = true;
bool print_newline = false; // Print a new line before the end_time. Should be done after ^C^C but not after ^D
double last_interrupt = -EXIT_TIMEOUT;
char* color_env = getenv("ZEITKATZE_COLOR");
if (color_env != nullptr && std::string(color_env) == "0")
color_enabled = false;
if (argc > 1) {
if (std::string(argv[1]) == "-c" || std::string(argv[1]) == "--color") {
color_enabled = true;
} else if (std::string(argv[1]) == "-n" || std::string(argv[1]) == "--no-color") {
color_enabled = false;
} else {
std::cout << "Zeitkatze" << std::endl;
std::cout << std::endl;
std::cout << " time cat -- literally" << std::endl;
std::cout << std::endl;
std::cout << "Usage: zeitkatze [-c | -n | --color | --no-color]" << std::endl;
std::cout << std::endl;
std::cout << "Ctrl-c for split/lap time, Ctrl-cc or Ctrl-d to stop." << std::endl;
std::cout << std::endl;
std::cout << "-c, --color Enable colored output (default)." << std::endl;
std::cout << "-n, --no-color Disable colored output." << std::endl;
std::cout << " If both arguments are present, the first one counts." << std::endl;
std::cout << " (overrides ZEITKATZE_COLOR environment variable (set to" << std::endl;
std::cout << " \"0\" for no color))" << std::endl;
return 0;
}
}
signal(SIGINT, interrupt);
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
pollfd fds[] = { { STDIN_FILENO, POLLIN, 0 } };
unsigned char x = 0;
struct termios tio;
if (tcgetattr(1, &tio) == 0) {
tio.c_lflag &= ~(ECHO | ICANON);
tio.c_cc[VMIN] = 0;
tio.c_cc[VTIME] = 0;
tcsetattr(1, TCSANOW, &tio);
}
while(running) {
if (poll(fds, 1, 42) == 1) {
if ((fds[0].revents & POLLIN) && read(0, &x, 1) == 1) {
switch (x) {
case '\n':
case '\r':
z.print_split_time();
break;
case 'r':
z.reset_laps();
break;
case 4: // ^D
running = false;
}
}
}
if (z.elapsed() - last_interrupt > EXIT_TIMEOUT)
z.print_current_time();
if (interrupted) {
if (z.elapsed() - last_interrupt < EXIT_TIMEOUT)
{
running = false;
print_newline = true;
} else {
z.print_split_time();
}
last_interrupt = z.elapsed();
interrupted = false;
}
}
if (print_newline)
std::cout << std::endl;
z.print_end_time();
std::cout << std::endl;
return 0;
}