-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory.cpp
98 lines (86 loc) · 2.5 KB
/
factory.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
//factory.cpp
//Progress++ a simple and lightweight progress bar library written in c++
#include "factory.hpp"
namespace progress {
//Simple constructor
bar_factory::bar_factory(int size, bool safe_characters) {
//Set vars from arguments
this->size = size;
this->safe_characters = safe_characters;
}
//Return the bar as a string
std::string bar_factory::generate(double percentage) {
/*
Returns a bar corresponding to the percentage and size of the bar
Example:
bar size 100 10%:
|██████████ |
If safe characters is true then it outputs a boring bar but it is compatible with just about every console imaginable
Example:
bar size 100 10%:
|========== |
*/
//The string for the output bar, the number of blocks (inluding partial ones), and the number of full blocks
std::string bar;
double num_blocks = size * percentage;
double full_blocks = std::floor(size * percentage);
//TODO Do this more elegantly without magic numbers
if (!safe_characters) {
//THis double represents how filled the last block should be
double last_block = num_blocks - full_blocks;
std::string last_char;
if (last_block < 0.111) {
last_char = "";
}
else if (last_block < 0.222) {
last_char = "▏";
}
else if (last_block < 0.333) {
last_char = "▎";
}
else if (last_block < 0.444) {
last_char = "▍";
}
else if (last_block < 0.555) {
last_char = "▌";
}
else if (last_block < 0.666) {
last_char = "▋";
}
else if (last_block < 0.777) {
last_char = "▊";
}
else if (last_block < 0.888) {
last_char = "▉";
}
else {
last_char = "█";
}
//Constructs the bar from all the parts
bar = "|";
for (int i = 0; i < full_blocks; i++) {
bar.append("█");
}
bar.append(last_char);
//TODO Make this less hacky
// |
// \ /
for (int i = 0; i < (size - full_blocks - !(last_block < 0.111)); i++) {
bar.append(" ");
}
bar.append("|");
}
else {
//Constructs the bar from all the parts
bar = "|";
for (int i = 0; i < full_blocks; i++) {
bar.append("=");
}
for (int i = 0; i < (size - full_blocks); i++) {
bar.append(" ");
}
bar.append("|");
}
return bar;
}
}