Skip to content
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

Add get position #105

Merged
merged 3 commits into from Sep 7, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/progress.rs
Expand Up @@ -509,6 +509,7 @@ impl ProgressBar {
/// Sets the position of the progress bar.
pub fn set_position(&self, pos: u64) {
self.update_and_draw(|state| {
state.draw_next = pos;
state.pos = pos;
if state.steady_tick == 0 || state.tick == 0 {
state.tick = state.tick.saturating_add(1);
Expand Down Expand Up @@ -562,6 +563,17 @@ impl ProgressBar {
});
}

pub fn reset(&self) {
self.reset_eta();
self.reset_elapsed();
self.update_and_draw(|state| {
state.draw_next = 0;
state.pos = 0;
state.status = Status::InProgress;
});

}

/// Finishes the progress bar and leaves the current message.
pub fn finish(&self) {
self.update_and_draw(|state| {
Expand Down Expand Up @@ -668,6 +680,10 @@ impl ProgressBar {
fn draw(&self) -> io::Result<()> {
draw_state(&self.state)
}

pub fn position(&self) -> u64 {
self.state.read().pos
}
}

fn draw_state(state: &Arc<RwLock<ProgressState>>) -> io::Result<()> {
Expand Down Expand Up @@ -713,6 +729,15 @@ fn test_pbar_overflow() {
pb.finish();
}

#[test]
fn test_get_position() {
let pb = ProgressBar::new(1);
pb.set_draw_target(ProgressDrawTarget::hidden());
pb.inc(2);
let pos = pb.position();
assert_eq!(pos, 2);
}

struct MultiObject {
done: bool,
draw_state: Option<ProgressDrawState>,
Expand Down