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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
use better_term::{flush_styles, Color};
#[cfg(feature = "crossterm")]
use crossterm::{cursor, execute, terminal};
use std::error::Error;
#[cfg(feature = "crossterm")]
use std::io::stdout;
/// The Type of bar to be used
#[derive(Debug, Clone)]
pub enum BarType {
/// creates a bar that looks like "████████████████████"
Bar, // [██████████]
/// creates a bar that looks like "████████████████████"
RawBar, // ██████
/// Cycles through ., .., and ...
Dots, // ...
/// Cycles through |, /, -, and \
Line, // |
}
#[derive(Debug, Clone)]
pub struct Bar {
// crossterm position handling
#[cfg(feature = "crossterm")]
x: u16,
#[cfg(feature = "crossterm")]
y: u16,
// settings
color: bool,
bar_type: BarType,
show_percent: bool,
bar_length: u16,
// for special types
repeat_at: u8,
// value the bar is at
percent: u8,
}
impl Bar {
/// create a new progress bar when not using crossterm
/// # parameters
/// bar_type: the type of bar to display
/// color: if it should output in color
/// show_percent: if it should show the percentage next to the bar
/// bar_length: How long the bar should be
#[cfg(not(feature = "crossterm"))]
pub fn new(bar_type: BarType, color: bool, show_percent: bool, bar_length: u16) -> Self {
Self {
percent: 0,
color,
bar_type,
bar_length,
show_percent,
repeat_at: 0,
}
}
#[cfg(not(feature = "crossterm"))]
fn set_pos(&mut self) {
print!("\r");
}
/// create a new progress bar when using crossterm
/// # parameters
/// bar_type: the type of bar to display
/// color: if it should output in color
/// show_percent: if it should show the percentage next to the bar
/// bar_length: How long the bar should be
#[cfg(feature = "crossterm")]
pub fn new(bar_type: BarType, color: bool, show_percent: bool, bar_length: u16) -> Self {
Self::new_at(0, 0, bar_type, color, show_percent, bar_length)
}
/// Create a new bar using crossterm at a location
/// # parameters
/// x: the x location to put the bar (0 is the left)
/// y: the y location to put the bar (0 is the top)
/// bar_type: the type of bar to display
/// color: if it should output in color
/// show_percent: if it should show the percentage next to the bar
/// bar_length: How long the bar should be
#[cfg(feature = "crossterm")]
pub fn new_at(
x: u16,
y: u16,
bar_type: BarType,
color: bool,
show_percent: bool,
bar_length: u16,
) -> Self {
Self {
x,
y,
percent: 0,
color,
bar_type,
bar_length,
show_percent,
repeat_at: 0,
}
}
/// Creates a new bar using crossterm at the cursor's current position
/// # parameters
/// bar_type: the type of bar to display
/// color: if it should output in color
/// show_percent: if it should show the percentage next to the bar
/// bar_length: How long the bar should be
#[cfg(feature = "crossterm")]
pub fn new_at_cursor(
bar_type: BarType,
color: bool,
show_percent: bool,
bar_length: u16,
) -> Result<Self, String> {
let pos = crossterm::cursor::position();
if pos.is_err() {
return Err(pos.unwrap_err().to_string());
}
let (x, y) = pos.unwrap();
Ok(Self::new_at(
x,
y,
bar_type,
color,
show_percent,
bar_length,
))
}
#[cfg(feature = "crossterm")]
fn set_pos(&mut self) -> Result<(), String> {
let r = execute!(stdout(), cursor::MoveTo(self.x, self.y));
if r.is_err() {
return Err(r.unwrap_err().to_string());
}
Ok(())
}
/// Update the bar's progress
/// # Parameters
/// percent: the new percentage (0 to 100)
pub fn update(&mut self, mut percent: u8) {
if percent > 100 {
percent = 100;
}
self.percent = percent;
}
fn draw_dots_and_line(&mut self, output: String, color: Color) -> String {
format!(
"{}{}",
output,
if self.show_percent {
if self.color {
format!(" {}{}%", color, self.percent)
} else {
format!(" {}%", self.percent)
}
} else {
format!("")
}
)
}
pub fn clear_term(&mut self) -> Result<(), Box<dyn Error>> {
execute!(stdout(), terminal::Clear(terminal::ClearType::All))?;
Ok(())
}
/// Draw the bar at its set location
pub fn draw(&mut self) {
#[cfg(feature = "crossterm")]
{
let r = self.set_pos();
if r.is_err() {
panic!("Failed to set cursor position!");
}
}
#[cfg(not(feature = "crossterm"))]
self.set_pos();
// get the current completion color of the bar
// only used if self.color is true
let red = 255 - ((self.percent as f32 / 100.0) * 200.0) as u8;
let green = (self.percent as f32 / 100.0 * 200.0) as u8;
let color = Color::RGB(red, green, 25);
let output = match self.bar_type {
BarType::RawBar => {
let chunk_weight = 100 / self.bar_length;
// set how complete the bar should be
let bar_completion = self.percent as usize / chunk_weight as usize;
if self.color {
// create the colored bar
let bar = format!("{}{}", color, "█".repeat(bar_completion));
format!(
"{}{}",
bar,
if self.show_percent {
format!(" {}{}{}%", color, self.percent, Color::White)
} else {
format!("")
}
)
} else {
// create the bar
let bar = format!("{}", "█".repeat(bar_completion));
format!(
"{}{}",
bar,
if self.show_percent {
format!(" {}%", self.percent)
} else {
format!("")
}
)
}
}
BarType::Bar => {
let chunk_weight = 100 / self.bar_length;
// set how complete the bar should be
let bar_completion = self.percent as usize / chunk_weight as usize;
let mut bar_uncomplete = (100 - (self.percent)) as usize / chunk_weight as usize;
// handle if the bar needs to be resized because of rounding issues
let add = bar_completion + bar_uncomplete;
if add < self.bar_length as usize {
bar_uncomplete += 1;
}
if add > self.bar_length as usize {
bar_uncomplete -= 1;
}
if self.color {
// create the main bar
let completed_bar = format!("{}{}", color, "█".repeat(bar_completion));
let uncompleted_bar =
format!("{}{}", Color::BrightBlack, "█".repeat(bar_uncomplete));
// format the bar and return it
format!(
"{dc}[{}{}{dc}]{}",
completed_bar,
uncompleted_bar,
if self.show_percent {
format!(" {}{}{}%", color, self.percent, Color::White)
} else {
format!("")
},
dc = Color::White
)
} else {
// create the main bar
let completed_bar = format!("{}", "█".repeat(bar_completion));
let uncompleted_bar = format!("{}", "=".repeat(bar_uncomplete));
// format the bar and return it
format!(
"[{}{}]{}",
completed_bar,
uncompleted_bar,
if self.show_percent {
format!(" {}%", self.percent)
} else {
format!("")
}
)
}
}
BarType::Dots => {
if self.percent % 2 == 0 {
self.repeat_at += 1;
if self.repeat_at == 3 {
self.repeat_at = 0;
}
}
let output = match self.repeat_at {
0 => {
format!(". ")
}
1 => {
format!(".. ")
}
_ => {
format!("...")
}
};
self.draw_dots_and_line(output, color)
}
BarType::Line => {
if self.percent % 2 == 0 {
self.repeat_at += 1;
if self.repeat_at == 4 {
self.repeat_at = 0;
}
}
let output = match self.repeat_at {
0 => {
format!("|")
}
1 => {
format!("/")
}
2 => {
format!("-")
}
_ => {
format!("\\")
}
};
self.draw_dots_and_line(output, color)
}
};
#[cfg(feature = "crossterm")]
print!("{}", output);
#[cfg(not(feature = "crossterm"))]
print!("\r{}", output);
if self.color {
flush_styles()
}
}
pub fn is_complete(&self) -> bool {
self.percent == 100
}
}