Skip to content

Commit

Permalink
handle empty lines properly
Browse files Browse the repository at this point in the history
  • Loading branch information
tgolsson committed Dec 8, 2023
1 parent c5f5a53 commit aba298a
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions src/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,9 +521,14 @@ enum InsertLocation {

// Calculate real length based on terminal width
// This take in account linewrap from terminal
fn real_line_count<StrRef: AsRef<str>>(lines: &[StrRef], width: f64) -> usize {
fn real_line_count<T: AsRef<str>>(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)
}
})
}

Expand Down Expand Up @@ -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: &[
Expand Down

0 comments on commit aba298a

Please sign in to comment.