Skip to content

Commit

Permalink
Add get position API (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmacedoeu authored and mitsuhiko committed Sep 7, 2019
1 parent c6e9871 commit 958dc9b
Showing 1 changed file with 25 additions and 0 deletions.
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 @@ -676,6 +688,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 @@ -721,6 +737,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

0 comments on commit 958dc9b

Please sign in to comment.