Skip to content

Commit

Permalink
Handle MultiProgress println
Browse files Browse the repository at this point in the history
  • Loading branch information
RDruon authored and djc committed May 9, 2023
1 parent 80fd142 commit 66a90a6
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,14 @@ impl MultiState {
if panicking() {
return Ok(());
}
let width = self.width() as f64;
// Calculate real length based on terminal width
// This take in account linewrap from terminal
fn real_len(lines: &[String], width: f64) -> usize {
lines.iter().fold(0, |sum, val| {
sum + (console::measure_text_width(val) as f64 / width).ceil() as usize
})
}

// Assumption: if extra_lines is not None, then it has at least one line
debug_assert_eq!(
Expand All @@ -279,9 +287,8 @@ impl MultiState {
let line_count = member
.draw_state
.as_ref()
.map(|d| d.lines.len())
.map(|d| real_len(&d.lines, width))
.unwrap_or_default();

// Track the total number of zombie lines on the screen.
self.zombie_lines_count += line_count;

Expand All @@ -300,7 +307,7 @@ impl MultiState {
self.zombie_lines_count = 0;
}

let orphan_lines_count = self.orphan_lines.len();
let orphan_lines_count = real_len(&self.orphan_lines, width);
force_draw |= orphan_lines_count > 0;
let mut drawable = match self.draw_target.drawable(force_draw, now) {
Some(drawable) => drawable,
Expand All @@ -313,7 +320,7 @@ impl MultiState {

if let Some(extra_lines) = &extra_lines {
draw_state.lines.extend_from_slice(extra_lines.as_slice());
draw_state.orphan_lines_count += extra_lines.len();
draw_state.orphan_lines_count += real_len(extra_lines, width);
}

// Add lines from `ProgressBar::println` call.
Expand Down

0 comments on commit 66a90a6

Please sign in to comment.