Refactor ansi stripping into nu-utils functions (#6966)

Allows use of slightly optimized variants that check if they have to use
the heavier vte parser. Tries to avoid unnnecessary allocations. Initial
performance characteristics proven out in #4378.

Also reduces boilerplate with right-ward drift.
This commit is contained in:
Stefan Holderbach
2022-11-04 19:49:45 +01:00
committed by GitHub
parent b9195c2668
commit 2c4048eb43
24 changed files with 126 additions and 134 deletions

View File

@ -14,4 +14,5 @@ path = "src/main.rs"
[dependencies]
unicode-width = "0.1.9"
strip-ansi-escapes = "0.1.1"
nu-utils = { path = "../nu-utils", version = "0.70.1" }

View File

@ -91,35 +91,13 @@
//! [`fit_into_width`]: ./struct.Grid.html#method.fit_into_width
//! [`GridOptions`]: ./struct.GridOptions.html
use std::borrow::Cow;
use std::cmp::max;
use std::fmt;
use std::iter::repeat;
use strip_ansi_escapes;
use unicode_width::UnicodeWidthStr;
/// Removes ANSI escape codes and some ASCII control characters
///
/// Keeps `\n` removes `\r`, `\t` etc.
///
/// If parsing fails silently returns the input string
fn strip_ansi(string: &str) -> Cow<str> {
// Check if any ascii control character except LF(0x0A = 10) is present,
// which will be stripped. Includes the primary start of ANSI sequences ESC
// (0x1B = decimal 27)
if string.bytes().any(|x| matches!(x, 0..=9 | 11..=31)) {
if let Ok(stripped) = strip_ansi_escapes::strip(string) {
if let Ok(new_string) = String::from_utf8(stripped) {
return Cow::Owned(new_string);
}
}
}
// Else case includes failures to parse!
Cow::Borrowed(string)
}
fn unicode_width_strip_ansi(astring: &str) -> usize {
strip_ansi(astring).width()
nu_utils::strip_ansi_unlikely(astring).width()
}
/// Alignment indicate on which side the content should stick if some filling