diff options
author | Thomas Otto <th1000s@posteo.net> | 2022-10-24 23:21:41 +0200 |
---|---|---|
committer | Dan Davison <dandavison7@gmail.com> | 2022-10-24 17:46:13 -0400 |
commit | 970000f36886bdab908d1037dbc1148abde52d5e (patch) | |
tree | 5ac1cc2c662991631fd804c706fb8f790ed91d1f | |
parent | 60492f318f6e46bfb8ba0b4396cce3210897a1c0 (diff) |
measure_text_width() without constructing a temporary string
-rw-r--r-- | src/ansi/mod.rs | 4 | ||||
-rw-r--r-- | src/delta.rs | 2 |
2 files changed, 4 insertions, 2 deletions
diff --git a/src/ansi/mod.rs b/src/ansi/mod.rs index 90eac1de..ae202d31 100644 --- a/src/ansi/mod.rs +++ b/src/ansi/mod.rs @@ -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 diff --git a/src/delta.rs b/src/delta.rs index 741d3c63..1b0b2980 100644 --- a/src/delta.rs +++ b/src/delta.rs @@ -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], |