From b4977f15159b9137be783165337a2b28fe334bd6 Mon Sep 17 00:00:00 2001 From: Fernando Herrera Date: Fri, 24 Sep 2021 13:03:39 +0100 Subject: [PATCH 1/2] better print out for stream output --- Cargo.lock | 12 ++++++------ crates/nu-command/src/lines.rs | 23 ++++++++++++----------- crates/nu-command/src/run_external.rs | 2 +- crates/nu-protocol/src/shell_error.rs | 4 ++++ crates/nu-protocol/src/value/stream.rs | 9 +++------ 5 files changed, 26 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ce3a3c1d07..37aa2c5045 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -234,9 +234,9 @@ dependencies = [ [[package]] name = "instant" -version = "0.1.10" +version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bee0328b1209d157ef001c94dd85b4f8f64139adb0eac2659f4b08382b2f474d" +checksum = "716d3d89f35ac6a34fd0eed635395f4c3b76fa889338a4632e5231a8684216bd" dependencies = [ "cfg-if", ] @@ -625,8 +625,8 @@ dependencies = [ [[package]] name = "reedline" -version = "0.1.0" -source = "git+https://github.com/jntrnr/reedline?branch=main#22fc31c68d1da6a41a93b5cfa901143b4eca4051" +version = "0.2.0" +source = "git+https://github.com/jntrnr/reedline?branch=main#93c2146fcf4257c40426bc2f0c6903d4115caaf1" dependencies = [ "chrono", "crossterm", @@ -772,9 +772,9 @@ dependencies = [ [[package]] name = "syn" -version = "1.0.76" +version = "1.0.77" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c6f107db402c2c2055242dbf4d2af0e69197202e9faacbef9571bbe47f5a1b84" +checksum = "5239bc68e0fef57495900cfea4e8dc75596d9a319d7e16b1e0a440d24e6fe0a0" dependencies = [ "proc-macro2", "quote", diff --git a/crates/nu-command/src/lines.rs b/crates/nu-command/src/lines.rs index 3694f26e20..f3517c8e65 100644 --- a/crates/nu-command/src/lines.rs +++ b/crates/nu-command/src/lines.rs @@ -3,7 +3,7 @@ use std::rc::Rc; use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EvaluationContext}; -use nu_protocol::{Signature, Span, Value, ValueStream}; +use nu_protocol::{ShellError, Signature, Span, Value, ValueStream}; pub struct Lines; @@ -25,10 +25,10 @@ impl Command for Lines { fn run( &self, _context: &EvaluationContext, - _call: &Call, + call: &Call, input: Value, ) -> Result { - let value = match input { + match input { #[allow(clippy::needless_collect)] // Collect is needed because the string may not live long enough for // the Rc structure to continue using it. If split could take ownership @@ -47,10 +47,10 @@ impl Command for Lines { } }); - Value::Stream { + Ok(Value::Stream { stream: ValueStream(Rc::new(RefCell::new(iter))), span: Span::unknown(), - } + }) } Value::Stream { stream, span: _ } => { let iter = stream @@ -78,14 +78,15 @@ impl Command for Lines { }) .flatten(); - Value::Stream { + Ok(Value::Stream { stream: ValueStream(Rc::new(RefCell::new(iter))), span: Span::unknown(), - } + }) } - _ => unimplemented!(), - }; - - Ok(value) + val => Err(ShellError::UnsupportedInput( + format!("Not supported input: {}", val.as_string()?), + call.head, + )), + } } } diff --git a/crates/nu-command/src/run_external.rs b/crates/nu-command/src/run_external.rs index 557d9d7408..3ed506176b 100644 --- a/crates/nu-command/src/run_external.rs +++ b/crates/nu-command/src/run_external.rs @@ -154,7 +154,7 @@ impl<'call, 'contex> ExternalCommand<'call, 'contex> { // If this external is not the last expression, then its output is piped to a channel // and we create a ValueStream that can be consumed let value = if !self.last_expression { - let (tx, rx) = mpsc::sync_channel(0); + let (tx, rx) = mpsc::channel(); let stdout = child.stdout.take().ok_or_else(|| { ShellError::ExternalCommand( "Error taking stdout from external".to_string(), diff --git a/crates/nu-protocol/src/shell_error.rs b/crates/nu-protocol/src/shell_error.rs index 9d1677116f..ab8a735aa9 100644 --- a/crates/nu-protocol/src/shell_error.rs +++ b/crates/nu-protocol/src/shell_error.rs @@ -69,4 +69,8 @@ pub enum ShellError { #[error("External command")] #[diagnostic(code(nu::shell::external_command), url(docsrs))] ExternalCommand(String, #[label("{0}")] Span), + + #[error("Unsupported input")] + #[diagnostic(code(nu::shell::unsupported_input), url(docsrs))] + UnsupportedInput(String, #[label("{0}")] Span), } diff --git a/crates/nu-protocol/src/value/stream.rs b/crates/nu-protocol/src/value/stream.rs index ca56f39f05..2bbadb42b7 100644 --- a/crates/nu-protocol/src/value/stream.rs +++ b/crates/nu-protocol/src/value/stream.rs @@ -6,12 +6,9 @@ pub struct ValueStream(pub Rc>>); impl ValueStream { pub fn into_string(self) -> String { - format!( - "[{}]", - self.map(|x: Value| x.into_string()) - .collect::>() - .join(", ") - ) + self.map(|x: Value| x.into_string()) + .collect::>() + .join("\n") } pub fn from_stream(input: impl Iterator + 'static) -> ValueStream { From 767d822cbf50da2dc698902daebb427f0f79b665 Mon Sep 17 00:00:00 2001 From: Fernando Herrera Date: Fri, 24 Sep 2021 13:20:50 +0100 Subject: [PATCH 2/2] change line format for test --- crates/nu-protocol/src/value/stream.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/crates/nu-protocol/src/value/stream.rs b/crates/nu-protocol/src/value/stream.rs index 2bbadb42b7..ca56f39f05 100644 --- a/crates/nu-protocol/src/value/stream.rs +++ b/crates/nu-protocol/src/value/stream.rs @@ -6,9 +6,12 @@ pub struct ValueStream(pub Rc>>); impl ValueStream { pub fn into_string(self) -> String { - self.map(|x: Value| x.into_string()) - .collect::>() - .join("\n") + format!( + "[{}]", + self.map(|x: Value| x.into_string()) + .collect::>() + .join(", ") + ) } pub fn from_stream(input: impl Iterator + 'static) -> ValueStream {