Skip to content

FEATURE: add --raw. --tabs and --indent to to nuon as in to json - #8366

Merged
fdncred merged 22 commits into
nushell:mainfrom
amtoine:feature/add-raw-tabs-and-indent-options-to-to-nuon
Mar 20, 2023
Merged

fdncred merged 22 commits into
nushell:mainfrom
amtoine:feature/add-raw-tabs-and-indent-options-to-to-nuon

Conversation

@amtoine

@amtoine amtoine commented Mar 8, 2023 •

Copy link
Copy Markdown
Member

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 (indent plus one) and idt_pt (indent plus two) 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 0b4fad7

After Submitting

the to nuon page would have to be regenerated at some point due to the new tests

amtoine added 15 commits March 8, 2023 19:58
Real computations are missing and will be added later.
This commit calls the main "to nuon" function with
- no argument when `--raw` is used
- " " repeated $i$ times with the `--indent <i>`
- "\t" repeated $t$ times with the `--tabs <t>`
This function does not add a depth level to the Value it's being
given, it only
- adds or removes quotes from a String
- passes the Value to `value_to_string` again
This commit also makes `clippy` happy.
Commands used:
```
sd "to nuon\"" "to nuon --raw\"" **/*.rs
git restore crates/nu-command/src/formats/to/nuon.rs
```
and some manuals tweaks as well.
This avoid hardcoding a " " in the field separation of the NUON
output, leading in trailing whitespaces when newlines are enabled.
This is a temporary commit.
@codecov

codecov Bot commented Mar 11, 2023 •

Copy link
Copy Markdown

Codecov Report

Merging #8366 (8484f13) into main (0df847d) will increase coverage by 0.02%.
The diff coverage is 97.72%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #8366      +/-   ##
==========================================
+ Coverage   68.53%   68.56%   +0.02%     
==========================================
  Files         620      622       +2     
  Lines       99426   100631    +1205     
==========================================
+ Hits        68145    68996     +851     
- Misses      31281    31635     +354     
Impacted Files Coverage Δ
crates/nu-command/src/formats/to/nuon.rs 88.32% <97.70%> (+5.08%) ⬆️
crates/nu-command/src/filters/uniq.rs 98.22% <100.00%> (ø)

... and 92 files with indirect coverage changes

@amtoine

This comment was marked as outdated.

@amtoine

amtoine commented Mar 11, 2023 •

Copy link
Copy Markdown
Member Author

i'm starting working on the to_nuon_range problem 👌

=> ok that was quite easy: solved in 4cc61df

@amtoine

amtoine commented Mar 18, 2023

Copy link
Copy Markdown
Member Author

tests fixed in 0b4fad7 👌

amtoine added 4 commits March 18, 2023 10:03
This is to encourage the compactness of the NUON format:
- by default, NUON will be printed in `--raw` format
- adding `--indent` or `--tabs` will make NUON prettier, with the
same precedence rules as before
- adding `--raw` to any set of options will overwrite them and
make the NUON output compact
This is related to previous commit.
…sults"

This reverts commit 9159377.

With previous commit, we do not need to impose `--raw`.
This commit will make the overall diff much smaller.
@amtoine

amtoine commented Mar 18, 2023

Copy link
Copy Markdown
Member Author

in the four latest commits, i've made "raw" the default behaviour.

the motivation comes from a remark of @fdncred i found very sensible!

I think one of the big selling points of nuon is how it's minified.
[...]
I figure we'd want the --raw output as the default and then --pretty if they don't want raw, for nuon

Note
see the original discussion

in the end i did not implement a --pretty option, but rather kept the --indent and --tabs to play this role

  • to nuon will use the "raw" format, the new default
  • to nuon --indent i, to nuon --tabs t and to nuon --indent i --tabs t will use the new "pretty" format
  • to nuon --raw and any to nuon {flags} --raw will use the "raw" option, disabling all the pretty output

changelog

  • 6bc95ce makes the "raw" format the default behaviour again, disabling the pretty ones
  • 86261f5 fixes the documentation and the examples
  • a823600 reverts the --raw option change i've made to all the tests in 9159377 => this makes the overall PR change muuuch smaller

🧪 all the tests pass locally

@amtoine amtoine closed this Mar 18, 2023
@amtoine amtoine reopened this Mar 18, 2023
@amtoine
amtoine marked this pull request as ready for review March 18, 2023 12:18
@fdncred

fdncred commented Mar 20, 2023

Copy link
Copy Markdown
Contributor

Seems like a good thing. Thanks!

@fdncred
fdncred merged commit 758351c into nushell:main Mar 20, 2023
@amtoine

amtoine commented Mar 21, 2023

Copy link
Copy Markdown
Member Author

Seems like a good thing. Thanks!

yeaah 🥳

@amtoine
amtoine deleted the feature/add-raw-tabs-and-indent-options-to-to-nuon branch March 21, 2023 16:58
bobhy pushed a commit to bobhy/nushell that referenced this pull request Oct 22, 2023
* Release notes for `0.78`

Please add your new features and breaking changes to the release notes
by opening PRs against the `release-notes-0.78` branch.

* Document optional cell path members

* Add placeholders

* Mention empty list changes

* Mention comparison operator breaking change

* add a note about better `error make` (nushell#849)

* Update 2023-04-04-nushell_0_78.md

* Update 2023-04-04-nushell_0_78.md

* Update 2023-04-04-nushell_0_78.md

* Update 2023-04-04-nushell_0_78.md

* Update 2023-04-04-nushell_0_78.md

* Update 2023-04-04-nushell_0_78.md

* Mention added `math exp`

* Update 2023-04-04-nushell_0_78.md

* Expand alias changes

* add nushell#8366 to the release notes (nushell#848)

* add nushell#8366 to the release note

* make the title more explicit for the reader

* be more explicit about what "pretty format" means

* fix nuon quoting

* Update 2023-04-04-nushell_0_78.md

---------

Co-authored-by: Reilly Wood <[email protected]>
Co-authored-by: Antoine Stevan <[email protected]>
Co-authored-by: JT <[email protected]>
Co-authored-by: Jakub Žádník <[email protected]>
sholderbach added a commit to sholderbach/nushell that referenced this pull request Apr 20, 2024
This was introduced as nushell#8366 introduced a logic to have a priority of
those flags (and have the `--raw` flag purely to match the flags on `to
json`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add whitespace pretty-printing to to nuon

2 participants