Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow constructing and setting the progress bar len to None #664

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 empty() -> Self {
cdellacqua marked this conversation as resolved.
Show resolved Hide resolved
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