/
custom.rs
45 lines (38 loc) · 1.07 KB
/
custom.rs
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
use kdam::{tqdm, Bar, BarExt};
use std::{io::Result, num::NonZeroU16};
#[derive(BarExt)]
struct CustomBar {
#[bar]
pb: Bar,
}
impl CustomBar {
/// Render progress bar text.
fn render(&mut self) -> String {
let fmt_percentage = self.pb.fmt_percentage(0);
let padding = 1 + fmt_percentage.chars().count() as u16 + self.pb.animation.spaces() as u16;
let ncols = self.pb.ncols_for_animation(padding);
if ncols == 0 {
self.pb.bar_length = padding - 1;
fmt_percentage
} else {
self.pb.bar_length = padding + ncols;
self.pb.animation.fmt_render(
NonZeroU16::new(ncols).unwrap(),
self.pb.percentage(),
&None,
) + " "
+ &fmt_percentage
}
}
}
fn main() -> Result<()> {
let mut pb = CustomBar {
pb: tqdm!(total = 100, force_refresh = true),
};
for _ in 0..100 {
std::thread::sleep(std::time::Duration::from_secs_f32(0.02));
pb.update(1)?;
}
eprintln!();
Ok(())
}