table as string output

This commit is contained in:
Fernando Herrera 2021-09-25 15:47:23 +01:00
parent b8d218e65b
commit cf60f72452
3 changed files with 27 additions and 13 deletions

View File

@ -117,7 +117,6 @@ impl<'call, 'contex> ExternalCommand<'call, 'contex> {
Ok(mut child) => { Ok(mut child) => {
// if there is a string or a stream, that is sent to the pipe std // if there is a string or a stream, that is sent to the pipe std
match input { match input {
Value::Nothing { span: _ } => (),
Value::String { val, span: _ } => { Value::String { val, span: _ } => {
if let Some(mut stdin_write) = child.stdin.take() { if let Some(mut stdin_write) = child.stdin.take() {
self.write_to_stdin(&mut stdin_write, val.as_bytes())? 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 // If this external is not the last expression, then its output is piped to a channel

View File

@ -65,7 +65,11 @@ fn convert_to_table(iter: impl IntoIterator<Item = Value>) -> Option<nu_table::T
let mut iter = iter.into_iter().peekable(); let mut iter = iter.into_iter().peekable();
if let Some(first) = iter.peek() { if let Some(first) = iter.peek() {
let mut headers = first.columns(); let mut headers = match first {
Value::Record { .. } => first.columns(),
_ => ["Column_0".to_string()].to_vec(),
};
headers.insert(0, "#".into()); headers.insert(0, "#".into());
let mut data = vec![]; let mut data = vec![];
@ -74,10 +78,13 @@ fn convert_to_table(iter: impl IntoIterator<Item = Value>) -> Option<nu_table::T
let mut row = vec![row_num.to_string()]; let mut row = vec![row_num.to_string()];
for header in headers.iter().skip(1) { for header in headers.iter().skip(1) {
let result = item.clone().follow_cell_path(&[PathMember::String { let result = match item {
val: header.into(), Value::Record { .. } => item.clone().follow_cell_path(&[PathMember::String {
span: Span::unknown(), val: header.into(),
}]); span: Span::unknown(),
}]),
_ => Ok(item.clone()),
};
match result { match result {
Ok(value) => row.push(value.into_string()), Ok(value) => row.push(value.into_string()),

View File

@ -4,6 +4,7 @@ use nu_command::create_default_context;
use nu_engine::eval_block; use nu_engine::eval_block;
use nu_parser::parse; use nu_parser::parse;
use nu_protocol::{ use nu_protocol::{
ast::Call,
engine::{EngineState, EvaluationContext, StateWorkingSet}, engine::{EngineState, EvaluationContext, StateWorkingSet},
Value, Value,
}; };
@ -126,7 +127,19 @@ fn main() -> Result<()> {
match eval_block(&state, &block, Value::nothing()) { match eval_block(&state, &block, Value::nothing()) {
Ok(value) => { 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) => { Err(err) => {
let engine_state = engine_state.borrow(); let engine_state = engine_state.borrow();