better print out for stream output

This commit is contained in:
Fernando Herrera 2021-09-24 13:03:39 +01:00
parent 6c589affe7
commit b4977f1515
5 changed files with 26 additions and 24 deletions

12
Cargo.lock generated
View File

@ -234,9 +234,9 @@ dependencies = [
[[package]] [[package]]
name = "instant" name = "instant"
version = "0.1.10" version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bee0328b1209d157ef001c94dd85b4f8f64139adb0eac2659f4b08382b2f474d" checksum = "716d3d89f35ac6a34fd0eed635395f4c3b76fa889338a4632e5231a8684216bd"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
] ]
@ -625,8 +625,8 @@ dependencies = [
[[package]] [[package]]
name = "reedline" name = "reedline"
version = "0.1.0" version = "0.2.0"
source = "git+https://github.com/jntrnr/reedline?branch=main#22fc31c68d1da6a41a93b5cfa901143b4eca4051" source = "git+https://github.com/jntrnr/reedline?branch=main#93c2146fcf4257c40426bc2f0c6903d4115caaf1"
dependencies = [ dependencies = [
"chrono", "chrono",
"crossterm", "crossterm",
@ -772,9 +772,9 @@ dependencies = [
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.76" version = "1.0.77"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c6f107db402c2c2055242dbf4d2af0e69197202e9faacbef9571bbe47f5a1b84" checksum = "5239bc68e0fef57495900cfea4e8dc75596d9a319d7e16b1e0a440d24e6fe0a0"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",

View File

@ -3,7 +3,7 @@ use std::rc::Rc;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext}; use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{Signature, Span, Value, ValueStream}; use nu_protocol::{ShellError, Signature, Span, Value, ValueStream};
pub struct Lines; pub struct Lines;
@ -25,10 +25,10 @@ impl Command for Lines {
fn run( fn run(
&self, &self,
_context: &EvaluationContext, _context: &EvaluationContext,
_call: &Call, call: &Call,
input: Value, input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> { ) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
let value = match input { match input {
#[allow(clippy::needless_collect)] #[allow(clippy::needless_collect)]
// Collect is needed because the string may not live long enough for // Collect is needed because the string may not live long enough for
// the Rc structure to continue using it. If split could take ownership // 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))), stream: ValueStream(Rc::new(RefCell::new(iter))),
span: Span::unknown(), span: Span::unknown(),
} })
} }
Value::Stream { stream, span: _ } => { Value::Stream { stream, span: _ } => {
let iter = stream let iter = stream
@ -78,14 +78,15 @@ impl Command for Lines {
}) })
.flatten(); .flatten();
Value::Stream { Ok(Value::Stream {
stream: ValueStream(Rc::new(RefCell::new(iter))), stream: ValueStream(Rc::new(RefCell::new(iter))),
span: Span::unknown(), span: Span::unknown(),
})
} }
val => Err(ShellError::UnsupportedInput(
format!("Not supported input: {}", val.as_string()?),
call.head,
)),
} }
_ => unimplemented!(),
};
Ok(value)
} }
} }

View File

@ -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 // 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 // and we create a ValueStream that can be consumed
let value = if !self.last_expression { 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(|| { let stdout = child.stdout.take().ok_or_else(|| {
ShellError::ExternalCommand( ShellError::ExternalCommand(
"Error taking stdout from external".to_string(), "Error taking stdout from external".to_string(),

View File

@ -69,4 +69,8 @@ pub enum ShellError {
#[error("External command")] #[error("External command")]
#[diagnostic(code(nu::shell::external_command), url(docsrs))] #[diagnostic(code(nu::shell::external_command), url(docsrs))]
ExternalCommand(String, #[label("{0}")] Span), ExternalCommand(String, #[label("{0}")] Span),
#[error("Unsupported input")]
#[diagnostic(code(nu::shell::unsupported_input), url(docsrs))]
UnsupportedInput(String, #[label("{0}")] Span),
} }

View File

@ -6,12 +6,9 @@ pub struct ValueStream(pub Rc<RefCell<dyn Iterator<Item = Value>>>);
impl ValueStream { impl ValueStream {
pub fn into_string(self) -> String { pub fn into_string(self) -> String {
format!(
"[{}]",
self.map(|x: Value| x.into_string()) self.map(|x: Value| x.into_string())
.collect::<Vec<String>>() .collect::<Vec<String>>()
.join(", ") .join("\n")
)
} }
pub fn from_stream(input: impl Iterator<Item = Value> + 'static) -> ValueStream { pub fn from_stream(input: impl Iterator<Item = Value> + 'static) -> ValueStream {