Skip to content

Commit

Permalink
measure_text_width() without constructing a temporary string
Browse files Browse the repository at this point in the history
  • Loading branch information
th1000s authored and dandavison committed Oct 24, 2022
1 parent 60492f3 commit 970000f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/ansi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ pub fn strip_ansi_codes(s: &str) -> String {

pub fn measure_text_width(s: &str) -> usize {
// TODO: how should e.g. '\n' be handled?
strip_ansi_codes(s).width()
ansi_strings_iterator(s).fold(0, |acc, (element, is_ansi)| {
acc + if is_ansi { 0 } else { element.width() }
})
}

/// Truncate string such that `tail` is present as a suffix, preceded by as much of `s` as can be
Expand Down
2 changes: 1 addition & 1 deletion src/delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ impl<'a> StateMachine<'a> {
// \r and \n, in which case byte_lines does not remove the \r. Remove it now.
// TODO: Limit the number of characters we examine when looking for the \r?
if let Some(cr_index) = self.raw_line.rfind('\r') {
if ansi::strip_ansi_codes(&self.raw_line[cr_index + 1..]).is_empty() {
if ansi::measure_text_width(&self.raw_line[cr_index + 1..]) == 0 {
self.raw_line = format!(
"{}{}",
&self.raw_line[..cr_index],
Expand Down

0 comments on commit 970000f

Please # to comment.