From cf60f72452faf72125f292d1487e23cc743f21d1 Mon Sep 17 00:00:00 2001 From: Fernando Herrera Date: Sat, 25 Sep 2021 15:47:23 +0100 Subject: [PATCH 1/7] 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(); From b12a265f1ee1ef3b127b36c39b8f227388de368f Mon Sep 17 00:00:00 2001 From: Fernando Herrera Date: Sat, 25 Sep 2021 15:56:33 +0100 Subject: [PATCH 2/7] writing to stdout --- src/main.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 1586a21a68..84976b179a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +use std::io::Write; + use miette::{IntoDiagnostic, Result}; use nu_cli::{report_error, NuCompleter, NuHighlighter, NuValidator}; use nu_command::create_default_context; @@ -139,7 +141,12 @@ fn main() -> Result<()> { } None => value.into_string(), }; - println!("{}", output); + let stdout = std::io::stdout(); + + match stdout.lock().write_all(output.as_bytes()) { + Ok(_) => (), + Err(err) => eprintln!("{}", err), + }; } Err(err) => { let engine_state = engine_state.borrow(); From 637e4f6e6d9a398050101a7d4fda81bf0b0c186b Mon Sep 17 00:00:00 2001 From: Fernando Herrera Date: Sat, 25 Sep 2021 15:58:04 +0100 Subject: [PATCH 3/7] simplify command call --- src/main.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 84976b179a..f98195f999 100644 --- a/src/main.rs +++ b/src/main.rs @@ -134,9 +134,11 @@ fn main() -> Result<()> { 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)?; + let table = engine_state.get_decl(decl_id).run( + &state, + &Call::new(), + value, + )?; table.into_string() } None => value.into_string(), From 25a776c36b2af490deac7952e991b608b5f00d40 Mon Sep 17 00:00:00 2001 From: Fernando Herrera Date: Sat, 25 Sep 2021 16:45:02 +0100 Subject: [PATCH 4/7] trim lines in command --- crates/nu-command/src/build_string.rs | 13 ++++++------- crates/nu-command/src/lines.rs | 2 +- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/crates/nu-command/src/build_string.rs b/crates/nu-command/src/build_string.rs index 90516cde07..a0b64b9f3d 100644 --- a/crates/nu-command/src/build_string.rs +++ b/crates/nu-command/src/build_string.rs @@ -1,7 +1,7 @@ use nu_engine::eval_expression; use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EvaluationContext}; -use nu_protocol::{Signature, SyntaxShape, Value}; +use nu_protocol::{ShellError, Signature, SyntaxShape, Value}; pub struct BuildString; @@ -24,13 +24,12 @@ impl Command for BuildString { call: &Call, _input: Value, ) -> Result { - let mut output = vec![]; + let output = call + .positional + .iter() + .map(|expr| eval_expression(context, expr).map(|val| val.into_string())) + .collect::, ShellError>>()?; - for expr in &call.positional { - let val = eval_expression(context, expr)?; - - output.push(val.into_string()); - } Ok(Value::String { val: output.join(""), span: call.head, diff --git a/crates/nu-command/src/lines.rs b/crates/nu-command/src/lines.rs index f3517c8e65..74333ff3ef 100644 --- a/crates/nu-command/src/lines.rs +++ b/crates/nu-command/src/lines.rs @@ -62,7 +62,7 @@ impl Command for Lines { .filter_map(|s| { if !s.is_empty() { Some(Value::String { - val: s.into(), + val: s.trim().into(), span, }) } else { From dadc3548477d6c5d583c0cb4ab0e3a0eb2e4cc03 Mon Sep 17 00:00:00 2001 From: Fernando Herrera Date: Sat, 25 Sep 2021 16:58:50 +0100 Subject: [PATCH 5/7] move print to function --- src/main.rs | 48 +++++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/src/main.rs b/src/main.rs index f98195f999..9f0a43f5d5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,7 @@ use nu_parser::parse; use nu_protocol::{ ast::Call, engine::{EngineState, EvaluationContext, StateWorkingSet}, - Value, + ShellError, Value, }; use reedline::DefaultCompletionActionHandler; @@ -128,28 +128,7 @@ fn main() -> Result<()> { }; match eval_block(&state, &block, Value::nothing()) { - Ok(value) => { - // 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 table = engine_state.get_decl(decl_id).run( - &state, - &Call::new(), - value, - )?; - table.into_string() - } - None => value.into_string(), - }; - let stdout = std::io::stdout(); - - match stdout.lock().write_all(output.as_bytes()) { - Ok(_) => (), - Err(err) => eprintln!("{}", err), - }; - } + Ok(value) => print_value(value, &state)?, Err(err) => { let engine_state = engine_state.borrow(); let working_set = StateWorkingSet::new(&*engine_state); @@ -179,3 +158,26 @@ fn main() -> Result<()> { Ok(()) } } + +fn print_value(value: Value, state: &EvaluationContext) -> Result<(), ShellError> { + // 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 = state.engine_state.borrow(); + let output = match engine_state.find_decl("table".as_bytes()) { + Some(decl_id) => { + let table = engine_state + .get_decl(decl_id) + .run(&state, &Call::new(), value)?; + table.into_string() + } + None => value.into_string(), + }; + let stdout = std::io::stdout(); + + match stdout.lock().write_all(output.as_bytes()) { + Ok(_) => (), + Err(err) => eprintln!("{}", err), + }; + + Ok(()) +} From 63874010415354e92eeffb22770fe6d55653398a Mon Sep 17 00:00:00 2001 From: Fernando Herrera Date: Sat, 25 Sep 2021 17:03:25 +0100 Subject: [PATCH 6/7] clippy error --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 9f0a43f5d5..8824660214 100644 --- a/src/main.rs +++ b/src/main.rs @@ -167,7 +167,7 @@ fn print_value(value: Value, state: &EvaluationContext) -> Result<(), ShellError Some(decl_id) => { let table = engine_state .get_decl(decl_id) - .run(&state, &Call::new(), value)?; + .run(state, &Call::new(), value)?; table.into_string() } None => value.into_string(), From d9c42eb194eb48607556b459fb5a337982eafaf1 Mon Sep 17 00:00:00 2001 From: Fernando Herrera Date: Sat, 25 Sep 2021 17:28:15 +0100 Subject: [PATCH 7/7] contents declaration --- crates/nu-command/src/default_context.rs | 2 ++ crates/nu-protocol/src/engine/engine_state.rs | 5 +++++ src/main.rs | 3 +++ 3 files changed, 10 insertions(+) diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index 5890d791cb..9beee6c1f1 100644 --- a/crates/nu-command/src/default_context.rs +++ b/crates/nu-command/src/default_context.rs @@ -67,6 +67,8 @@ pub fn create_default_context() -> Rc> { working_set.add_decl(sig.predeclare()); let sig = Signature::build("stack"); working_set.add_decl(sig.predeclare()); + let sig = Signature::build("contents"); + working_set.add_decl(sig.predeclare()); working_set.render() }; diff --git a/crates/nu-protocol/src/engine/engine_state.rs b/crates/nu-protocol/src/engine/engine_state.rs index 6a75884289..11941fe428 100644 --- a/crates/nu-protocol/src/engine/engine_state.rs +++ b/crates/nu-protocol/src/engine/engine_state.rs @@ -113,6 +113,11 @@ impl EngineState { } } + pub fn print_contents(&self) { + let string = String::from_utf8_lossy(&self.file_contents); + println!("{}", string); + } + pub fn find_decl(&self, name: &[u8]) -> Option { for scope in self.scope.iter().rev() { if let Some(decl_id) = scope.decls.get(name) { diff --git a/src/main.rs b/src/main.rs index 8824660214..577a9c7211 100644 --- a/src/main.rs +++ b/src/main.rs @@ -102,6 +102,9 @@ fn main() -> Result<()> { } else if s.trim() == "stack" { stack.print_stack(); continue; + } else if s.trim() == "contents" { + engine_state.borrow().print_contents(); + continue; } let (block, delta) = {