mirror of
https://github.com/nushell/nushell.git
synced 2025-04-25 05:38:20 +02:00
# Description I was reading through the documentation yesterday, when I stumbled upon [this section](https://www.nushell.sh/book/pipelines.html#behind-the-scenes) explaining how command output is formatted using the `table` command. I was surprised that this section didn't mention the `display_output` hook, so I took a look in the code and was shocked to discovered that the documentation was correct, and the `table` command _is_ automatically applied to printed pipelines. This auto-tabling has two ramifications for the `display_output` hook: 1. The `table` command is called on the output of a pipeline after the `display_output` has run, even if `display_output` contains the table command. This means each pipeline output is roughly equivalent to the following (using `ls` as an example): ```nushell ls | do $config.hooks.display_output | table ``` 2. If `display_output` returns structured data, it will _still_ be formatted through the table command. This PR removes the auto-table when the `display_output` hook is set. The auto-table made sense before `display_output` was introduced, but to me, it now seems like unnecessary "automagic" which can be accomplished using existing Nushell features. This means that you can now pull back the curtain a bit, and replace your `display_output` hook with an empty closure (`$env.config.hooks.display_output = {||}`, setting it to null retains the previous behavior) to see the values printed normally without the table formatting. I think this is a good thing, and makes it easier to understand Nushell fundamentals. It is important to note that this PR does not change how `print` and other commands (well, specifically only `watch`) print out values. They continue to use `table` with no arguments, so changing your config/`display_output` hook won't affect what `print`ing a value does. Rel: [Discord discussion](https://discord.com/channels/601130461678272522/615329862395101194/1307102690848931904) (cc @dcarosone) # User-Facing Changes Pipelines are no longer automatically formatted using the `table` command. Instead, the `display_output` hook is used to format pipeline output. Most users should see no impact, as the default `display_output` hook already uses the `table` command. # Tests + Formatting - 🟢 `toolkit fmt` - 🟢 `toolkit clippy` - 🟢 `toolkit test` - 🟢 `toolkit test stdlib` # After Submitting Will update mentioned docs page to call out `display_output` hook.
112 lines
3.6 KiB
Rust
112 lines
3.6 KiB
Rust
use nu_engine::command_prelude::*;
|
|
use nu_protocol::ByteStreamSource;
|
|
|
|
#[derive(Clone)]
|
|
pub struct Print;
|
|
|
|
impl Command for Print {
|
|
fn name(&self) -> &str {
|
|
"print"
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build("print")
|
|
.input_output_types(vec![
|
|
(Type::Nothing, Type::Nothing),
|
|
(Type::Any, Type::Nothing),
|
|
])
|
|
.allow_variants_without_examples(true)
|
|
.rest("rest", SyntaxShape::Any, "the values to print")
|
|
.switch(
|
|
"no-newline",
|
|
"print without inserting a newline for the line ending",
|
|
Some('n'),
|
|
)
|
|
.switch("stderr", "print to stderr instead of stdout", Some('e'))
|
|
.switch(
|
|
"raw",
|
|
"print without formatting (including binary data)",
|
|
Some('r'),
|
|
)
|
|
.category(Category::Strings)
|
|
}
|
|
|
|
fn description(&self) -> &str {
|
|
"Print the given values to stdout."
|
|
}
|
|
|
|
fn extra_description(&self) -> &str {
|
|
r#"Unlike `echo`, this command does not return any value (`print | describe` will return "nothing").
|
|
Since this command has no output, there is no point in piping it with other commands.
|
|
|
|
`print` may be used inside blocks of code (e.g.: hooks) to display text during execution without interfering with the pipeline."#
|
|
}
|
|
|
|
fn search_terms(&self) -> Vec<&str> {
|
|
vec!["display"]
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
call: &Call,
|
|
mut input: PipelineData,
|
|
) -> Result<PipelineData, ShellError> {
|
|
let args: Vec<Value> = call.rest(engine_state, stack, 0)?;
|
|
let no_newline = call.has_flag(engine_state, stack, "no-newline")?;
|
|
let to_stderr = call.has_flag(engine_state, stack, "stderr")?;
|
|
let raw = call.has_flag(engine_state, stack, "raw")?;
|
|
|
|
// This will allow for easy printing of pipelines as well
|
|
if !args.is_empty() {
|
|
for arg in args {
|
|
if raw {
|
|
arg.into_pipeline_data()
|
|
.print_raw(engine_state, no_newline, to_stderr)?;
|
|
} else {
|
|
arg.into_pipeline_data().print_table(
|
|
engine_state,
|
|
stack,
|
|
no_newline,
|
|
to_stderr,
|
|
)?;
|
|
}
|
|
}
|
|
} else if !input.is_nothing() {
|
|
if let PipelineData::ByteStream(stream, _) = &mut input {
|
|
if let ByteStreamSource::Child(child) = stream.source_mut() {
|
|
child.ignore_error(true);
|
|
}
|
|
}
|
|
if raw {
|
|
input.print_raw(engine_state, no_newline, to_stderr)?;
|
|
} else {
|
|
input.print_table(engine_state, stack, no_newline, to_stderr)?;
|
|
}
|
|
}
|
|
|
|
Ok(PipelineData::empty())
|
|
}
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![
|
|
Example {
|
|
description: "Print 'hello world'",
|
|
example: r#"print "hello world""#,
|
|
result: None,
|
|
},
|
|
Example {
|
|
description: "Print the sum of 2 and 3",
|
|
example: r#"print (2 + 3)"#,
|
|
result: None,
|
|
},
|
|
Example {
|
|
description: "Print 'ABC' from binary data",
|
|
example: r#"0x[41 42 43] | print --raw"#,
|
|
result: None,
|
|
},
|
|
]
|
|
}
|
|
}
|