nu_command/debug/
inspect.rs

1use super::inspect_table;
2use nu_engine::command_prelude::*;
3use nu_utils::terminal_size;
4
5#[derive(Clone)]
6pub struct Inspect;
7
8impl Command for Inspect {
9    fn name(&self) -> &str {
10        "inspect"
11    }
12
13    fn description(&self) -> &str {
14        "Inspect pipeline results while running a pipeline."
15    }
16
17    fn signature(&self) -> nu_protocol::Signature {
18        Signature::build("inspect")
19            .input_output_types(vec![(Type::Any, Type::Any)])
20            .allow_variants_without_examples(true)
21            .category(Category::Debug)
22    }
23
24    fn run(
25        &self,
26        engine_state: &EngineState,
27        _stack: &mut Stack,
28        call: &Call,
29        input: PipelineData,
30    ) -> Result<PipelineData, ShellError> {
31        let input_metadata = input.metadata();
32        let input_val = input.into_value(call.head)?;
33        if input_val.is_nothing() {
34            return Err(ShellError::PipelineEmpty {
35                dst_span: call.head,
36            });
37        }
38        let original_input = input_val.clone();
39        let description = input_val.get_type().to_string();
40
41        let (cols, _rows) = terminal_size().unwrap_or((0, 0));
42
43        let table = inspect_table::build_table(engine_state, input_val, description, cols as usize);
44
45        // Note that this is printed to stderr. The reason for this is so it doesn't disrupt the regular nushell
46        // tabular output. If we printed to stdout, nushell would get confused with two outputs.
47        eprintln!("{table}\n");
48
49        Ok(original_input.into_pipeline_data_with_metadata(input_metadata))
50    }
51
52    fn examples(&self) -> Vec<Example<'_>> {
53        vec![Example {
54            description: "Inspect pipeline results",
55            example: "ls | inspect | get name | inspect",
56            result: None,
57        }]
58    }
59}