allow ansi strip to work better on other nushell values (#11781)

# Description

This PR help `ansi strip` work on more nushell values. It does this by
converting values like filesize and dates to strings. This may not be
precisely correct but I think it does more what the user expects.

### Before

![image](https://github.com/nushell/nushell/assets/343840/768ffbb2-e3d7-424e-8e3b-1d20c9aa7d91)


### After

![image](https://github.com/nushell/nushell/assets/343840/6141aebb-481f-45a9-9cb7-084ca9ca1ea5)


# 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` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# 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.
-->
This commit is contained in:
Darren Schroeder 2024-02-07 16:28:40 -06:00 committed by GitHub
parent c79432f33c
commit b6b36e00c6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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_engine::CallExt;
use nu_protocol::{ use nu_protocol::{
ast::Call, ast::CellPath, engine::Command, engine::EngineState, engine::Stack, Category, ast::{Call, CellPath},
Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value, engine::{Command, EngineState, Stack},
Category, Config, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
}; };
pub struct Arguments {
cell_paths: Option<Vec<CellPath>>,
config: Config,
}
impl CmdArgument for Arguments {
fn take_cell_paths(&mut self) -> Option<Vec<CellPath>> {
self.cell_paths.take()
}
}
#[derive(Clone)] #[derive(Clone)]
pub struct SubCommand; pub struct SubCommand;
@ -41,9 +53,14 @@ impl Command for SubCommand {
call: &Call, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 0)?; let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 1)?;
let arg = CellPathOnlyArgs::from(cell_paths); let cell_paths = (!cell_paths.is_empty()).then_some(cell_paths);
operate(action, arg, input, call.head, engine_state.ctrlc.clone()) 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<Example> { fn examples(&self) -> Vec<Example> {
@ -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(); let span = input.span();
match input { match input {
Value::String { val, .. } => { Value::String { val, .. } => {
Value::string(nu_utils::strip_ansi_likely(val).to_string(), span) Value::string(nu_utils::strip_ansi_likely(val).to_string(), span)
} }
other => { other => {
let got = format!("value is {}, not string", other.get_type()); // Fake stripping ansi for other types and just show the abbreviated string
// instead of showing an error message
Value::error( Value::string(other.into_abbreviated_string(&args.config), span)
ShellError::TypeMismatch {
err_message: got,
span: other.span(),
},
other.span(),
)
} }
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{action, SubCommand}; use super::{action, Arguments, SubCommand};
use nu_protocol::{Span, Value}; use nu_protocol::{engine::EngineState, Span, Value};
#[test] #[test]
fn examples_work_as_expected() { 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"); 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 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); assert_eq!(actual, expected);
} }
} }