nushell/crates/nu-cli/src/commands/from_tsv.rs

63 lines
1.4 KiB
Rust
Raw Normal View History

use crate::commands::from_delimited_data::from_delimited_data;
2019-08-29 11:02:16 +02:00
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use nu_errors::ShellError;
use nu_protocol::Signature;
2019-08-29 11:02:16 +02:00
pub struct FromTSV;
#[derive(Deserialize)]
pub struct FromTSVArgs {
headerless: bool,
}
2020-05-29 10:22:52 +02:00
#[async_trait]
2019-08-29 11:02:16 +02:00
impl WholeStreamCommand for FromTSV {
fn name(&self) -> &str {
"from tsv"
2019-08-29 11:02:16 +02:00
}
fn signature(&self) -> Signature {
Signature::build("from tsv").switch(
"headerless",
"don't treat the first row as column names",
None,
)
2019-08-29 11:02:16 +02:00
}
fn usage(&self) -> &str {
"Parse text as .tsv and create table."
}
2020-05-29 10:22:52 +02:00
async fn run(
2019-08-29 11:02:16 +02:00
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
from_tsv(args, registry).await
2019-08-29 11:02:16 +02:00
}
}
async fn from_tsv(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let name = args.call_info.name_tag.clone();
let (FromTSVArgs { headerless }, input) = args.process(&registry).await?;
from_delimited_data(headerless, '\t', "TSV", input, name).await
2019-08-29 11:02:16 +02:00
}
#[cfg(test)]
mod tests {
use super::FromTSV;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(FromTSV {})
}
}