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
/// structure representing a progressbar
#[derive(Clone)]
pub struct ProgressBar {
progress: f32,
text: String,
empty: char,
full: char,
}
impl ProgressBar {
/// create a new progress bar
/// # Arguments
/// *`text`: the text to use for the progress bar
/// *`empty`: the character to use for empty progress bar fields
/// *`full`: the character to use for full progress bar fields
/// # Returns
/// Returns a new progress bar
pub fn new(text: &str, empty: char, full: char) -> ProgressBar {
ProgressBar {
progress: 0.0,
text: text.to_string(),
empty,
full,
}
}
/// increase the current progress
pub fn incr(&mut self) {
if self.progress < 100.0 {
self.progress += 1.0;
}
}
/// set the current progress
pub fn set_progress(&mut self, amount: f32) {
if amount > 100.0 {
self.progress = 100.0;
} else {
self.progress = amount;
}
}
/// render the current progress bar
pub fn render(&self) -> String {
let mut bar: String = String::new();
let mut idx: u8 = 0;
while (idx as f32) < self.progress {
bar.push_str(format!("{}", self.full).as_str());
idx += 1;
}
let leftover: u8 = (100.0 - self.progress).floor() as u8;
idx = 0;
while idx != leftover {
bar.push_str(format!("{}", self.empty).as_str());
idx += 1;
}
format!("{}: [{}] {:.2}%", self.text.clone(), bar, self.progress)
}
}
/// struct representing a progressbar with extra features
#[derive(Clone)]
pub struct ExtProgressBar {
format: String,
text: String,
progress: f32,
}
impl ExtProgressBar {
/// create a new progress bar
pub fn new(format: &str, text: &str) -> ExtProgressBar {
ExtProgressBar {
format: format.to_string(),
text: text.to_string(),
progress: 0.0,
}
}
/// increase current progress by one
pub fn incr(&mut self) {
if self.progress < 100.0 {
self.progress += 1.0;
}
}
/// set the progress to a specific amount
/// # Arguments
/// *`amount`: the amount to set
pub fn set_progress(&mut self, amount: f32) {
if amount > 100.0 {
self.progress = 100.0;
} else {
self.progress = amount;
}
}
/// render the progress bar to a printable string
pub fn render(&self) -> String {
match self.format.len() {
4 => {
let mut format = self.format.chars();
let begin: char = format.next().unwrap();
let full: char = format.next().unwrap();
let empty: char = format.next().unwrap();
let close: char = format.next().unwrap();
let mut bar: String = String::new();
let mut idx: u8 = 0;
while (idx as f32) < self.progress {
bar.push_str(format!("{}", full).as_str());
idx += 1;
}
idx = 0;
let leftover: u8 = (100.0 - self.progress) as u8;
while idx != leftover {
bar.push_str(format!("{}", empty).as_str());
idx += 1;
}
format!(
"{}: {}{}{} {:.2}%",
self.text, begin, bar, close, self.progress
)
}
5 => {
let mut format = self.format.chars();
let begin: char = format.next().unwrap();
let full: char = format.next().unwrap();
let full_end: char = format.next().unwrap();
let empty: char = format.next().unwrap();
let close: char = format.next().unwrap();
let mut bar: String = String::new();
let mut idx: u8 = 0;
while (idx as f32) < self.progress {
if ((self.progress - idx as f32).floor() - 1.0).abs() < 1.0 {
bar.push(full_end);
break;
} else {
bar.push(full);
}
idx += 1;
}
idx = 0;
let leftover: u8 = (100.0 - self.progress).floor() as u8;
while idx != leftover {
bar.push(empty);
idx += 1;
}
format!(
"{}: {}{}{} {:.2}%",
self.text, begin, bar, close, self.progress
)
}
_ => {
let mut bar: String = String::new();
let mut idx: u8 = 0;
while (idx as f32) < self.progress {
if ((self.progress - idx as f32).floor() - 1.0).abs() < 1.0 {
bar.push('>');
} else {
bar.push('=');
}
idx += 1;
}
idx = 0;
let leftover: u8 = (100.0 - self.progress).floor() as u8;
while idx != leftover {
bar.push(' ');
idx += 1;
}
format!("{}: [{}] {:.2}%", self.text, bar, self.progress)
}
}
}
}