Skip to content

Commit e84863a

Browse files
authored
feat: allow constructing and setting the progress bar len to None (#664)
1 parent 90a1f3b commit e84863a

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/progress_bar.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ impl ProgressBar {
4747
Self::with_draw_target(Some(len), ProgressDrawTarget::stderr())
4848
}
4949

50+
/// Creates a new progress bar without a specified length
51+
///
52+
/// This progress bar by default draws directly to stderr, and refreshes a maximum of 20 times
53+
/// a second. To change the refresh rate, [set] the [draw target] to one with a different refresh
54+
/// rate.
55+
///
56+
/// [set]: ProgressBar::set_draw_target
57+
/// [draw target]: ProgressDrawTarget
58+
pub fn no_length() -> Self {
59+
Self::with_draw_target(None, ProgressDrawTarget::stderr())
60+
}
61+
5062
/// Creates a completely hidden progress bar
5163
///
5264
/// This progress bar still responds to API changes but it does not have a length or render in
@@ -263,6 +275,11 @@ impl ProgressBar {
263275
}
264276
}
265277

278+
/// Sets the length of the progress bar to `None`
279+
pub fn unset_length(&self) {
280+
self.state().unset_length(Instant::now());
281+
}
282+
266283
/// Sets the length of the progress bar
267284
pub fn set_length(&self, len: u64) {
268285
self.state().set_length(Instant::now(), len);

src/state.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ impl BarState {
9696
}
9797
}
9898

99+
pub(crate) fn unset_length(&mut self, now: Instant) {
100+
self.state.len = None;
101+
self.update_estimate_and_draw(now);
102+
}
103+
99104
pub(crate) fn set_length(&mut self, now: Instant, len: u64) {
100105
self.state.len = Some(len);
101106
self.update_estimate_and_draw(now);

0 commit comments

Comments
 (0)