Skip to content

Commit

Permalink
Add dec and dec_length to ProgressBar
Browse files Browse the repository at this point in the history
  • Loading branch information
jaheba authored and djc committed Jan 22, 2025
1 parent be119bd commit 2871b47
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/progress_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,15 @@ impl ProgressBar {
}
}

/// Decrease the position of the progress bar by `delta`
pub fn dec(&self, delta: u64) {
self.pos.dec(delta);
let now = Instant::now();
if self.pos.allow(now) {
self.tick_inner(now);
}
}

/// A quick convenience check if the progress bar is hidden
pub fn is_hidden(&self) -> bool {
self.state().draw_target.is_hidden()
Expand Down Expand Up @@ -290,6 +299,11 @@ impl ProgressBar {
self.state().inc_length(Instant::now(), delta);
}

/// Decrease the length of the progress bar
pub fn dec_length(&self, delta: u64) {
self.state().dec_length(Instant::now(), delta);
}

/// Sets the current prefix of the progress bar
///
/// For the prefix to be visible, the `{prefix}` placeholder must be present in the template
Expand Down
11 changes: 11 additions & 0 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ impl BarState {
self.update_estimate_and_draw(now);
}

pub(crate) fn dec_length(&mut self, now: Instant, delta: u64) {
if let Some(len) = self.state.len {
self.state.len = Some(len.saturating_sub(delta));
}
self.update_estimate_and_draw(now);
}

pub(crate) fn set_tab_width(&mut self, tab_width: usize) {
self.tab_width = tab_width;
self.state.message.set_tab_width(tab_width);
Expand Down Expand Up @@ -585,6 +592,10 @@ impl AtomicPosition {
self.pos.fetch_add(delta, Ordering::SeqCst);
}

pub(crate) fn dec(&self, delta: u64) {
self.pos.fetch_sub(delta, Ordering::SeqCst);
}

pub(crate) fn set(&self, pos: u64) {
self.pos.store(pos, Ordering::Release);
}
Expand Down

0 comments on commit 2871b47

Please sign in to comment.