Add Interrupt
struct
This commit is contained in:
parent
de2b752771
commit
8cb842eef8
|
@ -1187,6 +1187,13 @@ pub enum ShellError {
|
|||
span: Option<Span>,
|
||||
},
|
||||
|
||||
/// Operation interrupted
|
||||
#[error("Operation interrupted")]
|
||||
Interrupted {
|
||||
#[label("This operation was interrupted")]
|
||||
span: Span,
|
||||
},
|
||||
|
||||
/// Operation interrupted by user
|
||||
#[error("Operation interrupted by user")]
|
||||
InterruptedByUser {
|
||||
|
|
35
crates/nu-protocol/src/pipeline/interrupt.rs
Normal file
35
crates/nu-protocol/src/pipeline/interrupt.rs
Normal file
|
@ -0,0 +1,35 @@
|
|||
use crate::{ShellError, Span};
|
||||
use std::sync::{
|
||||
atomic::{AtomicBool, Ordering},
|
||||
Arc,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Interrupt {
|
||||
interrupt: Option<Arc<AtomicBool>>,
|
||||
}
|
||||
|
||||
impl Interrupt {
|
||||
pub fn new(ctrlc: Arc<AtomicBool>) -> Self {
|
||||
Self {
|
||||
interrupt: Some(ctrlc),
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn empty() -> Self {
|
||||
Self { interrupt: None }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn poll(&self, span: Span) -> Result<(), ShellError> {
|
||||
if self
|
||||
.interrupt
|
||||
.as_deref()
|
||||
.is_some_and(|b| b.load(Ordering::Relaxed))
|
||||
{
|
||||
Err(ShellError::Interrupted { span })
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,10 +1,12 @@
|
|||
pub mod byte_stream;
|
||||
mod interrupt;
|
||||
pub mod list_stream;
|
||||
mod metadata;
|
||||
mod out_dest;
|
||||
mod pipeline_data;
|
||||
|
||||
pub use byte_stream::*;
|
||||
pub use interrupt::*;
|
||||
pub use list_stream::*;
|
||||
pub use metadata::*;
|
||||
pub use out_dest::*;
|
||||
|
|
Loading…
Reference in New Issue
Block a user