mirror of
https://github.com/nushell/nushell.git
synced 2024-11-25 18:03:51 +01:00
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:
parent
c79432f33c
commit
b6b36e00c6
@ -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<Vec<CellPath>>,
|
||||
config: Config,
|
||||
}
|
||||
|
||||
impl CmdArgument for Arguments {
|
||||
fn take_cell_paths(&mut self) -> Option<Vec<CellPath>> {
|
||||
self.cell_paths.take()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SubCommand;
|
||||
|
||||
@ -41,9 +53,14 @@ impl Command for SubCommand {
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let cell_paths: Vec<CellPath> = 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<CellPath> = 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<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();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user