working end-to-end! bunch of clean up to go

This commit is contained in:
Andy Gayton 2024-06-22 06:53:10 -04:00
parent 34acc251ff
commit 621241c731
2 changed files with 13 additions and 3 deletions

View File

@ -11,7 +11,7 @@ use nu_plugin_protocol::{
ProtocolInfo, ProtocolInfo,
}; };
use nu_protocol::{ use nu_protocol::{
engine::Closure, Config, LabeledError, PipelineData, PluginSignature, ShellError, Span, engine::{ctrlc, Closure}, Config, LabeledError, PipelineData, PluginSignature, ShellError, Span,
Spanned, Value, Spanned, Value,
}; };
use std::{ use std::{
@ -59,6 +59,8 @@ struct EngineInterfaceState {
mpsc::Sender<(EngineCallId, mpsc::Sender<EngineCallResponse<PipelineData>>)>, mpsc::Sender<(EngineCallId, mpsc::Sender<EngineCallResponse<PipelineData>>)>,
/// The synchronized output writer /// The synchronized output writer
writer: Box<dyn PluginWrite<PluginOutput>>, writer: Box<dyn PluginWrite<PluginOutput>>,
/// todo: docs
ctrlc_handlers: ctrlc::Handlers,
} }
impl std::fmt::Debug for EngineInterfaceState { impl std::fmt::Debug for EngineInterfaceState {
@ -112,6 +114,7 @@ impl EngineInterfaceManager {
stream_id_sequence: Sequence::default(), stream_id_sequence: Sequence::default(),
engine_call_subscription_sender: subscription_tx, engine_call_subscription_sender: subscription_tx,
writer: Box::new(writer), writer: Box::new(writer),
ctrlc_handlers: ctrlc::Handlers::new(),
}), }),
protocol_info_mut, protocol_info_mut,
plugin_call_sender: Some(plug_tx), plugin_call_sender: Some(plug_tx),
@ -323,7 +326,7 @@ impl InterfaceManager for EngineInterfaceManager {
self.send_engine_call_response(id, response) self.send_engine_call_response(id, response)
} }
PluginInput::Ctrlc => { PluginInput::Ctrlc => {
eprintln!("Received Ctrl-C!"); self.state.ctrlc_handlers.run();
Ok(()) Ok(())
} }
} }
@ -498,6 +501,11 @@ impl EngineInterface {
self.state.writer.is_stdout() self.state.writer.is_stdout()
} }
/// todo: docs
pub fn register_ctrlc_handler(&self, handler: ctrlc::Handler) -> ctrlc::Guard {
self.state.ctrlc_handlers.add(handler)
}
/// Get the full shell configuration from the engine. As this is quite a large object, it is /// Get the full shell configuration from the engine. As this is quite a large object, it is
/// provided on request only. /// provided on request only.
/// ///

View File

@ -4,7 +4,7 @@ use std::sync::{
Arc, Mutex, Arc, Mutex,
}; };
type Handler = Box<dyn Fn() + Send + Sync>; pub type Handler = Box<dyn Fn() + Send + Sync>;
#[derive(Clone)] #[derive(Clone)]
pub struct Handlers { pub struct Handlers {
@ -20,6 +20,7 @@ pub struct Guard {
impl Drop for Guard { impl Drop for Guard {
fn drop(&mut self) { fn drop(&mut self) {
eprintln!("Dropping guard: {:?}", self);
if let Ok(mut handlers) = self.handlers.lock() { if let Ok(mut handlers) = self.handlers.lock() {
handlers.retain(|(id, _)| *id != self.id); handlers.retain(|(id, _)| *id != self.id);
} }
@ -44,6 +45,7 @@ impl Handlers {
if let Ok(mut handlers) = self.handlers.lock() { if let Ok(mut handlers) = self.handlers.lock() {
handlers.push((id, handler)); handlers.push((id, handler));
} }
eprintln!("Adding guard: {:?}", id);
Guard { Guard {
id, id,
handlers: Arc::clone(&self.handlers), handlers: Arc::clone(&self.handlers),