2019-06-07 07:50:26 +00:00
|
|
|
use crate::commands::command::SinkCommandArgs;
|
|
|
|
use crate::errors::ShellError;
|
2019-07-04 22:17:18 +00:00
|
|
|
use crate::format::GenericView;
|
2019-06-07 07:50:26 +00:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
|
|
|
pub fn autoview(args: SinkCommandArgs) -> Result<(), ShellError> {
|
|
|
|
if args.input.len() > 0 {
|
2019-07-13 02:07:06 +00:00
|
|
|
if let Spanned {
|
|
|
|
item: Value::Binary(_),
|
|
|
|
..
|
|
|
|
} = args.input[0]
|
|
|
|
{
|
2019-07-04 05:23:05 +00:00
|
|
|
args.ctx.get_sink("binaryview").run(args)?;
|
2019-07-20 01:12:04 +00:00
|
|
|
} else if is_single_text_value(&args.input) {
|
2019-07-24 17:14:30 +00:00
|
|
|
args.ctx.get_sink("textview").run(args)?;
|
2019-07-04 05:11:56 +00:00
|
|
|
} else if equal_shapes(&args.input) {
|
|
|
|
args.ctx.get_sink("table").run(args)?;
|
2019-06-07 07:50:26 +00:00
|
|
|
} else {
|
|
|
|
let mut host = args.ctx.host.lock().unwrap();
|
|
|
|
for i in args.input.iter() {
|
|
|
|
let view = GenericView::new(&i);
|
|
|
|
handle_unexpected(&mut *host, |host| crate::format::print_view(&view, host));
|
|
|
|
host.stdout("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-07-08 16:44:53 +00:00
|
|
|
fn equal_shapes(input: &Vec<Spanned<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
|
|
|
|
|
|
|
fn is_single_text_value(input: &Vec<Spanned<Value>>) -> bool {
|
|
|
|
if input.len() != 1 {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if let Spanned {
|
|
|
|
item: Value::Primitive(Primitive::String(_)),
|
|
|
|
..
|
|
|
|
} = input[0]
|
|
|
|
{
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|