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

View File

@ -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<nu_protocol::Value, nu_protocol::ShellError> {
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,
)),
}
}
}

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
// 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(),

View File

@ -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),
}

View File

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