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,
};
use nu_protocol::{
engine::Closure, Config, LabeledError, PipelineData, PluginSignature, ShellError, Span,
engine::{ctrlc, Closure}, Config, LabeledError, PipelineData, PluginSignature, ShellError, Span,
Spanned, Value,
};
use std::{
@ -59,6 +59,8 @@ struct EngineInterfaceState {
mpsc::Sender<(EngineCallId, mpsc::Sender<EngineCallResponse<PipelineData>>)>,
/// The synchronized output writer
writer: Box<dyn PluginWrite<PluginOutput>>,
/// todo: docs
ctrlc_handlers: ctrlc::Handlers,
}
impl std::fmt::Debug for EngineInterfaceState {
@ -112,6 +114,7 @@ impl EngineInterfaceManager {
stream_id_sequence: Sequence::default(),
engine_call_subscription_sender: subscription_tx,
writer: Box::new(writer),
ctrlc_handlers: ctrlc::Handlers::new(),
}),
protocol_info_mut,
plugin_call_sender: Some(plug_tx),
@ -323,7 +326,7 @@ impl InterfaceManager for EngineInterfaceManager {
self.send_engine_call_response(id, response)
}
PluginInput::Ctrlc => {
eprintln!("Received Ctrl-C!");
self.state.ctrlc_handlers.run();
Ok(())
}
}
@ -498,6 +501,11 @@ impl EngineInterface {
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
/// provided on request only.
///

View File

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