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
This commit is contained in:
Antoine Stevan 2023-03-20 21:47:18 +01:00 committed by GitHub
parent 77d33766f1
commit 758351c732
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 146 additions and 39 deletions

View File

@ -244,7 +244,7 @@ fn sort_attributes(val: Value) -> Value {
fn generate_key(item: &ValueCounter) -> Result<String, ShellError> { fn generate_key(item: &ValueCounter) -> Result<String, ShellError> {
let value = sort_attributes(item.val_to_compare.clone()); //otherwise, keys could be different for Records let value = sort_attributes(item.val_to_compare.clone()); //otherwise, keys could be different for Records
value_to_string(&value, Span::unknown()) value_to_string(&value, Span::unknown(), 0, &None)
} }
fn generate_results_with_count(head: Span, uniq_values: Vec<ValueCounter>) -> Vec<Value> { fn generate_results_with_count(head: Span, uniq_values: Vec<ValueCounter>) -> Vec<Value> {

View File

@ -1,11 +1,13 @@
use core::fmt::Write; use core::fmt::Write;
use fancy_regex::Regex; use fancy_regex::Regex;
use nu_engine::get_columns; use nu_engine::get_columns;
use nu_engine::CallExt;
use nu_parser::escape_quote_string; use nu_parser::escape_quote_string;
use nu_protocol::ast::{Call, RangeInclusion}; use nu_protocol::ast::{Call, RangeInclusion};
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Type, Value, Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, SyntaxShape,
Type, Value,
}; };
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
@ -20,6 +22,23 @@ impl Command for ToNuon {
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
Signature::build("to nuon") Signature::build("to nuon")
.input_output_types(vec![(Type::Any, Type::String)]) .input_output_types(vec![(Type::Any, Type::String)])
.switch(
"raw",
"remove all of the whitespace (default behaviour and overwrites -i and -t)",
Some('r'),
)
.named(
"indent",
SyntaxShape::Number,
"specify indentation width",
Some('i'),
)
.named(
"tabs",
SyntaxShape::Number,
"specify indentation tab quantity",
Some('t'),
)
.category(Category::Experimental) .category(Category::Experimental)
} }
@ -29,28 +48,85 @@ impl Command for ToNuon {
fn run( fn run(
&self, &self,
_engine_state: &EngineState, engine_state: &EngineState,
_stack: &mut Stack, stack: &mut Stack,
call: &Call, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
Ok(Value::String { let raw = call.has_flag("raw");
val: to_nuon(call, input)?, let use_tabs = call.has_flag("tabs");
span: call.head, let use_indent = call.has_flag("indent");
let span = call.head;
let value = input.into_value(span);
let nuon_result = if raw {
value_to_string(&value, span, 0, &None)
} else if use_tabs {
let tab_count: usize = call.get_flag(engine_state, stack, "tabs")?.unwrap_or(1);
value_to_string(&value, span, 0, &Some("\t".repeat(tab_count)))
} else if use_indent {
let indent: usize = call.get_flag(engine_state, stack, "indent")?.unwrap_or(2);
value_to_string(&value, span, 0, &Some(" ".repeat(indent)))
} else {
value_to_string(&value, span, 0, &None)
};
match nuon_result {
Ok(serde_nuon_string) => Ok(Value::String {
val: serde_nuon_string,
span,
}
.into_pipeline_data()),
_ => Ok(Value::Error {
error: Box::new(ShellError::CantConvert {
to_type: "NUON".into(),
from_type: value.get_type().to_string(),
span,
help: None,
}),
}
.into_pipeline_data()),
} }
.into_pipeline_data())
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
vec![Example { vec![
description: "Outputs a nuon string representing the contents of this list", Example {
example: "[1 2 3] | to nuon", description: "Outputs a NUON string representing the contents of this list, compact by default",
result: Some(Value::test_string("[1, 2, 3]")), example: "[1 2 3] | to nuon",
}] result: Some(Value::test_string("[1, 2, 3]"))
},
Example {
description: "Outputs a NUON array of integers, with pretty indentation",
example: "[1 2 3] | to nuon --indent 2",
result: Some(Value::test_string("[\n 1,\n 2,\n 3\n]")),
},
Example {
description: "Overwrite any set option with --raw",
example: "[1 2 3] | to nuon --indent 2 --raw",
result: Some(Value::test_string("[1, 2, 3]"))
},
Example {
description: "A more complex record with multiple data types",
example: "{date: 2000-01-01, data: [1 [2 3] 4.56]} | to nuon --indent 2",
result: Some(Value::test_string("{\n date: 2000-01-01T00:00:00+00:00,\n data: [\n 1,\n [\n 2,\n 3\n ],\n 4.56\n ]\n}"))
}
]
} }
} }
pub fn value_to_string(v: &Value, span: Span) -> Result<String, ShellError> { pub fn value_to_string(
v: &Value,
span: Span,
depth: usize,
indent: &Option<String>,
) -> Result<String, ShellError> {
let (nl, sep) = get_true_separators(indent);
let idt = get_true_indentation(depth, indent);
let idt_po = get_true_indentation(depth + 1, indent);
let idt_pt = get_true_indentation(depth + 2, indent);
match v { match v {
Value::Binary { val, .. } => { Value::Binary { val, .. } => {
let mut s = String::with_capacity(2 * val.len()); let mut s = String::with_capacity(2 * val.len());
@ -125,13 +201,13 @@ pub fn value_to_string(v: &Value, span: Span) -> Result<String, ShellError> {
.iter() .iter()
.map(|string| { .map(|string| {
if needs_quotes(string) { if needs_quotes(string) {
format!("\"{string}\"") format!("{idt}\"{string}\"")
} else { } else {
string.to_string() format!("{idt}{string}")
} }
}) })
.collect(); .collect();
let headers_output = headers.join(", "); let headers_output = headers.join(&format!(",{sep}{nl}{idt_pt}"));
let mut table_output = vec![]; let mut table_output = vec![];
for val in vals { for val in vals {
@ -139,55 +215,73 @@ pub fn value_to_string(v: &Value, span: Span) -> Result<String, ShellError> {
if let Value::Record { vals, .. } = val { if let Value::Record { vals, .. } = val {
for val in vals { for val in vals {
row.push(value_to_string_without_quotes(val, span)?); row.push(value_to_string_without_quotes(
val,
span,
depth + 2,
indent,
)?);
} }
} }
table_output.push(row.join(", ")); table_output.push(row.join(&format!(",{sep}{nl}{idt_pt}")));
} }
Ok(format!( Ok(format!(
"[[{}]; [{}]]", "[{nl}{idt_po}[{nl}{idt_pt}{}{nl}{idt_po}];{sep}{nl}{idt_po}[{nl}{idt_pt}{}{nl}{idt_po}]{nl}{idt}]",
headers_output, headers_output,
table_output.join("], [") table_output.join(&format!("{nl}{idt_po}],{sep}{nl}{idt_po}[{nl}{idt_pt}"))
)) ))
} else { } else {
let mut collection = vec![]; let mut collection = vec![];
for val in vals { for val in vals {
collection.push(value_to_string_without_quotes(val, span)?); collection.push(format!(
"{idt_po}{}",
value_to_string_without_quotes(val, span, depth + 1, indent,)?
));
} }
Ok(format!("[{}]", collection.join(", "))) Ok(format!(
"[{nl}{}{nl}{idt}]",
collection.join(&format!(",{sep}{nl}"))
))
} }
} }
Value::Nothing { .. } => Ok("null".to_string()), Value::Nothing { .. } => Ok("null".to_string()),
Value::Range { val, .. } => Ok(format!( Value::Range { val, .. } => Ok(format!(
"{}..{}{}", "{}..{}{}",
value_to_string(&val.from, span)?, value_to_string(&val.from, span, depth + 1, indent)?,
if val.inclusion == RangeInclusion::RightExclusive { if val.inclusion == RangeInclusion::RightExclusive {
"<" "<"
} else { } else {
"" ""
}, },
value_to_string(&val.to, span)? value_to_string(&val.to, span, depth + 1, indent)?
)), )),
Value::Record { cols, vals, .. } => { Value::Record { cols, vals, .. } => {
let mut collection = vec![]; let mut collection = vec![];
for (col, val) in cols.iter().zip(vals) { for (col, val) in cols.iter().zip(vals) {
collection.push(if needs_quotes(col) { collection.push(if needs_quotes(col) {
format!( format!(
"\"{}\": {}", "{idt_po}\"{}\": {}",
col, col,
value_to_string_without_quotes(val, span)? value_to_string_without_quotes(val, span, depth + 1, indent)?
) )
} else { } else {
format!("{}: {}", col, value_to_string_without_quotes(val, span)?) format!(
"{idt_po}{}: {}",
col,
value_to_string_without_quotes(val, span, depth + 1, indent)?
)
}); });
} }
Ok(format!("{{{}}}", collection.join(", "))) Ok(format!(
"{{{nl}{}{nl}{idt}}}",
collection.join(&format!(",{sep}{nl}"))
))
} }
Value::LazyRecord { val, .. } => { Value::LazyRecord { val, .. } => {
let collected = val.collect()?; let collected = val.collect()?;
value_to_string(&collected, span) value_to_string(&collected, span, depth + 1, indent)
} }
// All strings outside data structures are quoted because they are in 'command position' // All strings outside data structures are quoted because they are in 'command position'
// (could be mistaken for commands by the Nu parser) // (could be mistaken for commands by the Nu parser)
@ -195,7 +289,26 @@ pub fn value_to_string(v: &Value, span: Span) -> Result<String, ShellError> {
} }
} }
fn value_to_string_without_quotes(v: &Value, span: Span) -> Result<String, ShellError> { fn get_true_indentation(depth: usize, indent: &Option<String>) -> String {
match indent {
Some(i) => i.repeat(depth),
None => "".to_string(),
}
}
fn get_true_separators(indent: &Option<String>) -> (String, String) {
match indent {
Some(_) => ("\n".to_string(), "".to_string()),
None => ("".to_string(), " ".to_string()),
}
}
fn value_to_string_without_quotes(
v: &Value,
span: Span,
depth: usize,
indent: &Option<String>,
) -> Result<String, ShellError> {
match v { match v {
Value::String { val, .. } => Ok({ Value::String { val, .. } => Ok({
if needs_quotes(val) { if needs_quotes(val) {
@ -204,16 +317,10 @@ fn value_to_string_without_quotes(v: &Value, span: Span) -> Result<String, Shell
val.clone() val.clone()
} }
}), }),
_ => value_to_string(v, span), _ => value_to_string(v, span, depth, indent),
} }
} }
fn to_nuon(call: &Call, input: PipelineData) -> Result<String, ShellError> {
let v = input.into_value(call.head);
value_to_string(&v, call.head)
}
// This hits, in order: // This hits, in order:
// • Any character of []:`{}#'";()|$, // • Any character of []:`{}#'";()|$,
// • Any digit (\d) // • Any digit (\d)

View File

@ -295,7 +295,7 @@ fn to_nuon_errs_on_closure() {
"# "#
)); ));
assert!(actual.err.contains("not nuon-compatible")); assert!(actual.err.contains("can't convert closure to NUON"));
} }
#[test] #[test]