nu_command/strings/ansi/
strip.rs

1use std::sync::Arc;
2
3use nu_cmd_base::input_handler::{CmdArgument, operate};
4use nu_engine::command_prelude::*;
5use nu_protocol::Config;
6
7struct Arguments {
8    cell_paths: Option<Vec<CellPath>>,
9    config: Arc<Config>,
10}
11
12impl CmdArgument for Arguments {
13    fn take_cell_paths(&mut self) -> Option<Vec<CellPath>> {
14        self.cell_paths.take()
15    }
16}
17
18#[derive(Clone)]
19pub struct AnsiStrip;
20
21impl Command for AnsiStrip {
22    fn name(&self) -> &str {
23        "ansi strip"
24    }
25
26    fn signature(&self) -> Signature {
27        Signature::build("ansi strip")
28            .input_output_types(vec![
29                (Type::String, Type::String),
30                (Type::List(Box::new(Type::String)), Type::List(Box::new(Type::String))),
31                (Type::table(), Type::table()),
32                (Type::record(), Type::record()),
33            ])
34            .rest(
35                "cell path",
36                SyntaxShape::CellPath,
37                "For a data structure input, remove ANSI sequences from strings at the given cell paths.",
38            )
39            .allow_variants_without_examples(true)
40            .category(Category::Platform)
41    }
42
43    fn description(&self) -> &str {
44        "Strip ANSI escape sequences from a string."
45    }
46
47    fn run(
48        &self,
49        engine_state: &EngineState,
50        stack: &mut Stack,
51        call: &Call,
52        input: PipelineData,
53    ) -> Result<PipelineData, ShellError> {
54        let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
55        let cell_paths = (!cell_paths.is_empty()).then_some(cell_paths);
56        let config = stack.get_config(engine_state);
57        let args = Arguments { cell_paths, config };
58        operate(action, args, input, call.head, engine_state.signals())
59    }
60
61    fn examples(&self) -> Vec<Example<'_>> {
62        vec![
63            Example {
64                description: "Strip ANSI escape sequences from a string",
65                example: r#"$'(ansi green)(ansi cursor_on)hello' | ansi strip"#,
66                result: Some(Value::test_string("hello")),
67            },
68            Example {
69                description: "Strip ANSI escape sequences from a record field",
70                example: r#"{ greeting: $'hello (ansi red)world' exclamation: false } | ansi strip greeting"#,
71                result: Some(Value::test_record(record! {
72                    "greeting" => Value::test_string("hello world"),
73                    "exclamation" => Value::test_bool(false)
74                })),
75            },
76            Example {
77                description: "Strip ANSI escape sequences from multiple table columns",
78                example: r#"[[language feature]; [$'(ansi red)rust' $'(ansi i)safety']] | ansi strip language feature"#,
79                result: Some(Value::test_list(vec![Value::test_record(record! {
80                    "language" => Value::test_string("rust"),
81                    "feature" => Value::test_string("safety")
82                })])),
83            },
84        ]
85    }
86}
87
88fn action(input: &Value, args: &Arguments, _span: Span) -> Value {
89    let span = input.span();
90    match input {
91        Value::String { val, .. } => {
92            Value::string(nu_utils::strip_ansi_likely(val).to_string(), span)
93        }
94        other => {
95            // Fake stripping ansi for other types and just show the abbreviated string
96            // instead of showing an error message
97            Value::string(other.to_abbreviated_string(&args.config), span)
98        }
99    }
100}
101
102#[cfg(test)]
103mod tests {
104    use super::{AnsiStrip, Arguments, action};
105    use nu_protocol::{Span, Value, engine::EngineState};
106
107    #[test]
108    fn examples_work_as_expected() {
109        use crate::test_examples;
110
111        test_examples(AnsiStrip {})
112    }
113
114    #[test]
115    fn test_stripping() {
116        let input_string =
117            Value::test_string("\u{1b}[3;93;41mHello\u{1b}[0m \u{1b}[1;32mNu \u{1b}[1;35mWorld");
118        let expected = Value::test_string("Hello Nu World");
119
120        let args = Arguments {
121            cell_paths: vec![].into(),
122            config: EngineState::new().get_config().clone(),
123        };
124
125        let actual = action(&input_string, &args, Span::test_data());
126        assert_eq!(actual, expected);
127    }
128}