Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugins): add api to close current plugin instance #3228

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
}),
}
}
}
Loading