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]]
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",

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(),
})
}
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
// 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(", ")
)
.join("\n")
}
pub fn from_stream(input: impl Iterator<Item = Value> + 'static) -> ValueStream {