mirror of
https://github.com/nushell/nushell.git
synced 2025-08-21 10:41:27 +02:00
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:
committed by
GitHub
parent
b9195c2668
commit
2c4048eb43
@@ -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);
|
||||
|
@@ -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(),
|
||||
|
Reference in New Issue
Block a user