diff --git a/crates/nu-command/src/platform/ansi/strip.rs b/crates/nu-command/src/platform/ansi/strip.rs index 33f087f6d2..8f542853ec 100644 --- a/crates/nu-command/src/platform/ansi/strip.rs +++ b/crates/nu-command/src/platform/ansi/strip.rs @@ -1,10 +1,22 @@ -use nu_cmd_base::input_handler::{operate, CellPathOnlyArgs}; +use nu_cmd_base::input_handler::{operate, CmdArgument}; use nu_engine::CallExt; use nu_protocol::{ - ast::Call, ast::CellPath, engine::Command, engine::EngineState, engine::Stack, Category, - Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value, + ast::{Call, CellPath}, + engine::{Command, EngineState, Stack}, + Category, Config, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value, }; +pub struct Arguments { + cell_paths: Option>, + config: Config, +} + +impl CmdArgument for Arguments { + fn take_cell_paths(&mut self) -> Option> { + self.cell_paths.take() + } +} + #[derive(Clone)] pub struct SubCommand; @@ -41,9 +53,14 @@ impl Command for SubCommand { call: &Call, input: PipelineData, ) -> Result { - let cell_paths: Vec = call.rest(engine_state, stack, 0)?; - let arg = CellPathOnlyArgs::from(cell_paths); - operate(action, arg, input, call.head, engine_state.ctrlc.clone()) + let cell_paths: Vec = call.rest(engine_state, stack, 1)?; + let cell_paths = (!cell_paths.is_empty()).then_some(cell_paths); + let config = engine_state.get_config(); + let args = Arguments { + cell_paths, + config: config.clone(), + }; + operate(action, args, input, call.head, engine_state.ctrlc.clone()) } fn examples(&self) -> Vec { @@ -55,30 +72,24 @@ impl Command for SubCommand { } } -fn action(input: &Value, _args: &CellPathOnlyArgs, _span: Span) -> Value { +fn action(input: &Value, args: &Arguments, _span: Span) -> Value { let span = input.span(); match input { Value::String { val, .. } => { Value::string(nu_utils::strip_ansi_likely(val).to_string(), span) } other => { - let got = format!("value is {}, not string", other.get_type()); - - Value::error( - ShellError::TypeMismatch { - err_message: got, - span: other.span(), - }, - other.span(), - ) + // Fake stripping ansi for other types and just show the abbreviated string + // instead of showing an error message + Value::string(other.into_abbreviated_string(&args.config), span) } } } #[cfg(test)] mod tests { - use super::{action, SubCommand}; - use nu_protocol::{Span, Value}; + use super::{action, Arguments, SubCommand}; + use nu_protocol::{engine::EngineState, Span, Value}; #[test] fn examples_work_as_expected() { @@ -93,7 +104,12 @@ mod tests { Value::test_string("\u{1b}[3;93;41mHello\u{1b}[0m \u{1b}[1;32mNu \u{1b}[1;35mWorld"); let expected = Value::test_string("Hello Nu World"); - let actual = action(&input_string, &vec![].into(), Span::test_data()); + let args = Arguments { + cell_paths: vec![].into(), + config: EngineState::new().get_config().clone(), + }; + + let actual = action(&input_string, &args, Span::test_data()); assert_eq!(actual, expected); } }