From 3cb1ff9946677f8246acfbab5454733e6c32fc0b Mon Sep 17 00:00:00 2001 From: Aram Drevekenin Date: Wed, 27 Mar 2024 19:53:03 +0100 Subject: [PATCH] feat(plugins): add api to close current plugin instance --- zellij-server/src/plugins/zellij_exports.rs | 17 +++++++++++++++++ zellij-tile/src/shim.rs | 8 ++++++++ zellij-utils/assets/prost/api.plugin_command.rs | 3 +++ zellij-utils/src/data.rs | 1 + .../src/plugin_api/plugin_command.proto | 1 + zellij-utils/src/plugin_api/plugin_command.rs | 8 ++++++++ 6 files changed, 38 insertions(+) diff --git a/zellij-server/src/plugins/zellij_exports.rs b/zellij-server/src/plugins/zellij_exports.rs index 1f65fad549..cc0752ff8f 100644 --- a/zellij-server/src/plugins/zellij_exports.rs +++ b/zellij-server/src/plugins/zellij_exports.rs @@ -264,6 +264,7 @@ fn host_run_plugin_command(env: FunctionEnvMut) { }, PluginCommand::WatchFilesystem => watch_filesystem(env), PluginCommand::DumpSessionLayout => dump_session_layout(env), + PluginCommand::CloseSelf => close_self(env), }, (PermissionStatus::Denied, permission) => { log::error!( @@ -813,6 +814,22 @@ fn show_self(env: &ForeignFunctionEnv, should_float_if_hidden: bool) { apply_action!(action, error_msg, env); } +fn close_self(env: &ForeignFunctionEnv) { + env.plugin_env + .senders + .send_to_screen(ScreenInstruction::ClosePane( + PaneId::Plugin(env.plugin_env.plugin_id), + None, + )) + .with_context(|| format!("failed to close self")) + .non_fatal(); + env.plugin_env + .senders + .send_to_plugin(PluginInstruction::Unload(env.plugin_env.plugin_id)) + .with_context(|| format!("failed to close self")) + .non_fatal(); +} + fn switch_to_mode(env: &ForeignFunctionEnv, input_mode: InputMode) { let action = Action::SwitchToMode(input_mode); let error_msg = || { diff --git a/zellij-tile/src/shim.rs b/zellij-tile/src/shim.rs index 532a5fafe4..2f00a8a2b1 100644 --- a/zellij-tile/src/shim.rs +++ b/zellij-tile/src/shim.rs @@ -249,6 +249,14 @@ pub fn show_self(should_float_if_hidden: bool) { unsafe { host_run_plugin_command() }; } +/// Close this plugin pane +pub fn close_self() { + let plugin_command = PluginCommand::CloseSelf; + let protobuf_plugin_command: ProtobufPluginCommand = plugin_command.try_into().unwrap(); + object_to_stdout(&protobuf_plugin_command.encode_to_vec()); + unsafe { host_run_plugin_command() }; +} + /// Switch to the specified Input Mode (eg. `Normal`, `Tab`, `Pane`) pub fn switch_to_input_mode(mode: &InputMode) { let plugin_command = PluginCommand::SwitchToMode(*mode); diff --git a/zellij-utils/assets/prost/api.plugin_command.rs b/zellij-utils/assets/prost/api.plugin_command.rs index ca067fd97f..3bf49d36d0 100644 --- a/zellij-utils/assets/prost/api.plugin_command.rs +++ b/zellij-utils/assets/prost/api.plugin_command.rs @@ -421,6 +421,7 @@ pub enum CommandName { ScanHostFolder = 82, WatchFilesystem = 83, DumpSessionLayout = 84, + CloseSelf = 85, } impl CommandName { /// String value of the enum field names used in the ProtoBuf definition. @@ -514,6 +515,7 @@ impl CommandName { CommandName::ScanHostFolder => "ScanHostFolder", CommandName::WatchFilesystem => "WatchFilesystem", CommandName::DumpSessionLayout => "DumpSessionLayout", + CommandName::CloseSelf => "CloseSelf", } } /// Creates an enum from field names used in the ProtoBuf definition. @@ -604,6 +606,7 @@ impl CommandName { "ScanHostFolder" => Some(Self::ScanHostFolder), "WatchFilesystem" => Some(Self::WatchFilesystem), "DumpSessionLayout" => Some(Self::DumpSessionLayout), + "CloseSelf" => Some(Self::CloseSelf), _ => None, } } diff --git a/zellij-utils/src/data.rs b/zellij-utils/src/data.rs index 78fbbd7701..b0e0a8861c 100644 --- a/zellij-utils/src/data.rs +++ b/zellij-utils/src/data.rs @@ -1379,4 +1379,5 @@ pub enum PluginCommand { ScanHostFolder(PathBuf), // TODO: rename to ScanHostFolder WatchFilesystem, DumpSessionLayout, + CloseSelf, } diff --git a/zellij-utils/src/plugin_api/plugin_command.proto b/zellij-utils/src/plugin_api/plugin_command.proto index b05c8ac8a8..b961593e7b 100644 --- a/zellij-utils/src/plugin_api/plugin_command.proto +++ b/zellij-utils/src/plugin_api/plugin_command.proto @@ -96,6 +96,7 @@ enum CommandName { ScanHostFolder = 82; WatchFilesystem = 83; DumpSessionLayout = 84; + CloseSelf = 85; } message PluginCommand { diff --git a/zellij-utils/src/plugin_api/plugin_command.rs b/zellij-utils/src/plugin_api/plugin_command.rs index 232a9f2dc4..080dd9e28b 100644 --- a/zellij-utils/src/plugin_api/plugin_command.rs +++ b/zellij-utils/src/plugin_api/plugin_command.rs @@ -871,6 +871,10 @@ impl TryFrom for PluginCommand { Some(_) => Err("DumpSessionLayout should have no payload, found a payload"), None => Ok(PluginCommand::DumpSessionLayout), }, + Some(CommandName::CloseSelf) => match protobuf_plugin_command.payload { + Some(_) => Err("CloseSelf should have no payload, found a payload"), + None => Ok(PluginCommand::CloseSelf), + }, None => Err("Unrecognized plugin command"), } } @@ -1389,6 +1393,10 @@ impl TryFrom for ProtobufPluginCommand { name: CommandName::DumpSessionLayout as i32, payload: None, }), + PluginCommand::CloseSelf => Ok(ProtobufPluginCommand { + name: CommandName::CloseSelf as i32, + payload: None, + }), } } }