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:
Ian Manske
2024-07-07 22:29:01 +00:00
committed by GitHub
parent c6b6b1b7a8
commit 399a7c8836
246 changed files with 1332 additions and 1234 deletions

View File

@ -102,9 +102,7 @@ fn expanded_table_list(input: &[Value], cfg: Cfg<'_>) -> TableResult {
}
for (row, item) in input.iter().enumerate() {
if nu_utils::ctrl_c::was_pressed(&cfg.opts.ctrlc) {
return Ok(None);
}
cfg.opts.signals.check(cfg.opts.span)?;
if let Value::Error { error, .. } = item {
return Err(*error.clone());
@ -143,9 +141,7 @@ fn expanded_table_list(input: &[Value], cfg: Cfg<'_>) -> TableResult {
}
for (row, item) in input.iter().enumerate() {
if nu_utils::ctrl_c::was_pressed(&cfg.opts.ctrlc) {
return Ok(None);
}
cfg.opts.signals.check(cfg.opts.span)?;
if let Value::Error { error, .. } = item {
return Err(*error.clone());
@ -230,9 +226,7 @@ fn expanded_table_list(input: &[Value], cfg: Cfg<'_>) -> TableResult {
}
for (row, item) in input.iter().enumerate() {
if nu_utils::ctrl_c::was_pressed(&cfg.opts.ctrlc) {
return Ok(None);
}
cfg.opts.signals.check(cfg.opts.span)?;
if let Value::Error { error, .. } = item {
return Err(*error.clone());
@ -353,9 +347,7 @@ fn expanded_table_kv(record: &Record, cfg: Cfg<'_>) -> StringResult {
let mut data = Vec::with_capacity(record.len());
for (key, value) in record {
if nu_utils::ctrl_c::was_pressed(&cfg.opts.ctrlc) {
return Ok(None);
}
cfg.opts.signals.check(cfg.opts.span)?;
let (value, is_expanded) = match expand_table_value(value, value_width, &cfg)? {
Some(val) => val,

View File

@ -43,9 +43,7 @@ fn create_table(input: &[Value], opts: TableOpts<'_>) -> Result<Option<String>,
fn kv_table(record: &Record, opts: TableOpts<'_>) -> StringResult {
let mut data = vec![Vec::with_capacity(2); record.len()];
for ((column, value), row) in record.iter().zip(data.iter_mut()) {
if nu_utils::ctrl_c::was_pressed(&opts.ctrlc) {
return Ok(None);
}
opts.signals.check(opts.span)?;
let value = nu_value_to_string_colored(value, opts.config, opts.style_computer);
@ -123,9 +121,7 @@ fn to_table_with_header(
}
for (row, item) in input.iter().enumerate() {
if nu_utils::ctrl_c::was_pressed(&opts.ctrlc) {
return Ok(None);
}
opts.signals.check(opts.span)?;
if let Value::Error { error, .. } = item {
return Err(*error.clone());
@ -158,9 +154,7 @@ fn to_table_with_no_header(
table.set_index_style(get_index_style(opts.style_computer));
for (row, item) in input.iter().enumerate() {
if nu_utils::ctrl_c::was_pressed(&opts.ctrlc) {
return Ok(None);
}
opts.signals.check(opts.span)?;
if let Value::Error { error, .. } = item {
return Err(*error.clone());

View File

@ -8,8 +8,7 @@ pub use general::JustTable;
use crate::{common::INDEX_COLUMN_NAME, NuTable};
use nu_color_config::StyleComputer;
use nu_protocol::{Config, Span, TableIndexMode, TableMode};
use std::sync::{atomic::AtomicBool, Arc};
use nu_protocol::{Config, Signals, Span, TableIndexMode, TableMode};
pub struct TableOutput {
pub table: NuTable,
@ -29,7 +28,7 @@ impl TableOutput {
#[derive(Debug, Clone)]
pub struct TableOpts<'a> {
ctrlc: Option<Arc<AtomicBool>>,
signals: &'a Signals,
config: &'a Config,
style_computer: &'a StyleComputer<'a>,
span: Span,
@ -45,7 +44,7 @@ impl<'a> TableOpts<'a> {
pub fn new(
config: &'a Config,
style_computer: &'a StyleComputer<'a>,
ctrlc: Option<Arc<AtomicBool>>,
signals: &'a Signals,
span: Span,
width: usize,
indent: (usize, usize),
@ -54,7 +53,7 @@ impl<'a> TableOpts<'a> {
index_remove: bool,
) -> Self {
Self {
ctrlc,
signals,
config,
style_computer,
span,