From cf60f72452faf72125f292d1487e23cc743f21d1 Mon Sep 17 00:00:00 2001 From: Fernando Herrera Date: Sat, 25 Sep 2021 15:47:23 +0100 Subject: [PATCH] table as string output --- crates/nu-command/src/run_external.rs | 8 +------- crates/nu-command/src/table.rs | 17 ++++++++++++----- src/main.rs | 15 ++++++++++++++- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/crates/nu-command/src/run_external.rs b/crates/nu-command/src/run_external.rs index 3ed506176b..8d03b4a7d3 100644 --- a/crates/nu-command/src/run_external.rs +++ b/crates/nu-command/src/run_external.rs @@ -117,7 +117,6 @@ impl<'call, 'contex> ExternalCommand<'call, 'contex> { Ok(mut child) => { // if there is a string or a stream, that is sent to the pipe std match input { - Value::Nothing { span: _ } => (), Value::String { val, span: _ } => { if let Some(mut stdin_write) = child.stdin.take() { self.write_to_stdin(&mut stdin_write, val.as_bytes())? @@ -143,12 +142,7 @@ impl<'call, 'contex> ExternalCommand<'call, 'contex> { } } } - _ => { - return Err(ShellError::ExternalCommand( - "Input is not string or binary".to_string(), - self.name.span, - )) - } + _ => (), } // If this external is not the last expression, then its output is piped to a channel diff --git a/crates/nu-command/src/table.rs b/crates/nu-command/src/table.rs index 45109cec3f..f9402872bd 100644 --- a/crates/nu-command/src/table.rs +++ b/crates/nu-command/src/table.rs @@ -65,7 +65,11 @@ fn convert_to_table(iter: impl IntoIterator) -> Option first.columns(), + _ => ["Column_0".to_string()].to_vec(), + }; + headers.insert(0, "#".into()); let mut data = vec![]; @@ -74,10 +78,13 @@ fn convert_to_table(iter: impl IntoIterator) -> Option item.clone().follow_cell_path(&[PathMember::String { + val: header.into(), + span: Span::unknown(), + }]), + _ => Ok(item.clone()), + }; match result { Ok(value) => row.push(value.into_string()), diff --git a/src/main.rs b/src/main.rs index 8dc8d7c6e1..1586a21a68 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ use nu_command::create_default_context; use nu_engine::eval_block; use nu_parser::parse; use nu_protocol::{ + ast::Call, engine::{EngineState, EvaluationContext, StateWorkingSet}, Value, }; @@ -126,7 +127,19 @@ fn main() -> Result<()> { match eval_block(&state, &block, Value::nothing()) { Ok(value) => { - println!("{}", value.into_string()); + // If the table function is in the declarations, then we can use it + // to create the table value that will be printed in the terminal + let engine_state = engine_state.borrow(); + let output = match engine_state.find_decl("table".as_bytes()) { + Some(decl_id) => { + let command = engine_state.get_decl(decl_id); + + let table = command.run(&state, &Call::new(), value)?; + table.into_string() + } + None => value.into_string(), + }; + println!("{}", output); } Err(err) => { let engine_state = engine_state.borrow();