From 66a90a660cd1308ba587ac4d44e70692d5ed14d0 Mon Sep 17 00:00:00 2001 From: Raphael Druon Date: Mon, 24 Apr 2023 03:33:17 -0600 Subject: [PATCH] Handle MultiProgress println --- src/multi.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/multi.rs b/src/multi.rs index aa41fff4..84af742f 100644 --- a/src/multi.rs +++ b/src/multi.rs @@ -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!( @@ -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; @@ -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, @@ -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.