Skip to content

Commit

Permalink
feat(plugins): add api to close current plugin instance (#3228)
Browse files Browse the repository at this point in the history
  • Loading branch information
imsnif authored Mar 27, 2024
1 parent c83b6cc commit 9d2e7fe
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 0 deletions.
17 changes: 17 additions & 0 deletions zellij-server/src/plugins/zellij_exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down Expand Up @@ -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 = || {
Expand Down
8 changes: 8 additions & 0 deletions zellij-tile/src/shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions zellij-utils/assets/prost/api.plugin_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -604,6 +606,7 @@ impl CommandName {
"ScanHostFolder" => Some(Self::ScanHostFolder),
"WatchFilesystem" => Some(Self::WatchFilesystem),
"DumpSessionLayout" => Some(Self::DumpSessionLayout),
"CloseSelf" => Some(Self::CloseSelf),
_ => None,
}
}
Expand Down
1 change: 1 addition & 0 deletions zellij-utils/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1379,4 +1379,5 @@ pub enum PluginCommand {
ScanHostFolder(PathBuf), // TODO: rename to ScanHostFolder
WatchFilesystem,
DumpSessionLayout,
CloseSelf,
}
1 change: 1 addition & 0 deletions zellij-utils/src/plugin_api/plugin_command.proto
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ enum CommandName {
ScanHostFolder = 82;
WatchFilesystem = 83;
DumpSessionLayout = 84;
CloseSelf = 85;
}

message PluginCommand {
Expand Down
8 changes: 8 additions & 0 deletions zellij-utils/src/plugin_api/plugin_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
}
Expand Down Expand Up @@ -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,
}),
}
}
}

0 comments on commit 9d2e7fe

Please sign in to comment.