nushell/src/commands/autoview.rs

125 lines
3.6 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::*;
use futures_async_stream::async_stream_block;
2019-06-07 09:50:26 +02:00
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 signature(&self) -> Signature {
Signature::build("autoview")
}
fn usage(&self) -> &str {
"View the contents of the pipeline as a table or list."
}
2019-08-02 21:15:07 +02:00
fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
Ok(args.process_raw(registry, autoview)?.run())
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 {
item: Value::Primitive(Primitive::Binary(_)),
2019-08-03 04:17:28 +02:00
..
2019-08-09 06:51:21 +02:00
} = input[0usize]
2019-08-03 04:17:28 +02:00
{
let binary = context.get_command("binaryview");
if let Some(binary) = binary {
2019-09-17 04:09:15 +02:00
let result = binary.run(raw.with_input(input), &context.commands, false);
result.collect::<Vec<_>>().await;
} else {
for i in input {
match i.item {
Value::Primitive(Primitive::Binary(b)) => {
use pretty_hex::*;
println!("{:?}", b.hex_dump());
}
_ => {}
}
}
};
2019-09-08 03:35:02 +02:00
} else if is_single_origined_text_value(&input) {
let text = context.get_command("textview");
if let Some(text) = text {
2019-09-17 04:09:15 +02:00
let result = text.run(raw.with_input(input), &context.commands, false);
result.collect::<Vec<_>>().await;
} else {
for i in input {
match i.item {
Value::Primitive(Primitive::String(s)) => {
println!("{}", s);
}
_ => {}
}
}
}
2019-09-08 03:35:02 +02:00
} else if is_single_text_value(&input) {
for i in input {
match i.item {
Value::Primitive(Primitive::String(s)) => {
println!("{}", s);
}
_ => {}
}
}
2019-08-03 04:17:28 +02:00
} else {
2019-08-10 22:18:14 +02:00
let table = context.expect_command("table");
2019-09-17 04:09:15 +02:00
let result = table.run(raw.with_input(input), &context.commands, false);
2019-08-10 22:18:14 +02:00
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 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
}
}
2019-09-08 03:35:02 +02:00
fn is_single_origined_text_value(input: &Vec<Tagged<Value>>) -> bool {
if input.len() != 1 {
return false;
}
2019-09-18 08:37:04 +02:00
2019-09-08 03:35:02 +02:00
if let Tagged {
item: Value::Primitive(Primitive::String(_)),
tag: Tag { origin, .. },
2019-09-08 03:35:02 +02:00
} = input[0]
{
origin != uuid::Uuid::nil()
2019-09-08 03:35:02 +02:00
} else {
false
}
}