nushell/crates/nu-command/src
Antoine Stevan 758351c732
FEATURE: add --raw. --tabs and --indent to to nuon as in to json (#8366)
Should close #7255.

# Description
**TL;DR**: this PR adds `--indent <int>`, `--tabs <int>` and `--raw` to
control a bit more the `string` output of `to nuon`, as done in `to
json` already, the goal being to promote the `NUON` format through easy
to read and formatted output `.nuon` files 😋

### outside of `crates/nu-command/src/formats/to/nuon.rs`
as the signature of `value_to_string` has changed, the single call to it
outside of its module definition has been changed to use default values
=> `value_to_string(&value, Span::unknown(), 0, &None)` in
`crates/nu-command/src/filters/uniq.rs`

### changes to `ToNuon` in `crates/nu-command/src/formats/to/nuon.rs`
- the signature now features `--raw`, `--indent <int>` and `--tabs
<int>`
- the structure of the `run` method is inspired from the one in `to
json`
  - we get the values of the arguments
  - we convert the input to a usable `Value`
- depending on whether the user raised `--raw`, `--indent` or `--tabs`,
we call the conversion to `string` with different values of the
indentation, starting at depth 0
- finally, we return `Ok` or a `ShellError::CantConvert` depending on
the conversion result
- some tool functions
- `get_true_indentation` gives the full indentation => `indent` repeated
`depth` times
- `get_true_separators` gives the line and field separators => a `("\n",
"")` when using some formatting or `("", " ")` when converting as pure
string on a single line

the meat of `nuon.rs` is now the `value_to_string` recursive function:
- takes the depth and the indent string
- adds correct newlines, space separators and indentation to the output
- calls itself with the same indent string but `depth + 1` to increase
the indentation by one level
- i used the `nl`, `idt`, `idt_po` (**i**n**d**en**t** **p**lus **o**ne)
and `idt_pt` (**i**n**d**en**t** **p**lus **t**wo) to make the
`format!`s easier to read

# User-Facing Changes
users can now
- control the amount and nature of NUON string output indentation with
  - `--indent <number of " " per level>`
  - `--tabs <number of "\t" per level>` 
- use the previous behaviour of `to nuon` with the `--raw` option
- have new examples with `help to nuon`

> **Note**
> the priority order of the options is the following
> 1. `--raw`
> 2. `--tabs`
> 3. `--indent`
>
> the default is `--indent 2`

# Tests + Formatting
### new tests
- tests involving the string output of `to nuon`, i.e. tests not of the
form `... | to nuon | from nuon ...`, now use the `to nuon --raw`
command => this is the smallest change to have the tests pass, as the
new `to nuon --raw` is equivalent to the old `to nuon`
- in `crates/nu-command/src/formats/to/nuon.rs`, the previous example
has been replaced with three examples
  - `[1 2 3] | to nuon` to show the default behaviour
  - `[1 2 3] | to nuon --raw` to show the not-formatted output
- a more complex example with `{date: 2000-01-01, data: [1 [2 3] 4.56]}
| to nuon`
  - the result values have been defined and the `examples` tests pass
 
### dev
- 🟢 `cargo fmt --all`
- 🟢 `cargo clippy --workspace -- -D warnings -D
clippy::unwrap_used -A clippy::needless_collect`
- 🟢 `cargo test --workspace` ~~passes but without
`to_nuon_errs_on_closure`~~ fixed in
0b4fad7eff

# After Submitting
the `to nuon` page would have to be regenerated at some point due to the
new tests
2023-03-20 15:47:18 -05:00
..
bits Box ShellError in Value::Error (#8375) 2023-03-12 09:57:27 +01:00
bytes Box ShellError in Value::Error (#8375) 2023-03-12 09:57:27 +01:00
charting Optional members in cell paths: Attempt 2 (#8379) 2023-03-15 20:50:58 -07:00
conversions Box ShellError in Value::Error (#8375) 2023-03-12 09:57:27 +01:00
database Fix SQLite table creation sql (#8430) 2023-03-13 10:11:28 -07:00
dataframe docs: Use capital letters for CSV and JSON acronyms (#8459) 2023-03-15 21:52:13 -07:00
date Box ShellError in Value::Error (#8375) 2023-03-12 09:57:27 +01:00
debug Restrict closure expression to be something like {|| ...} (#8290) 2023-03-17 07:36:28 -05:00
deprecated Revert "Allow NU_LIBS_DIR and friends to be const" (#8501) 2023-03-17 09:33:24 -05:00
env Revert "Allow NU_LIBS_DIR and friends to be const" (#8501) 2023-03-17 09:33:24 -05:00
experimental Uniformize usage() and extra_usage() message ending for commands helper. (#8268) 2023-02-28 21:33:02 -08:00
filesystem Add rest and glob support to 'open' (#8506) 2023-03-18 08:51:39 +13:00
filters FEATURE: add --raw. --tabs and --indent to to nuon as in to json (#8366) 2023-03-20 15:47:18 -05:00
formats FEATURE: add --raw. --tabs and --indent to to nuon as in to json (#8366) 2023-03-20 15:47:18 -05:00
generators Document and critically review ShellError variants - Ep. 2 (#8326) 2023-03-06 11:31:07 +01:00
hash Box ShellError in Value::Error (#8375) 2023-03-12 09:57:27 +01:00
math Box ShellError in Value::Error (#8375) 2023-03-12 09:57:27 +01:00
misc Uniformize usage() and extra_usage() message ending for commands helper. (#8268) 2023-02-28 21:33:02 -08:00
network Box ShellError in Value::Error (#8375) 2023-03-12 09:57:27 +01:00
path Box ShellError in Value::Error (#8375) 2023-03-12 09:57:27 +01:00
platform Box ShellError in Value::Error (#8375) 2023-03-12 09:57:27 +01:00
random Document and critically review ShellError variants - Ep. 3 (#8340) 2023-03-06 18:33:09 +01:00
shells Document and critically review ShellError variants - Ep. 3 (#8340) 2023-03-06 18:33:09 +01:00
strings Add char --list example to char command docs (#8474) 2023-03-17 10:15:41 +01:00
system feat: add a command_not_found hook (#8314) 2023-03-20 17:05:22 +13:00
viewers Optional members in cell paths: Attempt 2 (#8379) 2023-03-15 20:50:58 -07:00
default_context.rs SQL-style join command for Nushell tables (#8424) 2023-03-16 16:57:20 -07:00
example_test.rs cratification: Example support (#8231) 2023-02-27 13:58:56 -08:00
hook.rs feat: add a command_not_found hook (#8314) 2023-03-20 17:05:22 +13:00
input_handler.rs Box ShellError in Value::Error (#8375) 2023-03-12 09:57:27 +01:00
lib.rs feat: add a command_not_found hook (#8314) 2023-03-20 17:05:22 +13:00
progress_bar.rs cp progress bar implementation (#8012) 2023-02-22 11:57:38 -08:00
sort_utils.rs Document and critically review ShellError variants - Ep. 3 (#8340) 2023-03-06 18:33:09 +01:00
util.rs feat: add a command_not_found hook (#8314) 2023-03-20 17:05:22 +13:00