nushell/crates/nu-command/src/debug/view_span.rs
Darren Schroeder 4c787af26d
relocate debug commands (#8071)
# Description

Now that we've landed the debug commands we were working on, let's
relocate them to an easier place to find all of them. That's what this
PR does.

The only actual code change was changing the `timeit` command to a
`Category::Debug` command. The rest is just moving things around and
hooking them up.

# User-Facing Changes



# Tests + Formatting

Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass

# After Submitting

If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2023-02-13 16:39:07 +00:00

69 lines
2.4 KiB
Rust

use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Spanned,
SyntaxShape, Type, Value,
};
#[derive(Clone)]
pub struct ViewSpan;
impl Command for ViewSpan {
fn name(&self) -> &str {
"view span"
}
fn usage(&self) -> &str {
"View the contents of a span"
}
fn extra_usage(&self) -> &str {
"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."
}
fn signature(&self) -> nu_protocol::Signature {
Signature::build("view span")
.input_output_types(vec![(Type::Nothing, Type::String)])
.required("start", SyntaxShape::Int, "start of the span")
.required("end", SyntaxShape::Int, "end of the span")
.category(Category::Debug)
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let start_span: Spanned<usize> = call.req(engine_state, stack, 0)?;
let end_span: Spanned<usize> = call.req(engine_state, stack, 1)?;
if start_span.item < end_span.item {
let bin_contents =
engine_state.get_span_contents(&Span::new(start_span.item, end_span.item));
Ok(
Value::string(String::from_utf8_lossy(bin_contents), call.head)
.into_pipeline_data(),
)
} else {
Err(ShellError::GenericError(
"Cannot view span".to_string(),
"this start and end does not correspond to a viewable value".to_string(),
Some(call.head),
None,
Vec::new(),
))
}
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "View the source of a span. 1 and 2 are just example values. Use the return of debug -r to get the actual values",
example: r#"some | pipeline | or | variable | debug -r; view span 1 2"#,
result: None,
}]
}
}