Debian Codegraph @

rust/hg-core/src/progress.rs

raw
1 //! Progress-bar related things
2
3 use std::sync::atomic::AtomicBool;
4 use std::sync::atomic::Ordering;
5 use std::time::Duration;
6
7 use indicatif::ProgressBar;
8 use indicatif::ProgressDrawTarget;
9 use indicatif::ProgressStyle;
10
11 /// A generic determinate progress bar trait
12 pub trait Progress: Send + Sync + 'static {
13 /// Set the current position and optionally the total
14 fn update(&self, pos: u64, total: Option<u64>);
15 /// Increment the current position and optionally the total
16 fn increment(&self, step: u64, total: Option<u64>);
17 /// Declare that progress is over and the progress bar should be deleted
18 fn complete(self);
19 }
20
21 const PROGRESS_DELAY: Duration = Duration::from_secs(1);
22
23 /// A generic (determinate) progress bar. Stays hidden until `PROGRESS_DELAY`
24 /// to prevent flickering a progress bar for super fast operations.
25 pub struct HgProgressBar {
26 progress: ProgressBar,
27 has_been_shown: AtomicBool,
28 }
29
30 impl HgProgressBar {
31 // TODO pass config to check progress.disable/assume-tty/delay/etc.
32 /// Return a new progress bar with `topic` as the prefix.
33 /// The progress and total are both set to 0, and it is hidden until the
34 /// next call to `update` given that more than a second has elapsed.
35 pub fn new(topic: &str) -> Self {
36 let template =
37 format!("{} {{wide_bar}} {{pos}}/{{len}} {{eta}} ", topic);
38 let style = ProgressStyle::with_template(&template).unwrap();
39 let progress_bar = ProgressBar::new(0).with_style(style);
40 // Hide the progress bar and only show it if we've elapsed more
41 // than a second
42 progress_bar.set_draw_target(ProgressDrawTarget::hidden());
43 Self { progress: progress_bar, has_been_shown: false.into() }
44 }
45
46 /// Called whenever the progress changes to determine whether to start
47 /// showing the progress bar
48 fn maybe_show(&self) {
49 if self.progress.is_hidden() && self.progress.elapsed() > PROGRESS_DELAY
50 {
51 // Catch a race condition whereby we check if it's hidden, then
52 // set the draw target from another thread, then do it again from
53 // this thread, which results in multiple progress bar lines being
54 // left drawn.
55 let has_been_shown =
56 self.has_been_shown.fetch_or(true, Ordering::Relaxed);
57 if !has_been_shown {
58 // Here we are certain that we're the only thread that has
59 // set `has_been_shown` and we can change the draw target
60 self.progress.set_draw_target(ProgressDrawTarget::stderr());
61 self.progress.tick();
62 }
63 }
64 }
65 }
66
67 impl Progress for HgProgressBar {
68 fn update(&self, pos: u64, total: Option<u64>) {
69 self.progress.update(|state| {
70 state.set_pos(pos);
71 if let Some(t) = total {
72 state.set_len(t)
73 }
74 });
75 self.maybe_show();
76 }
77
78 fn increment(&self, step: u64, total: Option<u64>) {
79 self.progress.inc(step);
80 if let Some(t) = total {
81 self.progress.set_length(t)
82 }
83 self.maybe_show();
84 }
85
86 fn complete(self) {
87 self.progress.finish_and_clear();
88 }
89 }