diff --git a/src/multi.rs b/src/multi.rs index 49e1e176..86fe9dd9 100644 --- a/src/multi.rs +++ b/src/multi.rs @@ -521,9 +521,14 @@ enum InsertLocation { // Calculate real length based on terminal width // This take in account linewrap from terminal -fn real_line_count>(lines: &[StrRef], width: f64) -> usize { +fn real_line_count>(lines: &[T], width: f64) -> usize { lines.iter().fold(0, |sum, val| { - sum + (console::measure_text_width(val.as_ref()) as f64 / width).ceil() as usize + sum + if val.as_ref().is_empty() { + 1 + } else { + let effective_line_length = console::measure_text_width(val.as_ref()) as f64; + usize::max((effective_line_length / width).ceil() as usize, 1) + } }) } @@ -729,6 +734,37 @@ mod tests { expectation: 4, width: 3.0, }, + Case { + lines: &["1234567890", "", "1234567890"], + expectation: 3, + width: 10.0, + }, + Case { + lines: &["1234567890", "", "1234567890"], + expectation: 5, + width: 5.0, + }, + Case { + lines: &["1234567890", "", "1234567890"], + expectation: 7, + width: 4.0, + }, + Case { + lines: &["aaaaaaaaaaaaa", "", "bbbbbbbbbbbbbbbbb", "", "ccccccc"], + expectation: 8, + width: 7.0, + }, + Case { + lines: &["", "", "", "", ""], + expectation: 5, + width: 6.0, + }, + Case { + // These lines contain only ANSI escape sequences, so they should only count as 1 line + lines: &["\u{1b}[1m\u{1b}[1m\u{1b}[1m", "\u{1b}[1m\u{1b}[1m\u{1b}[1m"], + expectation: 2, + width: 5.0, + }, Case { // These lines contain ANSI escape sequences and two effective chars, so they should only count as 1 line still lines: &[