Skip to content

Commit

Permalink
feat: allow constructing and setting the progress bar len to None (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
cdellacqua authored Nov 12, 2024
1 parent 90a1f3b commit e84863a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/progress_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 no_length() -> 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
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit e84863a

Please sign in to comment.