2019-08-03 02:17:28 +00:00
|
|
|
use crate::commands::{RawCommandArgs, StaticCommand};
|
2019-06-07 07:50:26 +00:00
|
|
|
use crate::errors::ShellError;
|
|
|
|
use crate::prelude::*;
|
|
|
|
|
2019-08-02 19:15:07 +00:00
|
|
|
pub struct Autoview;
|
|
|
|
|
2019-08-03 02:17:28 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct AutoviewArgs {}
|
|
|
|
|
2019-08-02 19:15:07 +00:00
|
|
|
impl StaticCommand for Autoview {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"autoview"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2019-08-03 02:17:28 +00:00
|
|
|
args.process_raw(registry, autoview)?.run()
|
2019-08-02 19:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2019-08-03 02:17:28 +00:00
|
|
|
Signature::build("autoview")
|
2019-08-02 19:15:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-03 02:17:28 +00:00
|
|
|
pub fn autoview(
|
|
|
|
AutoviewArgs {}: AutoviewArgs,
|
|
|
|
mut context: RunnableContext,
|
|
|
|
raw: RawCommandArgs,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2019-08-06 16:26:33 +00:00
|
|
|
Ok(OutputStream::new(async_stream_block! {
|
2019-08-03 02:17:28 +00:00
|
|
|
let input = context.input.drain_vec().await;
|
|
|
|
|
|
|
|
if input.len() > 0 {
|
2019-08-09 04:51:21 +00:00
|
|
|
if let Tagged {
|
2019-08-03 02:17:28 +00:00
|
|
|
item: Value::Binary(_),
|
|
|
|
..
|
2019-08-09 04:51:21 +00:00
|
|
|
} = input[0usize]
|
2019-08-03 02:17:28 +00:00
|
|
|
{
|
|
|
|
let binary = context.expect_command("binaryview");
|
2019-08-09 07:54:21 +00:00
|
|
|
let result = binary.run(raw.with_input(input), &context.commands).await.unwrap();
|
|
|
|
result.collect::<Vec<_>>().await;
|
2019-08-03 02:17:28 +00:00
|
|
|
} else if is_single_text_value(&input) {
|
2019-08-09 07:54:21 +00: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 02:17:28 +00:00
|
|
|
} else if equal_shapes(&input) {
|
|
|
|
let table = context.expect_command("table");
|
2019-08-08 04:57:38 +00:00
|
|
|
let result = table.run(raw.with_input(input), &context.commands).await.unwrap();
|
|
|
|
result.collect::<Vec<_>>().await;
|
2019-08-03 02:17:28 +00:00
|
|
|
} else {
|
|
|
|
println!("TODO!")
|
|
|
|
// TODO
|
|
|
|
// let mut host = context.host.lock().unwrap();
|
|
|
|
// for i in input.iter() {
|
|
|
|
// let view = GenericView::new(&i);
|
|
|
|
// handle_unexpected(&mut *host, |host| crate::format::print_view(&view, host));
|
|
|
|
// host.stdout("");
|
|
|
|
// }
|
2019-06-07 07:50:26 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-06 16:26:33 +00:00
|
|
|
}))
|
2019-06-07 07:50:26 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 01:58:42 +00:00
|
|
|
fn equal_shapes(input: &Vec<Tagged<Value>>) -> bool {
|
2019-06-07 07:50:26 +00: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-07-20 01:12:04 +00:00
|
|
|
|
2019-08-01 01:58:42 +00:00
|
|
|
fn is_single_text_value(input: &Vec<Tagged<Value>>) -> bool {
|
2019-07-20 01:12:04 +00:00
|
|
|
if input.len() != 1 {
|
|
|
|
return false;
|
|
|
|
}
|
2019-08-01 01:58:42 +00:00
|
|
|
if let Tagged {
|
2019-07-20 01:12:04 +00:00
|
|
|
item: Value::Primitive(Primitive::String(_)),
|
|
|
|
..
|
|
|
|
} = input[0]
|
|
|
|
{
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|