mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 03:34:58 +02:00
fix exit_code handling when running a scripts with ctrlc (#11466)
# Description Fixes: #11394 When run `^sleep 3` we have an `exit_code ListStream`, and when we press ctrl-c, this `ListStream` will return None. But it's not expected, because `exit_code` sender in `run_external` always send an exit code out. This pr is trying to fix the issue by introducing a `first_guard` into ListStream, it will always generate a value from underlying stream if `first_guard` is true, so it's guarantee to have at least one value to return. And the pr also do a little refactor, which makes use of `ListStream::from_stream` rather than construct it manually. # User-Facing Changes ## Before ``` > nu -c "^sleep 3" # press ctrl-c > echo $env.LAST_EXIT_CODE 0 ``` ## After ``` > nu -c "^sleep 3" # press ctrl-c > echo $env.LAST_EXIT_CODE 255 ``` # Tests + Formatting None, sorry that I don't think it's easy to test the ctrlc behavior. # After Submitting None
This commit is contained in:
@ -184,6 +184,7 @@ impl Iterator for RawStream {
|
||||
pub struct ListStream {
|
||||
pub stream: Box<dyn Iterator<Item = Value> + Send + 'static>,
|
||||
pub ctrlc: Option<Arc<AtomicBool>>,
|
||||
first_guard: bool,
|
||||
}
|
||||
|
||||
impl ListStream {
|
||||
@ -209,6 +210,7 @@ impl ListStream {
|
||||
ListStream {
|
||||
stream: Box::new(input),
|
||||
ctrlc,
|
||||
first_guard: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -223,6 +225,17 @@ impl Iterator for ListStream {
|
||||
type Item = Value;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
// We need to check `first_guard` to guarantee that it always have something to return in
|
||||
// underlying stream.
|
||||
//
|
||||
// A realworld example is running an external commands, which have an `exit_code`
|
||||
// ListStream.
|
||||
// When we press ctrl-c, the external command receives the signal too, if we don't have
|
||||
// `first_guard`, the `exit_code` ListStream will return Nothing, which is not expected
|
||||
if self.first_guard {
|
||||
self.first_guard = false;
|
||||
return self.stream.next();
|
||||
}
|
||||
if nu_utils::ctrl_c::was_pressed(&self.ctrlc) {
|
||||
None
|
||||
} else {
|
||||
|
Reference in New Issue
Block a user