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

@ -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());