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 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
8 changes: 4 additions & 4 deletions src/in_memory.rs
Expand Up @@ -67,7 +67,7 @@ impl TermLike for InMemoryTerm {
.state
.lock()
.unwrap()
.write_str(&*format!("\x1b[{}A", n)),
.write_str(&format!("\x1b[{}A", n)),
}
}

Expand All @@ -78,7 +78,7 @@ impl TermLike for InMemoryTerm {
.state
.lock()
.unwrap()
.write_str(&*format!("\x1b[{}B", n)),
.write_str(&format!("\x1b[{}B", n)),
}
}

Expand All @@ -89,7 +89,7 @@ impl TermLike for InMemoryTerm {
.state
.lock()
.unwrap()
.write_str(&*format!("\x1b[{}C", n)),
.write_str(&format!("\x1b[{}C", n)),
}
}

Expand All @@ -100,7 +100,7 @@ impl TermLike for InMemoryTerm {
.state
.lock()
.unwrap()
.write_str(&*format!("\x1b[{}D", n)),
.write_str(&format!("\x1b[{}D", n)),
}
}

Expand Down
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
8 changes: 2 additions & 6 deletions src/style.rs
Expand Up @@ -190,11 +190,7 @@ impl ProgressStyle {
let entirely_filled = fill as usize;
// 1 if the bar is not entirely empty or full (meaning we need to draw the "current"
// character between the filled and "to do" segment), 0 otherwise.
let head = if fill > 0.0 && entirely_filled < width {
1
} else {
0
};
let head = usize::from(fill > 0.0 && entirely_filled < width);

let cur = if head == 1 {
// Number of fine-grained progress entries in progress_chars.
Expand Down Expand Up @@ -423,7 +419,7 @@ impl<'a> WideElement<'a> {
buf: &mut String,
width: u16,
) -> String {
let left = (width as usize).saturating_sub(measure_text_width(&*cur.replace('\x00', "")));
let left = (width as usize).saturating_sub(measure_text_width(&cur.replace('\x00', "")));
match self {
Self::Bar { alt_style } => cur.replace(
'\x00',
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