nu_command/debug/
view_span.rs1use nu_engine::command_prelude::*;
2use nu_protocol::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 span = if start_span.item <= end_span.item {
39 Ok(Span::new(start_span.item, end_span.item))
40 } else {
41 Err(ShellError::GenericError {
42 error: "Invalid span".to_string(),
43 msg: "the start position of this span is later than the end position".to_string(),
44 span: Some(call.head),
45 help: None,
46 inner: vec![],
47 })
48 }?;
49
50 let bin_contents = engine_state
51 .try_get_file_contents(span)
52 .map(String::from_utf8_lossy)
53 .ok_or_else(|| ShellError::GenericError {
54 error: "Cannot view span".to_string(),
55 msg: "this start and end does not correspond to a viewable value".to_string(),
56 span: Some(call.head),
57 help: None,
58 inner: vec![],
59 })?;
60
61 let metadata = PipelineMetadata {
62 content_type: Some("application/x-nuscript".into()),
63 ..Default::default()
64 };
65
66 let value = Value::string(bin_contents, call.head)
67 .into_pipeline_data()
68 .set_metadata(Some(metadata));
69 Ok(value)
70 }
71
72 fn examples(&self) -> Vec<Example<'_>> {
73 vec![Example {
74 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.",
75 example: r#"some | pipeline | or | variable | debug --raw; view span 1 2"#,
76 result: None,
77 }]
78 }
79}