nushell/crates/nu-command/src/platform/ansi/strip.rs
JT 6cdfee3573
Move Value to helpers, separate span call (#10121)
# Description

As part of the refactor to split spans off of Value, this moves to using
helper functions to create values, and using `.span()` instead of
matching span out of Value directly.

Hoping to get a few more helping hands to finish this, as there are a
lot of commands to update :)

# 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.
-->

---------

Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
Co-authored-by: WindSoilder <windsoilder@outlook.com>
2023-09-03 07:27:29 -07:00

100 lines
3.0 KiB
Rust

use nu_cmd_base::input_handler::{operate, CellPathOnlyArgs};
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,
};
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"ansi strip"
}
fn signature(&self) -> Signature {
Signature::build("ansi strip")
.input_output_types(vec![
(Type::String, Type::String),
(Type::List(Box::new(Type::String)), Type::List(Box::new(Type::String))),
(Type::Table(vec![]), Type::Table(vec![])),
(Type::Record(vec![]), Type::Record(vec![])),
])
.rest(
"cell path",
SyntaxShape::CellPath,
"for a data structure input, remove ANSI sequences from strings at the given cell paths",
)
.allow_variants_without_examples(true)
.category(Category::Platform)
}
fn usage(&self) -> &str {
"Strip ANSI escape sequences from a string."
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
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())
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Strip ANSI escape sequences from a string",
example: r#"$'(ansi green)(ansi cursor_on)hello' | ansi strip"#,
result: Some(Value::test_string("hello")),
}]
}
}
fn action(input: &Value, _args: &CellPathOnlyArgs, _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(),
)
}
}
}
#[cfg(test)]
mod tests {
use super::{action, SubCommand};
use nu_protocol::{Span, Value};
#[test]
fn examples_work_as_expected() {
use crate::test_examples;
test_examples(SubCommand {})
}
#[test]
fn test_stripping() {
let input_string =
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());
assert_eq!(actual, expected);
}
}