-
Notifications
You must be signed in to change notification settings - Fork 236
/
state.rs
446 lines (394 loc) · 14.3 KB
/
state.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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
use std::borrow::Cow;
use std::fmt;
use std::io;
use std::thread;
use std::time::{Duration, Instant};
use crate::draw_target::ProgressDrawTarget;
use crate::style::ProgressStyle;
pub(crate) struct BarState {
pub(crate) draw_target: ProgressDrawTarget,
pub(crate) on_finish: ProgressFinish,
pub(crate) style: ProgressStyle,
pub(crate) state: ProgressState,
}
impl BarState {
/// Finishes the progress bar using the [`ProgressFinish`] behavior stored
/// in the [`ProgressStyle`].
pub(crate) fn finish_using_style(&mut self, now: Instant) {
match &self.on_finish {
ProgressFinish::AndLeave => self.finish_and_leave(now),
ProgressFinish::AtCurrentPos => self.finish_at_current_pos(now),
ProgressFinish::WithMessage(msg) => {
// Equivalent to `self.finish_with_message` but avoids borrow checker error
self.state.message.clone_from(msg);
self.finish_and_leave(now);
}
ProgressFinish::AndClear => self.finish_and_clear(now),
ProgressFinish::Abandon => self.abandon(now),
ProgressFinish::AbandonWithMessage(msg) => {
// Equivalent to `self.abandon_with_message` but avoids borrow checker error
self.state.message.clone_from(msg);
self.abandon(now);
}
}
}
/// Finishes the progress bar and leaves the current message.
pub(crate) fn finish_and_leave(&mut self, now: Instant) {
self.update(now, true, |state| {
state.pos = state.len;
state.status = Status::DoneVisible;
});
}
/// Finishes the progress bar at current position and leaves the current message.
pub(crate) fn finish_at_current_pos(&mut self, now: Instant) {
self.update(now, true, |state| {
state.status = Status::DoneVisible;
});
}
/// Finishes the progress bar and completely clears it.
pub(crate) fn finish_and_clear(&mut self, now: Instant) {
self.update(now, true, |state| {
state.pos = state.len;
state.status = Status::DoneHidden;
});
}
/// Finishes the progress bar and leaves the current message and progress.
pub(crate) fn abandon(&mut self, now: Instant) {
self.update(now, true, |state| {
state.status = Status::DoneVisible;
});
}
/// Mutate the state, then draw if necessary
pub(crate) fn update<F: FnOnce(&mut ProgressState)>(
&mut self,
now: Instant,
force_draw: bool,
f: F,
) {
let updated = self.state.update(now, f);
if force_draw || updated {
self.draw(force_draw, now).ok();
}
}
pub(crate) fn draw(&mut self, mut force_draw: bool, now: Instant) -> io::Result<()> {
// we can bail early if the draw target is hidden.
if self.draw_target.is_hidden() {
return Ok(());
}
let width = self.draw_target.width();
force_draw |= self.state.is_finished();
let mut drawable = match self.draw_target.drawable(force_draw, now) {
Some(drawable) => drawable,
None => return Ok(()),
};
// `|| self.is_finished()` should not be needed here, but we used to always for draw for
// finished progress bar, so it's kept as to not cause compatibility issues in weird cases.
let mut draw_state = drawable.state();
if self.state.should_render() {
self.style
.format_state(&self.state, &mut draw_state.lines, width);
}
drop(draw_state);
drawable.draw()
}
}
impl Drop for BarState {
fn drop(&mut self) {
// Progress bar is already finished. Do not need to do anything.
if self.state.is_finished() {
return;
}
self.finish_using_style(Instant::now());
}
}
/// The state of a progress bar at a moment in time.
pub struct ProgressState {
pub pos: u64,
pub len: u64,
pub(crate) tick: u64,
pub(crate) started: Instant,
pub(crate) message: Cow<'static, str>,
pub(crate) prefix: Cow<'static, str>,
pub(crate) status: Status,
pub(crate) est: Estimate,
pub(crate) tick_thread: Option<thread::JoinHandle<()>>,
pub(crate) steady_tick: u64,
}
impl ProgressState {
pub(crate) fn new(len: u64) -> Self {
Self {
message: "".into(),
prefix: "".into(),
pos: 0,
len,
tick: 0,
status: Status::InProgress,
started: Instant::now(),
est: Estimate::new(),
tick_thread: None,
steady_tick: 0,
}
}
/// Indicates that the progress bar finished.
pub fn is_finished(&self) -> bool {
match self.status {
Status::InProgress => false,
Status::DoneVisible => true,
Status::DoneHidden => true,
}
}
/// Returns `false` if the progress bar should no longer be
/// drawn.
pub(crate) fn should_render(&self) -> bool {
!matches!(self.status, Status::DoneHidden)
}
/// Returns the completion as a floating-point number between 0 and 1
pub fn fraction(&self) -> f32 {
let pct = match (self.pos, self.len) {
(_, 0) => 1.0,
(0, _) => 0.0,
(pos, len) => pos as f32 / len as f32,
};
pct.max(0.0).min(1.0)
}
/// Returns the current message of the progress bar.
pub(crate) fn message(&self) -> &str {
&self.message
}
/// Returns the current prefix of the progress bar.
pub(crate) fn prefix(&self) -> &str {
&self.prefix
}
/// The expected ETA
pub fn eta(&self) -> Duration {
if self.len == !0 || self.is_finished() {
return Duration::new(0, 0);
}
let t = self.est.seconds_per_step();
secs_to_duration(t * self.len.saturating_sub(self.pos) as f64)
}
/// The expected total duration (that is, elapsed time + expected ETA)
pub(crate) fn duration(&self) -> Duration {
if self.len == !0 || self.is_finished() {
return Duration::new(0, 0);
}
self.started.elapsed() + self.eta()
}
/// The number of steps per second
pub(crate) fn per_sec(&self) -> f64 {
if matches!(&self.status, Status::InProgress) {
let per_sec = 1.0 / self.est.seconds_per_step();
if per_sec.is_nan() {
0.0
} else {
per_sec
}
} else {
self.len as f64 / self.started.elapsed().as_secs_f64()
}
}
/// Call the provided `FnOnce` to update the state. If a draw should be run, returns `true`.
pub(crate) fn update<F: FnOnce(&mut ProgressState)>(&mut self, now: Instant, f: F) -> bool {
let old = (self.pos, self.tick);
f(self);
let new = (self.pos, self.tick);
if new.0 != old.0 {
self.est.record_step(new.0, now);
}
old != new
}
}
/// Ring buffer with constant capacity. Used by `ProgressBar`s to display `{eta}`, `{eta_precise}`,
/// and `{*_per_sec}`.
pub(crate) struct Estimate {
buf: Box<[f64; 15]>,
/// Lower 4 bits signify the current length, meaning how many values of `buf` are actually
/// meaningful (and not just set to 0 by initialization).
///
/// The upper 4 bits signify the last used index in the `buf`. The estimate is currently
/// implemented as a ring buffer and when recording a new step the oldest value is overwritten.
/// Last index is the most recently used position, and as elements are always stored with
/// insertion order, `last_index + 1` is the least recently used position and is the first
/// to be overwritten.
data: u8,
start_time: Instant,
start_value: u64,
}
impl Estimate {
fn len(&self) -> u8 {
self.data & 0x0F
}
fn set_len(&mut self, len: u8) {
// Sanity check to make sure math is correct as otherwise it could result in unexpected bugs
debug_assert!(len < 16);
self.data = (self.data & 0xF0) | len;
}
fn last_idx(&self) -> u8 {
(self.data & 0xF0) >> 4
}
fn set_last_idx(&mut self, last_idx: u8) {
// This will wrap last_idx on overflow (setting to 16 will result in 0); this is fine
// because Estimate::buf is 15 elements long and this is a ring buffer, so overwriting
// the oldest value is correct
self.data = ((last_idx & 0x0F) << 4) | (self.data & 0x0F);
}
fn new() -> Self {
let this = Self {
buf: Box::new([0.0; 15]),
data: 0,
start_time: Instant::now(),
start_value: 0,
};
// Make sure not to break anything accidentally as self.data can't handle bufs longer than
// 15 elements (not enough space in a u8)
debug_assert!(this.buf.len() < 16);
this
}
pub(crate) fn reset(&mut self, start_value: u64) {
self.start_time = Instant::now();
self.start_value = start_value;
self.data = 0;
}
fn record_step(&mut self, value: u64, current_time: Instant) {
let elapsed = current_time - self.start_time;
let item = {
let divisor = value.saturating_sub(self.start_value) as f64;
if divisor == 0.0 {
0.0
} else {
duration_to_secs(elapsed) / divisor
}
};
self.push(item);
}
/// Adds the `value` into the buffer, overwriting the oldest one if full, or increasing length
/// by 1 and appending otherwise.
fn push(&mut self, value: f64) {
let len = self.len();
let last_idx = self.last_idx();
if self.buf.len() > usize::from(len) {
// Buffer isn't yet full, increase it's size
self.set_len(len + 1);
self.buf[usize::from(last_idx)] = value;
} else {
// Buffer is full, overwrite the oldest value
let idx = last_idx % len;
self.buf[usize::from(idx)] = value;
}
self.set_last_idx(last_idx + 1);
}
/// Average time per step in seconds, using rolling buffer of last 15 steps
fn seconds_per_step(&self) -> f64 {
let len = self.len();
self.buf[0..usize::from(len)].iter().sum::<f64>() / f64::from(len)
}
}
impl fmt::Debug for Estimate {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Estimate")
.field("buf", &self.buf)
.field("len", &self.len())
.field("last_idx", &self.last_idx())
.field("start_time", &self.start_time)
.field("start_value", &self.start_value)
.finish()
}
}
/// Behavior of a progress bar when it is finished
///
/// This is invoked when a [`ProgressBar`] or [`ProgressBarIter`] completes and
/// [`ProgressBar::is_finished`] is false.
///
/// [`ProgressBar`]: crate::ProgressBar
/// [`ProgressBarIter`]: crate::ProgressBarIter
/// [`ProgressBar::is_finished`]: crate::ProgressBar::is_finished
#[derive(Clone, Debug)]
pub enum ProgressFinish {
/// Finishes the progress bar and leaves the current message
///
/// Same behavior as calling [`ProgressBar::finish()`](crate::ProgressBar::finish).
AndLeave,
/// Finishes the progress bar at current position and leaves the current message
///
/// Same behavior as calling [`ProgressBar::finish_at_current_pos()`](crate::ProgressBar::finish_at_current_pos).
AtCurrentPos,
/// Finishes the progress bar and sets a message
///
/// Same behavior as calling [`ProgressBar::finish_with_message()`](crate::ProgressBar::finish_with_message).
WithMessage(Cow<'static, str>),
/// Finishes the progress bar and completely clears it (this is the default)
///
/// Same behavior as calling [`ProgressBar::finish_and_clear()`](crate::ProgressBar::finish_and_clear).
AndClear,
/// Finishes the progress bar and leaves the current message and progress
///
/// Same behavior as calling [`ProgressBar::abandon()`](crate::ProgressBar::abandon).
Abandon,
/// Finishes the progress bar and sets a message, and leaves the current progress
///
/// Same behavior as calling [`ProgressBar::abandon_with_message()`](crate::ProgressBar::abandon_with_message).
AbandonWithMessage(Cow<'static, str>),
}
impl Default for ProgressFinish {
fn default() -> Self {
Self::AndClear
}
}
fn duration_to_secs(d: Duration) -> f64 {
d.as_secs() as f64 + f64::from(d.subsec_nanos()) / 1_000_000_000f64
}
fn secs_to_duration(s: f64) -> Duration {
let secs = s.trunc() as u64;
let nanos = (s.fract() * 1_000_000_000f64) as u32;
Duration::new(secs, nanos)
}
#[derive(Debug)]
pub(crate) enum Status {
InProgress,
DoneVisible,
DoneHidden,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_time_per_step() {
let test_rate = |items_per_second| {
let mut est = Estimate::new();
let mut current_time = est.start_time;
let mut current_value = 0;
for _ in 0..est.buf.len() {
current_value += items_per_second;
current_time += Duration::from_secs(1);
est.record_step(current_value, current_time);
}
let avg_seconds_per_step = est.seconds_per_step();
assert!(avg_seconds_per_step > 0.0);
assert!(avg_seconds_per_step.is_finite());
let expected_rate = 1.0 / items_per_second as f64;
let absolute_error = (avg_seconds_per_step - expected_rate).abs();
assert!(
absolute_error < f64::EPSILON,
"Expected rate: {}, actual: {}, absolute error: {}",
expected_rate,
avg_seconds_per_step,
absolute_error
);
};
test_rate(1);
test_rate(1_000);
test_rate(1_000_000);
test_rate(1_000_000_000);
test_rate(1_000_000_001);
test_rate(100_000_000_000);
test_rate(1_000_000_000_000);
test_rate(100_000_000_000_000);
test_rate(1_000_000_000_000_000);
}
#[test]
fn test_duration_stuff() {
let duration = Duration::new(42, 100_000_000);
let secs = duration_to_secs(duration);
assert_eq!(secs_to_duration(secs), duration);
}
}