-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlapse.h
50 lines (39 loc) · 1.28 KB
/
lapse.h
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
#ifndef LAPSE_H
#define LAPSE_H
#include <iostream>
#include <string>
#include <chrono>
#include <iomanip>
namespace lapse {
class Lapse {
private:
int total;
int current;
int width;
std::chrono::steady_clock::time_point start_time;
public:
Lapse(int total, int width = 50) : total(total), current(0), width(width) {
start_time = std::chrono::steady_clock::now();
}
void update(int n = 1) {
current += n;
if (current > total) current = total;
float progress = static_cast<float>(current) / total;
int pos = static_cast<int>(width * progress);
std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::seconds>(now - start_time).count();
std::cout << "[";
for (int i = 0; i < width; ++i) {
if (i < pos) std::cout << "=";
else if (i == pos) std::cout << ">";
else std::cout << " ";
}
std::cout << "] " << int(progress * 100.0) << "% "
<< current << "/" << total
<< " (" << elapsed << "s)\r";
std::cout.flush();
if (current == total) std::cout << std::endl;
}
};
}
#endif