mirror of
https://github.com/nushell/nushell.git
synced 2024-11-25 18:03:51 +01:00
Fix to-csv command (#1635)
* Fix --headerless option of to-csv and to-tsv Before to-csv --headerless split the "headerfull" output on lines, skipped the first, and then concatenated them together. That meant that all remaining output would be put on the same line, without any delimiter, making it unusable. Now we replace the range of the first line with the empty string, maintaining the rest of the output unchanged. * Remove extra space in indentation of to_delimited_data * Add --separator <string> argument to to-csv This functionaliy has been present before, but wasn't exposed to the command.
This commit is contained in:
parent
88f06c81b2
commit
b3c623396f
@ -2,7 +2,7 @@ use crate::commands::to_delimited_data::to_delimited_data;
|
|||||||
use crate::commands::WholeStreamCommand;
|
use crate::commands::WholeStreamCommand;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use nu_errors::ShellError;
|
use nu_errors::ShellError;
|
||||||
use nu_protocol::{Primitive, Signature, UntaggedValue, Value};
|
use nu_protocol::{Primitive, Signature, SyntaxShape, UntaggedValue, Value};
|
||||||
|
|
||||||
pub struct ToCSV;
|
pub struct ToCSV;
|
||||||
|
|
||||||
@ -18,11 +18,18 @@ impl WholeStreamCommand for ToCSV {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build("to-csv").switch(
|
Signature::build("to-csv")
|
||||||
"headerless",
|
.named(
|
||||||
"do not output the columns names as the first row",
|
"separator",
|
||||||
None,
|
SyntaxShape::String,
|
||||||
)
|
"a character to separate columns, defaults to ','",
|
||||||
|
Some('s'),
|
||||||
|
)
|
||||||
|
.switch(
|
||||||
|
"headerless",
|
||||||
|
"do not output the columns names as the first row",
|
||||||
|
None,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn usage(&self) -> &str {
|
fn usage(&self) -> &str {
|
||||||
|
@ -175,40 +175,41 @@ pub fn to_delimited_data(
|
|||||||
let name_span = name_tag.span;
|
let name_span = name_tag.span;
|
||||||
|
|
||||||
let stream = async_stream! {
|
let stream = async_stream! {
|
||||||
let input: Vec<Value> = input.collect().await;
|
let input: Vec<Value> = input.collect().await;
|
||||||
|
|
||||||
let to_process_input = if input.len() > 1 {
|
let to_process_input = if input.len() > 1 {
|
||||||
let tag = input[0].tag.clone();
|
let tag = input[0].tag.clone();
|
||||||
vec![Value { value: UntaggedValue::Table(input), tag } ]
|
vec![Value { value: UntaggedValue::Table(input), tag } ]
|
||||||
} else if input.len() == 1 {
|
} else if input.len() == 1 {
|
||||||
input
|
input
|
||||||
} else {
|
} else {
|
||||||
vec![]
|
vec![]
|
||||||
};
|
};
|
||||||
|
|
||||||
for value in to_process_input {
|
for value in to_process_input {
|
||||||
match from_value_to_delimited_string(&clone_tagged_value(&value), sep) {
|
match from_value_to_delimited_string(&clone_tagged_value(&value), sep) {
|
||||||
Ok(x) => {
|
Ok(mut x) => {
|
||||||
let converted = if headerless {
|
if headerless {
|
||||||
x.lines().skip(1).collect()
|
x.find('\n').map(|second_line|{
|
||||||
} else {
|
let start = second_line + 1;
|
||||||
x
|
x.replace_range(0..start, "");
|
||||||
};
|
});
|
||||||
yield ReturnSuccess::value(UntaggedValue::Primitive(Primitive::String(converted)).into_value(&name_tag))
|
}
|
||||||
}
|
yield ReturnSuccess::value(UntaggedValue::Primitive(Primitive::String(x)).into_value(&name_tag))
|
||||||
Err(x) => {
|
}
|
||||||
let expected = format!("Expected a table with {}-compatible structure from pipeline", format_name);
|
Err(x) => {
|
||||||
let requires = format!("requires {}-compatible input", format_name);
|
let expected = format!("Expected a table with {}-compatible structure from pipeline", format_name);
|
||||||
yield Err(ShellError::labeled_error_with_secondary(
|
let requires = format!("requires {}-compatible input", format_name);
|
||||||
expected,
|
yield Err(ShellError::labeled_error_with_secondary(
|
||||||
requires,
|
expected,
|
||||||
name_span,
|
requires,
|
||||||
"originates from here".to_string(),
|
name_span,
|
||||||
value.tag.span,
|
"originates from here".to_string(),
|
||||||
))
|
value.tag.span,
|
||||||
}
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(stream.to_output_stream())
|
Ok(stream.to_output_stream())
|
||||||
|
Loading…
Reference in New Issue
Block a user