nu_command/debug/
view_span.rs1use nu_engine::command_prelude::*;
2use nu_protocol::{DataSource, PipelineMetadata};
3
4#[derive(Clone)]
5pub struct ViewSpan;
6
7impl Command for ViewSpan {
8 fn name(&self) -> &str {
9 "view span"
10 }
11
12 fn description(&self) -> &str {
13 "View the contents of a span."
14 }
15
16 fn extra_description(&self) -> &str {
17 "This command is meant for debugging purposes.\nIt allows you to view the contents of nushell spans.\nOne way to get spans is to pipe something into 'debug --raw'.\nThen you can use the Span { start, end } values as the start and end values for this command."
18 }
19
20 fn signature(&self) -> nu_protocol::Signature {
21 Signature::build("view span")
22 .input_output_types(vec![(Type::Nothing, Type::String)])
23 .required("start", SyntaxShape::Int, "Start of the span.")
24 .required("end", SyntaxShape::Int, "End of the span.")
25 .category(Category::Debug)
26 }
27
28 fn run(
29 &self,
30 engine_state: &EngineState,
31 stack: &mut Stack,
32 call: &Call,
33 _input: PipelineData,
34 ) -> Result<PipelineData, ShellError> {
35 let start_span: Spanned<usize> = call.req(engine_state, stack, 0)?;
36 let end_span: Spanned<usize> = call.req(engine_state, stack, 1)?;
37
38 let source = if start_span.item < end_span.item {
39 let bin_contents =
40 engine_state.get_span_contents(Span::new(start_span.item, end_span.item));
41 Ok(
42 Value::string(String::from_utf8_lossy(bin_contents), call.head)
43 .into_pipeline_data(),
44 )
45 } else {
46 Err(ShellError::GenericError {
47 error: "Cannot view span".to_string(),
48 msg: "this start and end does not correspond to a viewable value".to_string(),
49 span: Some(call.head),
50 help: None,
51 inner: vec![],
52 })
53 };
54
55 source.map(|x| {
56 x.set_metadata(Some(PipelineMetadata {
57 data_source: DataSource::None,
58 content_type: Some("application/x-nuscript".into()),
59 }))
60 })
61 }
62
63 fn examples(&self) -> Vec<Example> {
64 vec![Example {
65 description: "View the source of a span. 1 and 2 are just example values. Use the return of debug --raw to get the actual values",
66 example: r#"some | pipeline | or | variable | debug --raw; view span 1 2"#,
67 result: None,
68 }]
69 }
70}