From 2a9226a55ca2c0fb947c8b3c776864f938e3ca0e Mon Sep 17 00:00:00 2001 From: WindSoilder Date: Sat, 28 Jan 2023 22:11:21 +0800 Subject: [PATCH] refactor: use `input_handler::operate` in ansi_strip command (#7888) # Description While investigating `do --ignore-errors` issue, just found that `ansi_strip` command using a custom `operate` function(which is not needed), this pr is just a refactor # User-Facing Changes _(List of all changes that impact the user experience here. This helps us keep track of breaking changes.)_ # Tests + Formatting Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. --- crates/nu-command/src/platform/ansi/strip.rs | 41 ++++---------------- 1 file changed, 7 insertions(+), 34 deletions(-) diff --git a/crates/nu-command/src/platform/ansi/strip.rs b/crates/nu-command/src/platform/ansi/strip.rs index 9f903f8558..d08cbef802 100644 --- a/crates/nu-command/src/platform/ansi/strip.rs +++ b/crates/nu-command/src/platform/ansi/strip.rs @@ -1,3 +1,4 @@ +use crate::input_handler::{operate, CellPathOnlyArgs}; use nu_engine::CallExt; use nu_protocol::{ ast::Call, ast::CellPath, engine::Command, engine::EngineState, engine::Stack, Category, @@ -34,7 +35,9 @@ impl Command for SubCommand { call: &Call, input: PipelineData, ) -> Result { - operate(engine_state, stack, call, input) + 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()) } fn examples(&self) -> Vec { @@ -46,37 +49,7 @@ impl Command for SubCommand { } } -fn operate( - engine_state: &EngineState, - stack: &mut Stack, - call: &Call, - input: PipelineData, -) -> Result { - let column_paths: Vec = call.rest(engine_state, stack, 0)?; - let head = call.head; - input.map( - move |v| { - if column_paths.is_empty() { - action(&v, &head) - } else { - let mut ret = v; - - for path in &column_paths { - let r = ret - .update_cell_path(&path.members, Box::new(move |old| action(old, &head))); - if let Err(error) = r { - return Value::Error { error }; - } - } - - ret - } - }, - engine_state.ctrlc.clone(), - ) -} - -fn action(input: &Value, command_span: &Span) -> Value { +fn action(input: &Value, _args: &CellPathOnlyArgs, command_span: Span) -> Value { match input { Value::String { val, span } => { Value::string(nu_utils::strip_ansi_likely(val).to_string(), *span) @@ -85,7 +58,7 @@ fn action(input: &Value, command_span: &Span) -> Value { let got = format!("value is {}, not string", other.get_type()); Value::Error { - error: ShellError::TypeMismatch(got, other.span().unwrap_or(*command_span)), + error: ShellError::TypeMismatch(got, other.span().unwrap_or(command_span)), } } } @@ -109,7 +82,7 @@ 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, &Span::test_data()); + let actual = action(&input_string, &vec![].into(), Span::test_data()); assert_eq!(actual, expected); } }