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 committed Jan 21, 2025
1 parent be119bd commit ce3354f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
13 changes: 13 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 @@ -289,6 +298,10 @@ impl ProgressBar {
pub fn inc_length(&self, delta: u64) {
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
///
Expand Down
11 changes: 11 additions & 0 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,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 inc_length(&mut self, now: Instant, delta: u64) {
if let Some(len) = self.state.len {
self.state.len = Some(len.saturating_add(delta));
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 ce3354f

Please sign in to comment.