nushell/crates/nu-cli/src
132ikl d69e131450
Rely on display_output hook for formatting values from evaluations (#14361)
# 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.
2024-11-19 21:04:29 +08:00
..
commands Remove as_i64 and as_f64 (#14258) 2024-11-05 09:28:56 +01:00
completions add like and not-like operators as synonyms for the regex operators =~ and !~ (#14072) 2024-10-20 23:12:57 +02:00
menus Replace raw usize IDs with new types (#13832) 2024-09-30 13:20:15 +02:00
config_files.rs Reduce duplication in history path construction (#13475) 2024-10-11 07:51:50 -05:00
eval_cmds.rs Rely on display_output hook for formatting values from evaluations (#14361) 2024-11-19 21:04:29 +08:00
eval_file.rs Rely on display_output hook for formatting values from evaluations (#14361) 2024-11-19 21:04:29 +08:00
lib.rs Set current working directory at startup (#12953) 2024-09-25 13:04:26 -05:00
nu_highlight.rs Change the usage misnomer to "description" (#13598) 2024-08-22 12:02:08 +02:00
print.rs Rely on display_output hook for formatting values from evaluations (#14361) 2024-11-19 21:04:29 +08:00
prompt.rs Make get_env_var return a reference to a Value (#13987) 2024-10-02 13:05:48 +02:00
prompt_update.rs fix ansi bleed over on right prompt (#14357) 2024-11-17 19:47:09 +08:00
reedline_config.rs Cut down unnecessary lint allows (#14335) 2024-11-15 19:24:39 +01:00
repl.rs Remove the NU_DISABLE_IR option (#14293) 2024-11-15 12:09:25 +08:00
syntax_highlight.rs Overhaul $in expressions (#13357) 2024-07-17 16:02:42 -05:00
util.rs Rely on display_output hook for formatting values from evaluations (#14361) 2024-11-19 21:04:29 +08:00
validation.rs Remove old alias implementation (#8797) 2023-04-07 21:09:38 +03:00