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
pub struct ProgressBar {
pb: indicatif::ProgressBar,
count: usize,
total: usize,
}
impl ProgressBar {
#[inline]
#[must_use]
pub fn new(msg: &str, total: usize) -> Self {
debug_assert!(total > 0);
let pb = indicatif::ProgressBar::new(total as u64);
pb.set_style(
indicatif::ProgressStyle::default_bar()
.template("{spinner:.green} [{elapsed_precise}] [{bar:40.green/red}] [{pos}/{len}] {percent}% ({eta}) {msg}")
.progress_chars("\\/")
);
pb.set_message(msg);
Self {
pb,
count: 0,
total,
}
}
#[inline]
pub fn tick(&mut self) {
self.count += 1;
self.pb.inc(1);
}
#[inline]
pub fn block(&mut self, size: usize) -> Option<(usize, usize)> {
debug_assert!(size > 0);
if self.count >= self.total {
None
} else {
let remaining = self.total - self.count;
let alloc = size.min(remaining);
let start = self.count;
let end = start + alloc;
self.count += alloc;
self.pb.inc(alloc as u64);
Some((start, end))
}
}
#[inline]
#[must_use]
pub const fn is_done(&self) -> bool {
self.count >= self.total
}
#[inline]
pub fn finish_with_message(&mut self, msg: &'static str) {
self.pb.finish_with_message(msg);
}
}