Skip to content

Commit

Permalink
Fix percent initial value when there is no length
Browse files Browse the repository at this point in the history
Read more on issue console-rs#489
  • Loading branch information
devmatteini committed Nov 5, 2022
1 parent 2c85ff8 commit 6685477
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ impl ProgressState {
pub fn fraction(&self) -> f32 {
let pos = self.pos.pos.load(Ordering::Relaxed);
let pct = match (pos, self.len) {
(_, None) => 1.0,
(_, None) => 0.0,
(_, Some(0)) => 1.0,
(0, _) => 0.0,
(pos, Some(len)) => pos as f32 / len as f32,
Expand Down
33 changes: 33 additions & 0 deletions tests/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,39 @@ fn progress_bar_builder_method_order() {
);
}

#[test]
fn progress_bar_percent_with_no_length() {
let in_mem = InMemoryTerm::new(10, 80);
let pb = ProgressBar::with_draw_target(
None,
ProgressDrawTarget::term_like(Box::new(in_mem.clone())),
)
.with_style(ProgressStyle::with_template("{wide_bar} {percent}%").unwrap());

assert_eq!(in_mem.contents(), String::new());

pb.tick();

assert_eq!(
in_mem.contents(),
"░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 0%"
);

pb.set_length(10);

pb.inc(1);
assert_eq!(
in_mem.contents(),
"███████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 10%"
);

pb.finish();
assert_eq!(
in_mem.contents(),
"███████████████████████████████████████████████████████████████████████████ 100%"
);
}

#[test]
fn multi_progress_single_bar_and_leave() {
let in_mem = InMemoryTerm::new(10, 80);
Expand Down

0 comments on commit 6685477

Please sign in to comment.