From f709f9f37ebb67946d5491ef697dac08389c24fc Mon Sep 17 00:00:00 2001 From: cdellacqua Date: Thu, 7 Nov 2024 23:13:55 +0100 Subject: [PATCH 1/2] feat: allow constructing and setting the progress bar len to `None` --- src/progress_bar.rs | 17 +++++++++++++++++ src/state.rs | 5 +++++ 2 files changed, 22 insertions(+) diff --git a/src/progress_bar.rs b/src/progress_bar.rs index fec1fd76..02600b1c 100644 --- a/src/progress_bar.rs +++ b/src/progress_bar.rs @@ -47,6 +47,18 @@ impl ProgressBar { Self::with_draw_target(Some(len), ProgressDrawTarget::stderr()) } + /// Creates a new progress bar without a specified length + /// + /// This progress bar by default draws directly to stderr, and refreshes a maximum of 20 times + /// a second. To change the refresh rate, [set] the [draw target] to one with a different refresh + /// rate. + /// + /// [set]: ProgressBar::set_draw_target + /// [draw target]: ProgressDrawTarget + pub fn empty() -> Self { + Self::with_draw_target(None, ProgressDrawTarget::stderr()) + } + /// Creates a completely hidden progress bar /// /// This progress bar still responds to API changes but it does not have a length or render in @@ -263,6 +275,11 @@ impl ProgressBar { } } + /// Sets the length of the progress bar to `None` + pub fn unset_length(&self) { + self.state().unset_length(Instant::now()); + } + /// Sets the length of the progress bar pub fn set_length(&self, len: u64) { self.state().set_length(Instant::now(), len); diff --git a/src/state.rs b/src/state.rs index 136e2307..2f9100e2 100644 --- a/src/state.rs +++ b/src/state.rs @@ -96,6 +96,11 @@ impl BarState { } } + pub(crate) fn unset_length(&mut self, now: Instant) { + self.state.len = None; + self.update_estimate_and_draw(now); + } + pub(crate) fn set_length(&mut self, now: Instant, len: u64) { self.state.len = Some(len); self.update_estimate_and_draw(now); From 836e3dd388dde92d2e4c1c7dfb177b8f329e6fb3 Mon Sep 17 00:00:00 2001 From: cdellacqua Date: Tue, 12 Nov 2024 14:00:07 +0100 Subject: [PATCH 2/2] refactor: rename ProgressBar::empty to ProgressBar::no_length --- src/progress_bar.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/progress_bar.rs b/src/progress_bar.rs index 02600b1c..0c850e05 100644 --- a/src/progress_bar.rs +++ b/src/progress_bar.rs @@ -55,7 +55,7 @@ impl ProgressBar { /// /// [set]: ProgressBar::set_draw_target /// [draw target]: ProgressDrawTarget - pub fn empty() -> Self { + pub fn no_length() -> Self { Self::with_draw_target(None, ProgressDrawTarget::stderr()) }