/
lib.rs
225 lines (189 loc) · 6.91 KB
/
lib.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
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
use indicatif::{ProgressBar, ProgressStyle};
use std::borrow::Cow;
use std::time::{Duration, Instant};
/// How often should progress bars be redrawn?
pub const PROGRESS_UPDATE_INTERVAL: Duration = Duration::from_millis(500);
// NOTE: indicatif uses an estimation algorithm for ETA and the throughput that doesn't
// work well for Nosey Parker, resulting in wildly variable and inaccurate values.
// The problem is with the library's internal `Estimator` type.
//
// Until that's fixed or we otherwise work around it, we avoid showing ETAs and rates.
//
// See https://github.com/console-rs/indicatif/issues/394.
// XXX Consider switching from indicatif to status_line: https://docs.rs/status-line/latest/status_line/struct.StatusLine.html
/// Wraps an `indicatif::ProgressBar` with a local buffer to reduce update contention overhead.
/// Updates are batched an the progress bar is updated only every `PROGRESS_UPDATE_INTERVAL`.
///
/// XXX this abstraction is probably a little bit broken: if there are multiple clones of a
/// progress bar out there, and `.finish*()` is called on one of them, the others may have
/// unsynced local counts.
pub struct Progress {
inc_since_sync: u64,
last_sync: Instant,
inner: ProgressBar,
finish_style: Option<ProgressStyle>,
}
impl Progress {
pub fn new_spinner<T: Into<Cow<'static, str>>>(message: T, enabled: bool) -> Self {
let inner = if enabled {
let style = ProgressStyle::with_template("{spinner} {msg} [{elapsed_precise}]")
.expect("progress bar style template should compile");
let inner = ProgressBar::new_spinner()
.with_style(style)
.with_message(message);
inner.enable_steady_tick(PROGRESS_UPDATE_INTERVAL);
inner
} else {
ProgressBar::hidden()
};
let finish_style = ProgressStyle::with_template("{msg} [{elapsed_precise}]")
.expect("progress bar style template should compile");
Progress {
inc_since_sync: 0,
last_sync: Instant::now(),
inner,
finish_style: Some(finish_style),
}
}
#[inline]
pub fn set_message<T: Into<Cow<'static, str>>>(&mut self, message: T) {
self.inner.set_message(message);
}
pub fn new_countup_spinner<T: Into<Cow<'static, str>>>(message: T, enabled: bool) -> Self {
let inner = if enabled {
let style =
ProgressStyle::with_template("{spinner} {msg} {human_len} [{elapsed_precise}]")
.expect("progress bar style template should compile");
let inner = ProgressBar::new_spinner()
.with_style(style)
.with_message(message);
inner.enable_steady_tick(PROGRESS_UPDATE_INTERVAL);
inner
} else {
ProgressBar::hidden()
};
let finish_style = ProgressStyle::with_template("{msg} [{elapsed_precise}]")
.expect("progress bar style template should compile");
Progress {
inc_since_sync: 0,
last_sync: Instant::now(),
inner,
finish_style: Some(finish_style),
}
}
pub fn new_bytes_spinner<T: Into<Cow<'static, str>>>(message: T, enabled: bool) -> Self {
let inner = if enabled {
let style =
ProgressStyle::with_template("{spinner} {msg} {total_bytes} [{elapsed_precise}]")
.expect("progress bar style template should compile");
let inner = ProgressBar::new_spinner()
.with_style(style)
.with_message(message);
inner.enable_steady_tick(PROGRESS_UPDATE_INTERVAL);
inner
} else {
ProgressBar::hidden()
};
let finish_style = ProgressStyle::with_template("{msg} [{elapsed_precise}]")
.expect("progress bar style template should compile");
Progress {
inc_since_sync: 0,
last_sync: Instant::now(),
inner,
finish_style: Some(finish_style),
}
}
pub fn new_bar<T: Into<Cow<'static, str>>>(total: u64, message: T, enabled: bool) -> Self {
let style = ProgressStyle::with_template(
"{msg} {bar} {percent:>3}% {pos}/{len} [{elapsed_precise}]",
)
.expect("progress bar style template should compile");
let inner = if enabled {
let inner = ProgressBar::new(total)
.with_style(style)
.with_message(message);
inner.enable_steady_tick(PROGRESS_UPDATE_INTERVAL);
inner
} else {
ProgressBar::hidden()
};
Progress {
inc_since_sync: 0,
last_sync: Instant::now(),
inner,
finish_style: None,
}
}
pub fn new_bytes_bar<T: Into<Cow<'static, str>>>(
total_bytes: u64,
message: T,
enabled: bool,
) -> Self {
let style = ProgressStyle::with_template(
"{msg} {bar} {percent:>3}% {bytes}/{total_bytes} [{elapsed_precise}]",
)
.expect("progress bar style template should compile");
let inner = if enabled {
let inner = ProgressBar::new(total_bytes)
.with_style(style)
.with_message(message);
inner.enable_steady_tick(PROGRESS_UPDATE_INTERVAL);
inner
} else {
ProgressBar::hidden()
};
Progress {
inc_since_sync: 0,
last_sync: Instant::now(),
inner,
finish_style: None,
}
}
#[inline]
pub fn suspend<F: FnOnce() -> R, R>(&self, f: F) -> R {
self.inner.suspend(f)
}
#[inline]
pub fn inc(&mut self, amount: u64) {
self.inc_since_sync += amount;
if self.last_sync.elapsed() >= PROGRESS_UPDATE_INTERVAL {
self.sync();
}
}
pub fn finish_with_message<T: Into<Cow<'static, str>>>(&mut self, message: T) {
self.sync();
match &self.finish_style {
Some(style) => self.inner.set_style(style.clone()),
None => {}
};
self.inner.finish_with_message(message);
}
pub fn finish(&mut self) {
self.sync();
match &self.finish_style {
Some(style) => self.inner.set_style(style.clone()),
None => {}
};
self.inner.finish();
}
fn sync(&mut self) {
self.inner.inc(self.inc_since_sync);
self.inc_since_sync = 0;
self.last_sync = Instant::now();
}
}
impl Drop for Progress {
fn drop(&mut self) {
self.sync();
}
}
impl Clone for Progress {
fn clone(&self) -> Self {
Progress {
inc_since_sync: 0,
last_sync: Instant::now(),
inner: self.inner.clone(),
finish_style: self.finish_style.clone(),
}
}
}