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

@ -77,7 +77,6 @@ serde_yaml = "0.9.4"
sha2 = "0.10.0"
# Disable default features b/c the default features build Git (very slow to compile)
shadow-rs = { version = "0.16.1", default-features = false }
strip-ansi-escapes = "0.1.1"
sysinfo = "0.26.2"
terminal_size = "0.2.1"
thiserror = "1.0.31"

View File

@ -363,15 +363,12 @@ pub fn highlight_search_string(
}
};
// strip haystack to remove existing ansi style
let stripped_haystack: String = match strip_ansi_escapes::strip(haystack) {
Ok(i) => String::from_utf8(i).unwrap_or_else(|_| String::from(haystack)),
Err(_) => String::from(haystack),
};
let stripped_haystack = nu_utils::strip_ansi_likely(haystack);
let mut last_match_end = 0;
let style = Style::new().fg(White).on(Red);
let mut highlighted = String::new();
for cap in regex.captures_iter(stripped_haystack.as_str()) {
for cap in regex.captures_iter(stripped_haystack.as_ref()) {
match cap {
Ok(capture) => {
let start = match capture.get(0) {

View File

@ -248,14 +248,8 @@ fn nu_value_to_string(value: Value, separator: &str, config: &Config) -> String
}
Value::String { val, .. } => {
// don't store ansi escape sequences in the database
let stripped = {
match strip_ansi_escapes::strip(&val) {
Ok(item) => String::from_utf8(item).unwrap_or(val),
Err(_) => val,
}
};
// escape single quotes
stripped.replace('\'', "''")
nu_utils::strip_ansi_unlikely(&val).replace('\'', "''")
}
Value::List { vals: val, .. } => val
.iter()

View File

@ -67,10 +67,7 @@ impl Command for Cd {
let path_val = {
if let Some(path) = path_val {
Some(Spanned {
item: match strip_ansi_escapes::strip(&path.item) {
Ok(item) => String::from_utf8(item).unwrap_or(path.item),
Err(_) => path.item,
},
item: nu_utils::strip_ansi_string_unlikely(path.item),
span: path.span,
})
} else {

View File

@ -73,10 +73,7 @@ impl Command for Cp {
let src: Spanned<String> = call.req(engine_state, stack, 0)?;
let src = {
Spanned {
item: match strip_ansi_escapes::strip(&src.item) {
Ok(item) => String::from_utf8(item).unwrap_or(src.item),
Err(_) => src.item,
},
item: nu_utils::strip_ansi_string_unlikely(src.item),
span: src.span,
}
};

View File

@ -86,10 +86,7 @@ impl Command for Ls {
let pattern_arg = {
if let Some(path) = pattern_arg {
Some(Spanned {
item: match strip_ansi_escapes::strip(&path.item) {
Ok(item) => String::from_utf8(item).unwrap_or(path.item),
Err(_) => path.item,
},
item: nu_utils::strip_ansi_string_unlikely(path.item),
span: path.span,
})
} else {

View File

@ -66,10 +66,7 @@ impl Command for Mv {
let spanned_source: Spanned<String> = call.req(engine_state, stack, 0)?;
let spanned_source = {
Spanned {
item: match strip_ansi_escapes::strip(&spanned_source.item) {
Ok(item) => String::from_utf8(item).unwrap_or(spanned_source.item),
Err(_) => spanned_source.item,
},
item: nu_utils::strip_ansi_string_unlikely(spanned_source.item),
span: spanned_source.span,
}
};

View File

@ -53,10 +53,7 @@ impl Command for Open {
let path = {
if let Some(path_val) = path {
Some(Spanned {
item: match strip_ansi_escapes::strip(&path_val.item) {
Ok(item) => String::from_utf8(item).unwrap_or(path_val.item),
Err(_) => path_val.item,
},
item: nu_utils::strip_ansi_string_unlikely(path_val.item),
span: path_val.span,
})
} else {

View File

@ -143,10 +143,7 @@ fn rm(
for (idx, path) in targets.clone().into_iter().enumerate() {
let corrected_path = Spanned {
item: match strip_ansi_escapes::strip(&path.item) {
Ok(item) => String::from_utf8(item).unwrap_or(path.item),
Err(_) => path.item,
},
item: nu_utils::strip_ansi_string_unlikely(path.item),
span: path.span,
};
let _ = std::mem::replace(&mut targets[idx], corrected_path);

View File

@ -3,7 +3,6 @@ use nu_protocol::{
ast::Call, ast::CellPath, engine::Command, engine::EngineState, engine::Stack, Category,
Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
};
use strip_ansi_escapes::strip;
#[derive(Clone)]
pub struct SubCommand;
@ -79,15 +78,7 @@ fn operate(
fn action(input: &Value, command_span: &Span) -> Value {
match input {
Value::String { val, span } => {
let stripped_string = {
if let Ok(bytes) = strip(&val) {
String::from_utf8_lossy(&bytes).to_string()
} else {
val.to_string()
}
};
Value::string(stripped_string, *span)
Value::string(nu_utils::strip_ansi_likely(val).to_string(), *span)
}
other => {
let got = format!("value is {}, not string", other.get_type());

View File

@ -1,5 +1,3 @@
use std::borrow::Cow;
// use super::icons::{icon_for_file, iconify_style_ansi_to_nu};
use super::icons::icon_for_file;
use lscolors::Style;
@ -178,26 +176,6 @@ prints out the list properly."#
}
}
/// 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 create_grid_output(
items: Vec<(usize, String, String)>,
call: &Call,
@ -232,7 +210,7 @@ fn create_grid_output(
if header == "name" {
if color_param {
if use_grid_icons {
let no_ansi = strip_ansi(&value);
let no_ansi = nu_utils::strip_ansi_unlikely(&value);
let path = std::path::Path::new(no_ansi.as_ref());
let icon = icon_for_file(path, call.head)?;
let ls_colors_style = ls_colors.style_for_path(path);

View File

@ -1394,17 +1394,14 @@ fn render_path_name(
return None;
}
let stripped_path = match strip_ansi_escapes::strip(path) {
Ok(v) => String::from_utf8(v).unwrap_or_else(|_| path.to_owned()),
Err(_) => path.to_owned(),
};
let stripped_path = nu_utils::strip_ansi_unlikely(path);
let (style, has_metadata) = match std::fs::symlink_metadata(&stripped_path) {
let (style, has_metadata) = match std::fs::symlink_metadata(stripped_path.as_ref()) {
Ok(metadata) => (
ls_colors.style_for_path_with_metadata(&stripped_path, Some(&metadata)),
ls_colors.style_for_path_with_metadata(stripped_path.as_ref(), Some(&metadata)),
true,
),
Err(_) => (ls_colors.style_for_path(&stripped_path), false),
Err(_) => (ls_colors.style_for_path(stripped_path.as_ref()), false),
};
// clickable links don't work in remote SSH sessions
@ -1416,9 +1413,9 @@ fn render_path_name(
// .map(ToNuAnsiStyle::to_nu_ansi_style)
.unwrap_or_default();
let full_path = PathBuf::from(&stripped_path)
let full_path = PathBuf::from(stripped_path.as_ref())
.canonicalize()
.unwrap_or_else(|_| PathBuf::from(&stripped_path));
.unwrap_or_else(|_| PathBuf::from(stripped_path.as_ref()));
let full_path_link = make_clickable_link(
full_path.display().to_string(),