-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpbar.h
More file actions
60 lines (54 loc) · 1.37 KB
/
pbar.h
File metadata and controls
60 lines (54 loc) · 1.37 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
#pragma once
#include <iostream>
#include <atomic>
static inline void hide_cursor()
{
std::cout << "\033[?25l";
}
static inline void show_cursor()
{
std::cout << "\033[?25h";
}
struct progress_bar {
int progress{ 0 };
float bar_width{ 50 };
float max{ 100.0 };
bool show_percentage{ false };
std::string prefix_text{ "" };
std::string start{ "[" };
std::string fill{ "■" };
std::string remainder{ " " };
std::string end{ "]" };
std::string postfix_text{ "" };
void set_progress(float value) {
progress = value;
print_progress();
}
void tick() {
progress += 1;
print_progress();
}
private:
void print_progress() {
std::cout << prefix_text;
std::cout << start;
float pos = progress * bar_width / max;
for (size_t i = 0; i < bar_width; ++i) {
if (i <= pos)
std::cout << fill;
else
std::cout << remainder;
}
std::cout << end;
if (show_percentage)
std::cout << " " << int(progress / max * 100.0) << "%";
else
std::cout << " " << progress << "/" << max;
std::cout << " " << postfix_text << "\r";
std::cout.flush();
if (progress >= max) {
progress = 0;
std::cout << "\33[2K\r"; // erase line
}
}
};