ListStream touchup (#12524)

# Description

Does some misc changes to `ListStream`:
- Moves it into its own module/file separate from `RawStream`.
- `ListStream`s now have an associated `Span`.
- This required changes to `ListStreamInfo` in `nu-plugin`. Note sure if
this is a breaking change for the plugin protocol.
- Hides the internals of `ListStream` but also adds a few more methods.
- This includes two functions to more easily alter a stream (these take
a `ListStream` and return a `ListStream` instead of having to go through
the whole `into_pipeline_data(..)` route).
  -  `map`: takes a `FnMut(Value) -> Value`
  - `modify`: takes a function to modify the inner stream.
This commit is contained in:
Ian Manske
2024-05-05 16:00:59 +00:00
committed by GitHub
parent 3143ded374
commit e879d4ecaf
106 changed files with 957 additions and 874 deletions

View File

@ -88,7 +88,7 @@ impl Command for Complete {
};
if let Some(exit_code) = exit_code {
let mut v: Vec<_> = exit_code.collect();
let mut v: Vec<_> = exit_code.into_iter().collect();
if let Some(v) = v.pop() {
record.push("exit_code", v);

View File

@ -195,5 +195,5 @@ fn run_ps(
Ok(output
.into_iter()
.into_pipeline_data(engine_state.ctrlc.clone()))
.into_pipeline_data(span, engine_state.ctrlc.clone()))
}

View File

@ -106,7 +106,7 @@ fn registry_query(
*registry_key_span,
))
}
Ok(reg_values.into_pipeline_data(engine_state.ctrlc.clone()))
Ok(reg_values.into_pipeline_data(call_span, engine_state.ctrlc.clone()))
} else {
match registry_value {
Some(value) => {

View File

@ -163,8 +163,6 @@ impl ExternalCommand {
) -> Result<PipelineData, ShellError> {
let head = self.name.span;
let ctrlc = engine_state.ctrlc.clone();
#[allow(unused_mut)]
let (cmd, mut reader) = self.create_process(&input, false, head)?;
@ -431,7 +429,7 @@ impl ExternalCommand {
(
Some(RawStream::new(
Box::new(ByteLines::new(combined)),
ctrlc.clone(),
engine_state.ctrlc.clone(),
head,
None,
)),
@ -439,11 +437,21 @@ impl ExternalCommand {
)
} else {
let stdout = child.as_mut().stdout.take().map(|out| {
RawStream::new(Box::new(ByteLines::new(out)), ctrlc.clone(), head, None)
RawStream::new(
Box::new(ByteLines::new(out)),
engine_state.ctrlc.clone(),
head,
None,
)
});
let stderr = child.as_mut().stderr.take().map(|err| {
RawStream::new(Box::new(ByteLines::new(err)), ctrlc.clone(), head, None)
RawStream::new(
Box::new(ByteLines::new(err)),
engine_state.ctrlc.clone(),
head,
None,
)
});
if matches!(self.err, OutDest::Pipe) {
@ -505,15 +513,16 @@ impl ExternalCommand {
})
.err_span(head)?;
let exit_code_receiver = ValueReceiver::new(exit_code_rx);
let exit_code = Some(ListStream::new(
ValueReceiver::new(exit_code_rx),
head,
None,
));
Ok(PipelineData::ExternalStream {
stdout,
stderr,
exit_code: Some(ListStream::from_stream(
Box::new(exit_code_receiver),
ctrlc.clone(),
)),
exit_code,
span: head,
metadata: None,
trim_end_newline: true,

View File

@ -214,6 +214,7 @@ fn which(
stack: &mut Stack,
call: &Call,
) -> Result<PipelineData, ShellError> {
let head = call.head;
let which_args = WhichArgs {
applications: call.rest(engine_state, stack, 0)?,
all: call.has_flag(engine_state, stack, "all")?,
@ -223,7 +224,7 @@ fn which(
if which_args.applications.is_empty() {
return Err(ShellError::MissingParameter {
param_name: "application".into(),
span: call.head,
span: head,
});
}
@ -231,7 +232,7 @@ fn which(
#[allow(deprecated)]
let cwd = env::current_dir_str(engine_state, stack)?;
let paths = env::path_str(engine_state, stack, call.head)?;
let paths = env::path_str(engine_state, stack, head)?;
for app in which_args.applications {
let values = which_single(
@ -244,7 +245,7 @@ fn which(
output.extend(values);
}
Ok(output.into_iter().into_pipeline_data(ctrlc))
Ok(output.into_iter().into_pipeline_data(head, ctrlc))
}
#[cfg(test)]