nu_command/debug/
view_blocks.rs

1use nu_engine::command_prelude::*;
2
3#[derive(Clone)]
4pub struct ViewBlocks;
5
6impl Command for ViewBlocks {
7    fn name(&self) -> &str {
8        "view blocks"
9    }
10
11    fn description(&self) -> &str {
12        "View the blocks registered in nushell's EngineState memory."
13    }
14
15    fn extra_description(&self) -> &str {
16        "These are blocks parsed and loaded at runtime as well as any blocks that accumulate in the repl."
17    }
18
19    fn signature(&self) -> nu_protocol::Signature {
20        Signature::build("view blocks")
21            .input_output_types(vec![(
22                Type::Nothing,
23                Type::Table(
24                    [
25                        ("block_id".into(), Type::Int),
26                        ("content".into(), Type::String),
27                        ("start".into(), Type::Int),
28                        ("end".into(), Type::Int),
29                    ]
30                    .into(),
31                ),
32            )])
33            .category(Category::Debug)
34    }
35
36    fn run(
37        &self,
38        engine_state: &EngineState,
39        _stack: &mut Stack,
40        call: &Call,
41        _input: PipelineData,
42    ) -> Result<PipelineData, ShellError> {
43        let mut records = vec![];
44
45        for block_id in 0..engine_state.num_blocks() {
46            let block = engine_state.get_block(nu_protocol::BlockId::new(block_id));
47
48            if let Some(span) = block.span {
49                let contents_bytes = engine_state.get_span_contents(span);
50                let contents_string = String::from_utf8_lossy(contents_bytes);
51                let cur_rec = record! {
52                    "block_id" => Value::int(block_id as i64, span),
53                    "content" => Value::string(contents_string.trim().to_string(), span),
54                    "start" => Value::int(span.start as i64, span),
55                    "end" => Value::int(span.end as i64, span),
56                };
57                records.push(Value::record(cur_rec, span));
58            }
59        }
60
61        Ok(Value::list(records, call.head).into_pipeline_data())
62    }
63
64    fn examples(&self) -> Vec<Example<'_>> {
65        vec![Example {
66            description: "View the blocks registered in Nushell's EngineState memory",
67            example: r#"view blocks"#,
68            result: None,
69        }]
70    }
71}