Skip to content

Commit 2871b47

Browse files
jahebadjc
authored andcommitted
Add dec and dec_length to ProgressBar
1 parent be119bd commit 2871b47

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/progress_bar.rs

+14
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,15 @@ impl ProgressBar {
235235
}
236236
}
237237

238+
/// Decrease the position of the progress bar by `delta`
239+
pub fn dec(&self, delta: u64) {
240+
self.pos.dec(delta);
241+
let now = Instant::now();
242+
if self.pos.allow(now) {
243+
self.tick_inner(now);
244+
}
245+
}
246+
238247
/// A quick convenience check if the progress bar is hidden
239248
pub fn is_hidden(&self) -> bool {
240249
self.state().draw_target.is_hidden()
@@ -290,6 +299,11 @@ impl ProgressBar {
290299
self.state().inc_length(Instant::now(), delta);
291300
}
292301

302+
/// Decrease the length of the progress bar
303+
pub fn dec_length(&self, delta: u64) {
304+
self.state().dec_length(Instant::now(), delta);
305+
}
306+
293307
/// Sets the current prefix of the progress bar
294308
///
295309
/// For the prefix to be visible, the `{prefix}` placeholder must be present in the template

src/state.rs

+11
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ impl BarState {
114114
self.update_estimate_and_draw(now);
115115
}
116116

117+
pub(crate) fn dec_length(&mut self, now: Instant, delta: u64) {
118+
if let Some(len) = self.state.len {
119+
self.state.len = Some(len.saturating_sub(delta));
120+
}
121+
self.update_estimate_and_draw(now);
122+
}
123+
117124
pub(crate) fn set_tab_width(&mut self, tab_width: usize) {
118125
self.tab_width = tab_width;
119126
self.state.message.set_tab_width(tab_width);
@@ -585,6 +592,10 @@ impl AtomicPosition {
585592
self.pos.fetch_add(delta, Ordering::SeqCst);
586593
}
587594

595+
pub(crate) fn dec(&self, delta: u64) {
596+
self.pos.fetch_sub(delta, Ordering::SeqCst);
597+
}
598+
588599
pub(crate) fn set(&self, pos: u64) {
589600
self.pos.store(pos, Ordering::Release);
590601
}

0 commit comments

Comments
 (0)