diff options
author | Aram Drevekenin <aram@poor.dev> | 2024-03-26 18:44:56 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-26 18:44:56 +0100 |
commit | 2eaa50cc44284bdfd47a98770625f380c15fd51d (patch) | |
tree | e86909cdfce996d08442c5152fa4bacdbf203745 /zellij-server | |
parent | b24dd87b8044be59a9897bb19604deee85b236b0 (diff) |
feat(plugins): add api to dump the current session layout to a plugin (#3227)
Diffstat (limited to 'zellij-server')
-rw-r--r-- | zellij-server/src/plugins/mod.rs | 29 | ||||
-rw-r--r-- | zellij-server/src/plugins/zellij_exports.rs | 10 | ||||
-rw-r--r-- | zellij-server/src/pty.rs | 16 | ||||
-rw-r--r-- | zellij-server/src/screen.rs | 18 |
4 files changed, 71 insertions, 2 deletions
diff --git a/zellij-server/src/plugins/mod.rs b/zellij-server/src/plugins/mod.rs index 44e88ae0f..e55168939 100644 --- a/zellij-server/src/plugins/mod.rs +++ b/zellij-server/src/plugins/mod.rs @@ -37,6 +37,7 @@ use zellij_utils::{ }, ipc::ClientAttributes, pane_size::Size, + session_serialization, }; pub type PluginId = u32; @@ -104,6 +105,7 @@ pub enum PluginInstruction { Option<PathBuf>, ), DumpLayout(SessionLayoutMetadata, ClientId), + DumpLayoutToPlugin(SessionLayoutMetadata, PluginId), LogLayoutToHd(SessionLayoutMetadata), CliPipe { pipe_id: String, @@ -178,6 +180,7 @@ impl From<&PluginInstruction> for PluginContext { PluginInstruction::UnblockCliPipes { .. } => PluginContext::UnblockCliPipes, PluginInstruction::WatchFilesystem => PluginContext::WatchFilesystem, PluginInstruction::KeybindPipe { .. } => PluginContext::KeybindPipe, + PluginInstruction::DumpLayoutToPlugin(..) => PluginContext::DumpLayoutToPlugin, } } } @@ -484,6 +487,32 @@ pub(crate) fn plugin_thread_main( client_id, ))); }, + PluginInstruction::DumpLayoutToPlugin(mut session_layout_metadata, plugin_id) => { + populate_session_layout_metadata(&mut session_layout_metadata, &wasm_bridge); + match session_serialization::serialize_session_layout( + session_layout_metadata.into(), + ) { + Ok((layout, _pane_contents)) => { + let updates = vec![( + Some(plugin_id), + None, + Event::CustomMessage("session_layout".to_owned(), layout), + )]; + wasm_bridge.update_plugins(updates, shutdown_send.clone())?; + }, + Err(e) => { + let updates = vec![( + Some(plugin_id), + None, + Event::CustomMessage( + "session_layout_error".to_owned(), + format!("{}", e), + ), + )]; + wasm_bridge.update_plugins(updates, shutdown_send.clone())?; + }, + } + }, PluginInstruction::LogLayoutToHd(mut session_layout_metadata) => { populate_session_layout_metadata(&mut session_layout_metadata, &wasm_bridge); drop( diff --git a/zellij-server/src/plugins/zellij_exports.rs b/zellij-server/src/plugins/zellij_exports.rs index 65c18e16b..1f65fad54 100644 --- a/zellij-server/src/plugins/zellij_exports.rs +++ b/zellij-server/src/plugins/zellij_exports.rs @@ -263,6 +263,7 @@ fn host_run_plugin_command(env: FunctionEnvMut<ForeignFunctionEnv>) { scan_host_folder(env, folder_to_scan) }, PluginCommand::WatchFilesystem => watch_filesystem(env), + PluginCommand::DumpSessionLayout => dump_session_layout(env), }, (PermissionStatus::Denied, permission) => { log::error!( @@ -1340,6 +1341,14 @@ fn watch_filesystem(env: &ForeignFunctionEnv) { .map(|sender| sender.send(PluginInstruction::WatchFilesystem)); } +fn dump_session_layout(env: &ForeignFunctionEnv) { + let _ = env.plugin_env.senders.to_screen.as_ref().map(|sender| { + sender.send(ScreenInstruction::DumpLayoutToPlugin( + env.plugin_env.plugin_id, + )) + }); +} + fn scan_host_folder(env: &ForeignFunctionEnv, folder_to_scan: PathBuf) { if !folder_to_scan.starts_with("/host") { log::error!( @@ -1542,6 +1551,7 @@ fn check_command_permission( | PluginCommand::BlockCliPipeInput(..) | PluginCommand::CliPipeOutput(..) => PermissionType::ReadCliPipes, PluginCommand::MessageToPlugin(..) => PermissionType::MessageAndLaunchOtherPlugins, + PluginCommand::DumpSessionLayout => PermissionType::ReadApplicationState, _ => return (PermissionStatus::Granted, None), }; diff --git a/zellij-server/src/pty.rs b/zellij-server/src/pty.rs index cc219f372..c4e810536 100644 --- a/zellij-server/src/pty.rs +++ b/zellij-server/src/pty.rs @@ -2,7 +2,7 @@ use crate::background_jobs::BackgroundJob; use crate::terminal_bytes::TerminalBytes; use crate::{ panes::PaneId, - plugins::PluginInstruction, + plugins::{PluginId, PluginInstruction}, screen::ScreenInstruction, session_layout_metadata::SessionLayoutMetadata, thread_bus::{Bus, ThreadSenders}, @@ -77,6 +77,7 @@ pub enum PtyInstruction { ClientTabIndexOrPaneId, ), // String is an optional pane name DumpLayout(SessionLayoutMetadata, ClientId), + DumpLayoutToPlugin(SessionLayoutMetadata, PluginId), LogLayoutToHd(SessionLayoutMetadata), FillPluginCwd( Option<bool>, // should float @@ -110,6 +111,7 @@ impl From<&PtyInstruction> for PtyContext { PtyInstruction::DropToShellInPane { .. } => PtyContext::DropToShellInPane, PtyInstruction::SpawnInPlaceTerminal(..) => PtyContext::SpawnInPlaceTerminal, PtyInstruction::DumpLayout(..) => PtyContext::DumpLayout, + PtyInstruction::DumpLayoutToPlugin(..) => PtyContext::DumpLayoutToPlugin, PtyInstruction::LogLayoutToHd(..) => PtyContext::LogLayoutToHd, PtyInstruction::FillPluginCwd(..) => PtyContext::FillPluginCwd, PtyInstruction::Exit => PtyContext::Exit, @@ -630,6 +632,18 @@ pub(crate) fn pty_thread_main(mut pty: Pty, layout: Box<Layout>) -> Result<()> { }, } }, + PtyInstruction::DumpLayoutToPlugin(mut session_layout_metadata, plugin_id) => { + let err_context = || format!("Failed to dump layout"); + pty.populate_session_layout_metadata(&mut session_layout_metadata); + pty.bus + .senders + .send_to_plugin(PluginInstruction::DumpLayoutToPlugin( + session_layout_metadata, + plugin_id, + )) + .with_context(err_context) + .non_fatal(); + }, PtyInstruction::LogLayoutToHd(mut session_layout_metadata) => { let err_context = || format!("Failed to dump layout"); pty.populate_session_layout_metadata(&mut session_layout_metadata); diff --git a/zellij-server/src/screen.rs b/zellij-server/src/screen.rs index 35ec8fee5..5384987de 100644 --- a/zellij-server/src/screen.rs +++ b/zellij-server/src/screen.rs @@ -36,7 +36,7 @@ use crate::{ output::Output, panes::sixel::SixelImageStore, panes::PaneId, - plugins::{PluginInstruction, PluginRenderAsset}, + plugins::{PluginId, PluginInstruction, PluginRenderAsset}, pty::{ClientTabIndexOrPaneId, PtyInstruction, VteBytes}, tab::Tab, thread_bus::Bus, @@ -180,6 +180,7 @@ pub enum ScreenInstruction { DumpScreen(String, ClientId, bool), DumpLayout(Option<PathBuf>, ClientId), // PathBuf is the default configured // shell + DumpLayoutToPlugin(PluginId), EditScrollback(ClientId), ScrollUp(ClientId), ScrollUpAt(Position, ClientId), @@ -421,6 +422,7 @@ impl From<&ScreenInstruction> for ScreenContext { ScreenInstruction::ClearScreen(..) => ScreenContext::ClearScreen, ScreenInstruction::DumpScreen(..) => ScreenContext::DumpScreen, ScreenInstruction::DumpLayout(..) => ScreenContext::DumpLayout, + ScreenInstruction::DumpLayoutToPlugin(..) => ScreenContext::DumpLayoutToPlugin, ScreenInstruction::EditScrollback(..) => ScreenContext::EditScrollback, ScreenInstruction::ScrollUp(..) => ScreenContext::ScrollUp, ScreenInstruction::ScrollDown(..) => ScreenContext::ScrollDown, @@ -2630,6 +2632,20 @@ pub(crate) fn screen_thread_main( )) .with_context(err_context)?; }, + ScreenInstruction::DumpLayoutToPlugin(plugin_id) => { + let err_context = || format!("Failed to dump layout"); + let session_layout_metadata = + screen.get_layout_metadata(screen.default_shell.clone()); + screen + .bus + .senders + .send_to_pty(PtyInstruction::DumpLayoutToPlugin( + session_layout_metadata, + plugin_id, + )) + .with_context(err_context) + .non_fatal(); + }, ScreenInstruction::EditScrollback(client_id) => { active_tab_and_connected_client_id!( screen, |