mirror of
https://github.com/nushell/nushell.git
synced 2025-07-01 07:00:37 +02:00
Add and use new Signals
struct (#13314)
# Description This PR introduces a new `Signals` struct to replace our adhoc passing around of `ctrlc: Option<Arc<AtomicBool>>`. Doing so has a few benefits: - We can better enforce when/where resetting or triggering an interrupt is allowed. - Consolidates `nu_utils::ctrl_c::was_pressed` and other ad-hoc re-implementations into a single place: `Signals::check`. - This allows us to add other types of signals later if we want. E.g., exiting or suspension. - Similarly, we can more easily change the underlying implementation if we need to in the future. - Places that used to have a `ctrlc` of `None` now use `Signals::empty()`, so we can double check these usages for correctness in the future.
This commit is contained in:
@ -79,7 +79,7 @@ impl Command for BitsAnd {
|
||||
|
||||
input.map(
|
||||
move |value| binary_op(&value, &target, little_endian, |(l, r)| l & r, head),
|
||||
engine_state.ctrlc.clone(),
|
||||
engine_state.signals(),
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,7 @@ use std::io::{self, Read, Write};
|
||||
use nu_cmd_base::input_handler::{operate, CmdArgument};
|
||||
use nu_engine::command_prelude::*;
|
||||
|
||||
use nu_protocol::Signals;
|
||||
use num_traits::ToPrimitive;
|
||||
|
||||
pub struct Arguments {
|
||||
@ -127,31 +128,36 @@ fn into_bits(
|
||||
))
|
||||
} else {
|
||||
let args = Arguments { cell_paths };
|
||||
operate(action, args, input, call.head, engine_state.ctrlc.clone())
|
||||
operate(action, args, input, call.head, engine_state.signals())
|
||||
}
|
||||
}
|
||||
|
||||
fn byte_stream_to_bits(stream: ByteStream, head: Span) -> ByteStream {
|
||||
if let Some(mut reader) = stream.reader() {
|
||||
let mut is_first = true;
|
||||
ByteStream::from_fn(head, None, ByteStreamType::String, move |buffer| {
|
||||
let mut byte = [0];
|
||||
if reader.read(&mut byte[..]).err_span(head)? > 0 {
|
||||
// Format the byte as bits
|
||||
if is_first {
|
||||
is_first = false;
|
||||
ByteStream::from_fn(
|
||||
head,
|
||||
Signals::empty(),
|
||||
ByteStreamType::String,
|
||||
move |buffer| {
|
||||
let mut byte = [0];
|
||||
if reader.read(&mut byte[..]).err_span(head)? > 0 {
|
||||
// Format the byte as bits
|
||||
if is_first {
|
||||
is_first = false;
|
||||
} else {
|
||||
buffer.push(b' ');
|
||||
}
|
||||
write!(buffer, "{:08b}", byte[0]).expect("format failed");
|
||||
Ok(true)
|
||||
} else {
|
||||
buffer.push(b' ');
|
||||
// EOF
|
||||
Ok(false)
|
||||
}
|
||||
write!(buffer, "{:08b}", byte[0]).expect("format failed");
|
||||
Ok(true)
|
||||
} else {
|
||||
// EOF
|
||||
Ok(false)
|
||||
}
|
||||
})
|
||||
},
|
||||
)
|
||||
} else {
|
||||
ByteStream::read(io::empty(), head, None, ByteStreamType::String)
|
||||
ByteStream::read(io::empty(), head, Signals::empty(), ByteStreamType::String)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -82,7 +82,7 @@ impl Command for BitsNot {
|
||||
number_size,
|
||||
};
|
||||
|
||||
operate(action, args, input, head, engine_state.ctrlc.clone())
|
||||
operate(action, args, input, head, engine_state.signals())
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
|
@ -80,7 +80,7 @@ impl Command for BitsOr {
|
||||
|
||||
input.map(
|
||||
move |value| binary_op(&value, &target, little_endian, |(l, r)| l | r, head),
|
||||
engine_state.ctrlc.clone(),
|
||||
engine_state.signals(),
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -86,7 +86,7 @@ impl Command for BitsRol {
|
||||
bits,
|
||||
};
|
||||
|
||||
operate(action, args, input, head, engine_state.ctrlc.clone())
|
||||
operate(action, args, input, head, engine_state.signals())
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
|
@ -86,7 +86,7 @@ impl Command for BitsRor {
|
||||
bits,
|
||||
};
|
||||
|
||||
operate(action, args, input, head, engine_state.ctrlc.clone())
|
||||
operate(action, args, input, head, engine_state.signals())
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
|
@ -88,7 +88,7 @@ impl Command for BitsShl {
|
||||
bits,
|
||||
};
|
||||
|
||||
operate(action, args, input, head, engine_state.ctrlc.clone())
|
||||
operate(action, args, input, head, engine_state.signals())
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
|
@ -88,7 +88,7 @@ impl Command for BitsShr {
|
||||
bits,
|
||||
};
|
||||
|
||||
operate(action, args, input, head, engine_state.ctrlc.clone())
|
||||
operate(action, args, input, head, engine_state.signals())
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
|
@ -80,7 +80,7 @@ impl Command for BitsXor {
|
||||
|
||||
input.map(
|
||||
move |value| binary_op(&value, &target, little_endian, |(l, r)| l ^ r, head),
|
||||
engine_state.ctrlc.clone(),
|
||||
engine_state.signals(),
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -59,7 +59,7 @@ fn fmt(
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
|
||||
let args = CellPathOnlyArgs::from(cell_paths);
|
||||
operate(action, args, input, call.head, engine_state.ctrlc.clone())
|
||||
operate(action, args, input, call.head, engine_state.signals())
|
||||
}
|
||||
|
||||
fn action(input: &Value, _args: &CellPathOnlyArgs, span: Span) -> Value {
|
||||
|
@ -89,7 +89,7 @@ impl Command for EachWhile {
|
||||
}
|
||||
})
|
||||
.fuse()
|
||||
.into_pipeline_data(head, engine_state.ctrlc.clone()))
|
||||
.into_pipeline_data(head, engine_state.signals().clone()))
|
||||
}
|
||||
PipelineData::ByteStream(stream, ..) => {
|
||||
let span = stream.span();
|
||||
@ -107,7 +107,7 @@ impl Command for EachWhile {
|
||||
}
|
||||
})
|
||||
.fuse()
|
||||
.into_pipeline_data(head, engine_state.ctrlc.clone()))
|
||||
.into_pipeline_data(head, engine_state.signals().clone()))
|
||||
} else {
|
||||
Ok(PipelineData::Empty)
|
||||
}
|
||||
|
@ -108,7 +108,7 @@ impl Command for UpdateCells {
|
||||
columns,
|
||||
span: head,
|
||||
}
|
||||
.into_pipeline_data(head, engine_state.ctrlc.clone())
|
||||
.into_pipeline_data(head, engine_state.signals().clone())
|
||||
.set_metadata(metadata))
|
||||
}
|
||||
}
|
||||
|
@ -45,7 +45,7 @@ impl Command for SubCommand {
|
||||
}
|
||||
input.map(
|
||||
move |value| operate(value, head, use_degrees),
|
||||
engine_state.ctrlc.clone(),
|
||||
engine_state.signals(),
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -41,10 +41,7 @@ impl Command for SubCommand {
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty { dst_span: head });
|
||||
}
|
||||
input.map(
|
||||
move |value| operate(value, head),
|
||||
engine_state.ctrlc.clone(),
|
||||
)
|
||||
input.map(move |value| operate(value, head), engine_state.signals())
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
|
@ -45,7 +45,7 @@ impl Command for SubCommand {
|
||||
}
|
||||
input.map(
|
||||
move |value| operate(value, head, use_degrees),
|
||||
engine_state.ctrlc.clone(),
|
||||
engine_state.signals(),
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -41,10 +41,7 @@ impl Command for SubCommand {
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty { dst_span: head });
|
||||
}
|
||||
input.map(
|
||||
move |value| operate(value, head),
|
||||
engine_state.ctrlc.clone(),
|
||||
)
|
||||
input.map(move |value| operate(value, head), engine_state.signals())
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
|
@ -45,7 +45,7 @@ impl Command for SubCommand {
|
||||
}
|
||||
input.map(
|
||||
move |value| operate(value, head, use_degrees),
|
||||
engine_state.ctrlc.clone(),
|
||||
engine_state.signals(),
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -41,10 +41,7 @@ impl Command for SubCommand {
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty { dst_span: head });
|
||||
}
|
||||
input.map(
|
||||
move |value| operate(value, head),
|
||||
engine_state.ctrlc.clone(),
|
||||
)
|
||||
input.map(move |value| operate(value, head), engine_state.signals())
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
|
@ -44,7 +44,7 @@ impl Command for SubCommand {
|
||||
}
|
||||
input.map(
|
||||
move |value| operate(value, head, use_degrees),
|
||||
engine_state.ctrlc.clone(),
|
||||
engine_state.signals(),
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -41,10 +41,7 @@ impl Command for SubCommand {
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty { dst_span: head });
|
||||
}
|
||||
input.map(
|
||||
move |value| operate(value, head),
|
||||
engine_state.ctrlc.clone(),
|
||||
)
|
||||
input.map(move |value| operate(value, head), engine_state.signals())
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
|
@ -41,10 +41,7 @@ impl Command for SubCommand {
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty { dst_span: head });
|
||||
}
|
||||
input.map(
|
||||
move |value| operate(value, head),
|
||||
engine_state.ctrlc.clone(),
|
||||
)
|
||||
input.map(move |value| operate(value, head), engine_state.signals())
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
|
@ -41,10 +41,7 @@ impl Command for SubCommand {
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty { dst_span: head });
|
||||
}
|
||||
input.map(
|
||||
move |value| operate(value, head),
|
||||
engine_state.ctrlc.clone(),
|
||||
)
|
||||
input.map(move |value| operate(value, head), engine_state.signals())
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
|
@ -44,7 +44,7 @@ impl Command for SubCommand {
|
||||
}
|
||||
input.map(
|
||||
move |value| operate(value, head, use_degrees),
|
||||
engine_state.ctrlc.clone(),
|
||||
engine_state.signals(),
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -41,10 +41,7 @@ impl Command for SubCommand {
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty { dst_span: head });
|
||||
}
|
||||
input.map(
|
||||
move |value| operate(value, head),
|
||||
engine_state.ctrlc.clone(),
|
||||
)
|
||||
input.map(move |value| operate(value, head), engine_state.signals())
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
|
@ -44,7 +44,7 @@ impl Command for SubCommand {
|
||||
}
|
||||
input.map(
|
||||
move |value| operate(value, head, use_degrees),
|
||||
engine_state.ctrlc.clone(),
|
||||
engine_state.signals(),
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -41,10 +41,7 @@ impl Command for SubCommand {
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty { dst_span: head });
|
||||
}
|
||||
input.map(
|
||||
move |value| operate(value, head),
|
||||
engine_state.ctrlc.clone(),
|
||||
)
|
||||
input.map(move |value| operate(value, head), engine_state.signals())
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
|
@ -140,7 +140,7 @@ fn operate(
|
||||
ret
|
||||
}
|
||||
},
|
||||
engine_state.ctrlc.clone(),
|
||||
engine_state.signals(),
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -88,7 +88,7 @@ pub fn operate(
|
||||
cell_paths,
|
||||
};
|
||||
|
||||
general_operate(action, args, input, call.head, engine_state.ctrlc.clone())
|
||||
general_operate(action, args, input, call.head, engine_state.signals())
|
||||
}
|
||||
|
||||
fn action(
|
||||
|
@ -220,7 +220,7 @@ fn format(
|
||||
}
|
||||
}
|
||||
|
||||
Ok(ListStream::new(list.into_iter(), head_span, engine_state.ctrlc.clone()).into())
|
||||
Ok(ListStream::new(list.into_iter(), head_span, engine_state.signals().clone()).into())
|
||||
}
|
||||
// Unwrapping this ShellError is a bit unfortunate.
|
||||
// Ideally, its Span would be preserved.
|
||||
|
@ -44,7 +44,7 @@ where
|
||||
case_operation,
|
||||
cell_paths,
|
||||
};
|
||||
general_operate(action, args, input, call.head, engine_state.ctrlc.clone())
|
||||
general_operate(action, args, input, call.head, engine_state.signals())
|
||||
}
|
||||
|
||||
fn action<F>(input: &Value, args: &Arguments<F>, head: Span) -> Value
|
||||
|
Reference in New Issue
Block a user