summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAram Drevekenin <aram@poor.dev>2024-03-27 19:53:03 +0100
committerAram Drevekenin <aram@poor.dev>2024-03-27 19:53:03 +0100
commit3cb1ff9946677f8246acfbab5454733e6c32fc0b (patch)
treec077c4fdedb48d18c9e933c76ef8e97850d59bed
parentc83b6cc38d5915f186789721d0261df3c80a0e49 (diff)
feat(plugins): add api to close current plugin instanceclose_self
-rw-r--r--zellij-server/src/plugins/zellij_exports.rs17
-rw-r--r--zellij-tile/src/shim.rs8
-rw-r--r--zellij-utils/assets/prost/api.plugin_command.rs3
-rw-r--r--zellij-utils/src/data.rs1
-rw-r--r--zellij-utils/src/plugin_api/plugin_command.proto1
-rw-r--r--zellij-utils/src/plugin_api/plugin_command.rs8
6 files changed, 38 insertions, 0 deletions
diff --git a/zellij-server/src/plugins/zellij_exports.rs b/zellij-server/src/plugins/zellij_exports.rs
index 1f65fad54..cc0752ff8 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<ForeignFunctionEnv>) {
},
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 532a5fafe..2f00a8a2b 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 ca067fd97..3bf49d36d 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 78fbbd770..b0e0a8861 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 b05c8ac8a..b961593e7 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 232a9f2dc..080dd9e28 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<ProtobufPluginCommand> 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<PluginCommand> for ProtobufPluginCommand {
name: CommandName::DumpSessionLayout as i32,
payload: None,
}),
+ PluginCommand::CloseSelf => Ok(ProtobufPluginCommand {
+ name: CommandName::CloseSelf as i32,
+ payload: None,
+ }),
}
}
}