nushell/src/commands/autoview.rs

95 lines
2.5 KiB
Rust
Raw Normal View History

2019-08-15 07:02:02 +02:00
use crate::commands::{RawCommandArgs, WholeStreamCommand};
2019-06-07 09:50:26 +02:00
use crate::errors::ShellError;
use crate::prelude::*;
2019-08-02 21:15:07 +02:00
pub struct Autoview;
2019-08-03 04:17:28 +02:00
#[derive(Deserialize)]
pub struct AutoviewArgs {}
2019-08-15 07:02:02 +02:00
impl WholeStreamCommand for Autoview {
2019-08-02 21:15:07 +02:00
fn name(&self) -> &str {
"autoview"
}
fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
2019-08-03 04:17:28 +02:00
args.process_raw(registry, autoview)?.run()
2019-08-02 21:15:07 +02:00
}
fn signature(&self) -> Signature {
2019-08-03 04:17:28 +02:00
Signature::build("autoview")
2019-08-02 21:15:07 +02:00
}
}
2019-08-03 04:17:28 +02:00
pub fn autoview(
AutoviewArgs {}: AutoviewArgs,
mut context: RunnableContext,
raw: RawCommandArgs,
) -> Result<OutputStream, ShellError> {
2019-08-06 18:26:33 +02:00
Ok(OutputStream::new(async_stream_block! {
2019-08-03 04:17:28 +02:00
let input = context.input.drain_vec().await;
if input.len() > 0 {
2019-08-09 06:51:21 +02:00
if let Tagged {
2019-08-03 04:17:28 +02:00
item: Value::Binary(_),
..
2019-08-09 06:51:21 +02:00
} = input[0usize]
2019-08-03 04:17:28 +02:00
{
let binary = context.expect_command("binaryview");
2019-08-09 09:54:21 +02:00
let result = binary.run(raw.with_input(input), &context.commands).await.unwrap();
result.collect::<Vec<_>>().await;
2019-08-03 04:17:28 +02:00
} else if is_single_text_value(&input) {
2019-08-09 09:54:21 +02:00
let text = context.expect_command("textview");
let result = text.run(raw.with_input(input), &context.commands).await.unwrap();
result.collect::<Vec<_>>().await;
2019-08-03 04:17:28 +02:00
} else if equal_shapes(&input) {
let table = context.expect_command("table");
2019-08-08 06:57:38 +02:00
let result = table.run(raw.with_input(input), &context.commands).await.unwrap();
result.collect::<Vec<_>>().await;
2019-08-03 04:17:28 +02:00
} else {
2019-08-10 22:18:14 +02:00
let table = context.expect_command("table");
let result = table.run(raw.with_input(input), &context.commands).await.unwrap();
result.collect::<Vec<_>>().await;
2019-06-07 09:50:26 +02:00
}
}
2019-08-06 18:26:33 +02:00
}))
2019-06-07 09:50:26 +02:00
}
2019-08-01 03:58:42 +02:00
fn equal_shapes(input: &Vec<Tagged<Value>>) -> bool {
2019-06-07 09:50:26 +02:00
let mut items = input.iter();
let item = match items.next() {
Some(item) => item,
None => return false,
};
let desc = item.data_descriptors();
for item in items {
if desc != item.data_descriptors() {
return false;
}
}
true
}
2019-08-01 03:58:42 +02:00
fn is_single_text_value(input: &Vec<Tagged<Value>>) -> bool {
if input.len() != 1 {
return false;
}
2019-08-01 03:58:42 +02:00
if let Tagged {
item: Value::Primitive(Primitive::String(_)),
..
} = input[0]
{
true
} else {
false
}
}