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

@ -230,7 +230,7 @@ impl Command for Char {
// handle -l flag
if list {
return generate_character_list(ctrlc, call.head);
return Ok(generate_character_list(ctrlc, call.head));
}
// handle -i flag
@ -265,7 +265,7 @@ impl Command for Char {
// handle -l flag
if list {
return generate_character_list(ctrlc, call_span);
return Ok(generate_character_list(ctrlc, call_span));
}
// handle -i flag
@ -286,11 +286,8 @@ impl Command for Char {
}
}
fn generate_character_list(
ctrlc: Option<Arc<AtomicBool>>,
call_span: Span,
) -> Result<PipelineData, ShellError> {
Ok(CHAR_MAP
fn generate_character_list(ctrlc: Option<Arc<AtomicBool>>, call_span: Span) -> PipelineData {
CHAR_MAP
.iter()
.map(move |(name, s)| {
let unicode = Value::string(
@ -308,7 +305,7 @@ fn generate_character_list(
Value::record(record, call_span)
})
.into_pipeline_data(ctrlc))
.into_pipeline_data(call_span, ctrlc)
}
fn handle_integer_flag(

View File

@ -159,7 +159,7 @@ fn guess_width(
Err(e) => Value::error(e, input_span),
}
})
.into_pipeline_data(engine_state.ctrlc.clone()))
.into_pipeline_data(input_span, engine_state.ctrlc.clone()))
} else {
let length = result[0].len();
let columns: Vec<String> = (0..length).map(|n| format!("column{n}")).collect();
@ -184,7 +184,7 @@ fn guess_width(
Err(e) => Value::error(e, input_span),
}
})
.into_pipeline_data(engine_state.ctrlc.clone()))
.into_pipeline_data(input_span, engine_state.ctrlc.clone()))
}
}
@ -278,7 +278,7 @@ fn detect_columns(
None => Value::record(record, name_span),
}
})
.into_pipeline_data(ctrlc))
.into_pipeline_data(call.head, ctrlc))
} else {
Ok(PipelineData::empty())
}

View File

@ -1,6 +1,6 @@
use fancy_regex::Regex;
use nu_engine::command_prelude::*;
use nu_protocol::ListStream;
use nu_protocol::{ListStream, ValueIterator};
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
@ -187,44 +187,36 @@ fn operate(
}
}
Ok(PipelineData::ListStream(
ListStream::from_stream(parsed.into_iter(), ctrlc),
None,
))
Ok(ListStream::new(parsed.into_iter(), head, ctrlc).into())
}
PipelineData::ListStream(stream, ..) => Ok(PipelineData::ListStream(
ListStream::from_stream(
ParseStreamer {
span: head,
excess: Vec::new(),
regex: regex_pattern,
columns,
stream: stream.stream,
ctrlc: ctrlc.clone(),
},
PipelineData::ListStream(stream, ..) => Ok(stream
.modify(|stream| ParseStreamer {
span: head,
excess: Vec::new(),
regex: regex_pattern,
columns,
stream,
ctrlc,
),
None,
)),
})
.into()),
PipelineData::ExternalStream { stdout: None, .. } => Ok(PipelineData::Empty),
PipelineData::ExternalStream {
stdout: Some(stream),
..
} => Ok(PipelineData::ListStream(
ListStream::from_stream(
ParseStreamerExternal {
span: head,
excess: Vec::new(),
regex: regex_pattern,
columns,
stream: stream.stream,
},
ctrlc,
),
None,
)),
} => Ok(ListStream::new(
ParseStreamerExternal {
span: head,
excess: Vec::new(),
regex: regex_pattern,
columns,
stream: stream.stream,
},
head,
ctrlc,
)
.into()),
}
}
@ -299,7 +291,7 @@ pub struct ParseStreamer {
excess: Vec<Value>,
regex: Regex,
columns: Vec<String>,
stream: Box<dyn Iterator<Item = Value> + Send + 'static>,
stream: ValueIterator,
ctrlc: Option<Arc<AtomicBool>>,
}

View File

@ -196,9 +196,12 @@ fn split_list(
let mut temp_list = Vec::new();
let mut returned_list = Vec::new();
let iter = input.into_interruptible_iter(engine_state.ctrlc.clone());
let matcher = Matcher::new(call.has_flag(engine_state, stack, "regex")?, separator)?;
for val in iter {
for val in input {
if nu_utils::ctrl_c::was_pressed(&engine_state.ctrlc) {
break;
}
if matcher.compare(&val)? {
if !temp_list.is_empty() {
returned_list.push(Value::list(temp_list.clone(), call.head));