nushell/src/commands/table.rs

71 lines
2.0 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::format::TableView;
use crate::prelude::*;
use nu_errors::ShellError;
2019-11-30 01:21:05 +01:00
use nu_protocol::{Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
2019-06-21 06:20:06 +02:00
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 {
2019-10-28 06:15:35 +01:00
Signature::build("table").named(
"start_number",
SyntaxShape::Number,
"row number to start viewing from",
)
}
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(Value { value: UntaggedValue::Primitive(Primitive::Int(i)), .. }) => {
2020-01-02 06:24:41 +01:00
if let Some(num) = i.to_usize() {
num
} else {
yield Err(ShellError::labeled_error("Expected a row number", "expected a row number", &args.args.call_info.name_tag));
0
}
}
_ => {
0
}
};
let input: Vec<Value> = args.input.into_vec().await;
2019-08-03 04:17:28 +02:00
if input.len() > 0 {
let mut host = host.lock();
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(UntaggedValue::nothing().into_value(Tag::unknown()));
2019-09-28 02:05:18 +02:00
}
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
}