Skip to content

Add dec and dec_length to ProgressBar #690

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

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Loading