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

Fix percent initial value when there is no length #491

Merged
merged 2 commits into from Nov 6, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/state.rs
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
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