nushell/src/commands/table.rs

61 lines
1.6 KiB
Rust
Raw Normal View History

2019-08-15 07:02:02 +02:00
use crate::commands::WholeStreamCommand;
2019-06-21 06:20:06 +02:00
use crate::errors::ShellError;
use crate::format::TableView;
use crate::prelude::*;
2019-08-03 04:17:28 +02:00
pub struct Table;
2019-08-15 07:02:02 +02:00
impl WholeStreamCommand for Table {
2019-08-03 04:17:28 +02:00
fn name(&self) -> &str {
"table"
}
fn signature(&self) -> Signature {
Signature::build("table").named("start_number", SyntaxShape::Number)
}
fn usage(&self) -> &str {
"View the contents of the pipeline as a table."
}
2019-08-03 04:17:28 +02:00
fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
table(args, registry)
2019-06-21 06:20:06 +02:00
}
2019-08-03 04:17:28 +02:00
}
fn table(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
let args = args.evaluate_once(registry)?;
2019-09-28 02:05:18 +02:00
let stream = async_stream! {
let host = args.host.clone();
let start_number = match args.get("start_number") {
Some(Tagged { item: Value::Primitive(Primitive::Int(i)), .. }) => {
i.to_usize().unwrap()
}
_ => {
0
}
};
let input: Vec<Tagged<Value>> = args.input.into_vec().await;
2019-08-03 04:17:28 +02:00
if input.len() > 0 {
let mut host = host.lock().unwrap();
let view = TableView::from_list(&input, start_number);
2019-08-03 04:17:28 +02:00
if let Some(view) = view {
handle_unexpected(&mut *host, |host| crate::format::print_view(&view, host));
}
}
2019-09-28 02:05:18 +02:00
// Needed for async_stream to type check
if false {
yield ReturnSuccess::value(Value::nothing().tagged_unknown());
}
2019-08-03 04:17:28 +02:00
};
2019-06-21 06:20:06 +02:00
2019-08-03 04:17:28 +02:00
Ok(OutputStream::new(stream))
2019-06-21 06:20:06 +02:00
}