mirror of
https://github.com/nushell/nushell.git
synced 2025-01-11 08:48:23 +01:00
preserve space by letting to nuon
only add quotes when necessary (#6379)
* preserve space by letting `to nuon` only add quotes when necessary * fix CI, add quotes with colon * fmt * add more chars to blacklist
This commit is contained in:
parent
d97975e9fa
commit
884382bac4
@ -104,7 +104,13 @@ fn value_to_string(v: &Value, span: Span) -> Result<String, ShellError> {
|
|||||||
// Table output
|
// Table output
|
||||||
let headers: Vec<String> = headers
|
let headers: Vec<String> = headers
|
||||||
.iter()
|
.iter()
|
||||||
.map(|string| format!("\"{}\"", string))
|
.map(|string| {
|
||||||
|
if needs_quotes(string) {
|
||||||
|
format!("\"{}\"", string)
|
||||||
|
} else {
|
||||||
|
string.to_string()
|
||||||
|
}
|
||||||
|
})
|
||||||
.collect();
|
.collect();
|
||||||
let headers_output = headers.join(", ");
|
let headers_output = headers.join(", ");
|
||||||
|
|
||||||
@ -114,7 +120,7 @@ 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(val, span)?);
|
row.push(value_to_string_without_quotes(val, span)?);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -129,7 +135,7 @@ fn value_to_string(v: &Value, span: Span) -> Result<String, ShellError> {
|
|||||||
} else {
|
} else {
|
||||||
let mut collection = vec![];
|
let mut collection = vec![];
|
||||||
for val in vals {
|
for val in vals {
|
||||||
collection.push(value_to_string(val, span)?);
|
collection.push(value_to_string_without_quotes(val, span)?);
|
||||||
}
|
}
|
||||||
Ok(format!("[{}]", collection.join(", ")))
|
Ok(format!("[{}]", collection.join(", ")))
|
||||||
}
|
}
|
||||||
@ -148,7 +154,15 @@ fn value_to_string(v: &Value, span: Span) -> Result<String, ShellError> {
|
|||||||
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(format!("\"{}\": {}", col, value_to_string(val, span)?));
|
collection.push(if needs_quotes(col) {
|
||||||
|
format!(
|
||||||
|
"\"{}\": {}",
|
||||||
|
col,
|
||||||
|
value_to_string_without_quotes(val, span)?
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
format!("{}: {}", col, value_to_string_without_quotes(val, span)?)
|
||||||
|
});
|
||||||
}
|
}
|
||||||
Ok(format!("{{{}}}", collection.join(", ")))
|
Ok(format!("{{{}}}", collection.join(", ")))
|
||||||
}
|
}
|
||||||
@ -156,12 +170,39 @@ fn value_to_string(v: &Value, span: Span) -> Result<String, ShellError> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn value_to_string_without_quotes(v: &Value, span: Span) -> Result<String, ShellError> {
|
||||||
|
match v {
|
||||||
|
Value::String { val, .. } => Ok({
|
||||||
|
let mut quoted = escape_quote_string(val);
|
||||||
|
if !needs_quotes(val) {
|
||||||
|
quoted.remove(0);
|
||||||
|
quoted.pop();
|
||||||
|
}
|
||||||
|
quoted
|
||||||
|
}),
|
||||||
|
_ => value_to_string(v, span),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn to_nuon(call: &Call, input: PipelineData) -> Result<String, ShellError> {
|
fn to_nuon(call: &Call, input: PipelineData) -> Result<String, ShellError> {
|
||||||
let v = input.into_value(call.head);
|
let v = input.into_value(call.head);
|
||||||
|
|
||||||
value_to_string(&v, call.head)
|
value_to_string(&v, call.head)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn needs_quotes(string: &str) -> bool {
|
||||||
|
string.contains(' ')
|
||||||
|
|| string.contains(',')
|
||||||
|
|| string.contains(':')
|
||||||
|
|| string.contains(';')
|
||||||
|
|| string.contains('(')
|
||||||
|
|| string.contains(')')
|
||||||
|
|| string.contains('[')
|
||||||
|
|| string.contains(']')
|
||||||
|
|| string.contains('{')
|
||||||
|
|| string.contains('}')
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -249,3 +249,21 @@ fn to_nuon_converts_columns_with_spaces() {
|
|||||||
));
|
));
|
||||||
assert!(actual.err.is_empty());
|
assert!(actual.err.is_empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn to_nuon_does_not_quote_unnecessarily() {
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: "tests/fixtures/formats", pipeline(
|
||||||
|
r#"
|
||||||
|
let test = [["a", "b", "c d"]; [1 2 3] [4 5 6]]; $test | to nuon
|
||||||
|
"#
|
||||||
|
));
|
||||||
|
assert_eq!(actual.out, "[[a, b, \"c d\"]; [1, 2, 3], [4, 5, 6]]");
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: "tests/fixtures/formats", pipeline(
|
||||||
|
r#"
|
||||||
|
let a = {"ro name": "sam" rank: 10}; $a | to nuon
|
||||||
|
"#
|
||||||
|
));
|
||||||
|
assert_eq!(actual.out, "{\"ro name\": sam, rank: 10}");
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user