diff options
| author | Aram Drevekenin <aram@poor.dev> | 2024-10-22 15:27:40 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-22 15:27:40 +0200 |
| commit | d671ab650e231d353a05a2808c0984e952de38c4 (patch) | |
| tree | 38a0604f9b4b20adfed5cf3e8324c59fbdf1ff2b /zellij-server/src/plugins | |
| parent | docs(changelog): wasmtime patch upgrade (diff) | |
| download | sl0ppij-d671ab650e231d353a05a2808c0984e952de38c4.tar.gz sl0ppij-d671ab650e231d353a05a2808c0984e952de38c4.tar.xz sl0ppij-d671ab650e231d353a05a2808c0984e952de38c4.zip | |
feat(plugins): add API to list clients, their focused panes and running commands/plugins (#3687)
* fix(list-clients): properly show client info after a tab was closed
* feat(plugins): add API to list clients, their focused panes and running commands/plugins
* style(fmt): rustfmt
Diffstat (limited to 'zellij-server/src/plugins')
4 files changed, 125 insertions, 2 deletions
diff --git a/zellij-server/src/plugins/mod.rs b/zellij-server/src/plugins/mod.rs index 3a8caec6..783f4a4f 100644 --- a/zellij-server/src/plugins/mod.rs +++ b/zellij-server/src/plugins/mod.rs @@ -26,7 +26,7 @@ use wasm_bridge::WasmBridge; use zellij_utils::{ async_std::{channel, future::timeout, task}, data::{ - Event, EventType, InputMode, MessageToPlugin, PermissionStatus, PermissionType, + ClientInfo, Event, EventType, InputMode, MessageToPlugin, PermissionStatus, PermissionType, PipeMessage, PipeSource, PluginCapabilities, }, errors::{prelude::*, ContextType, PluginContext}, @@ -158,6 +158,7 @@ pub enum PluginInstruction { file_path: Option<PathBuf>, }, WatchFilesystem, + ListClientsToPlugin(SessionLayoutMetadata, PluginId, ClientId), Exit, } @@ -203,6 +204,7 @@ impl From<&PluginInstruction> for PluginContext { PluginInstruction::FailedToWriteConfigToDisk { .. } => { PluginContext::FailedToWriteConfigToDisk }, + PluginInstruction::ListClientsToPlugin(..) => PluginContext::ListClientsToPlugin, } } } @@ -607,6 +609,35 @@ pub(crate) fn plugin_thread_main( }, } }, + PluginInstruction::ListClientsToPlugin( + mut session_layout_metadata, + plugin_id, + client_id, + ) => { + populate_session_layout_metadata( + &mut session_layout_metadata, + &wasm_bridge, + &plugin_aliases, + ); + let mut clients_metadata = session_layout_metadata.all_clients_metadata(); + let mut client_list_for_plugin = vec![]; + let default_editor = session_layout_metadata.default_editor.clone(); + for (client_metadata_id, client_metadata) in clients_metadata.iter_mut() { + let is_current_client = client_metadata_id == &client_id; + client_list_for_plugin.push(ClientInfo::new( + *client_metadata_id, + client_metadata.get_pane_id().into(), + client_metadata.stringify_command(&default_editor), + is_current_client, + )); + } + let updates = vec![( + Some(plugin_id), + Some(client_id), + Event::ListClients(client_list_for_plugin), + )]; + wasm_bridge.update_plugins(updates, shutdown_send.clone())?; + }, PluginInstruction::LogLayoutToHd(mut session_layout_metadata) => { populate_session_layout_metadata( &mut session_layout_metadata, diff --git a/zellij-server/src/plugins/unit/plugin_tests.rs b/zellij-server/src/plugins/unit/plugin_tests.rs index 0750c0dd..63d88115 100644 --- a/zellij-server/src/plugins/unit/plugin_tests.rs +++ b/zellij-server/src/plugins/unit/plugin_tests.rs @@ -8551,3 +8551,74 @@ pub fn rebind_keys_plugin_command() { .clone(); assert_snapshot!(format!("{:#?}", rebind_event)); } + +#[test] +#[ignore] +pub fn list_clients_plugin_command() { + let temp_folder = tempdir().unwrap(); // placed explicitly in the test scope because its + // destructor removes the directory + let plugin_host_folder = PathBuf::from(temp_folder.path()); + let cache_path = plugin_host_folder.join("permissions_test.kdl"); + let (plugin_thread_sender, screen_receiver, teardown) = + create_plugin_thread(Some(plugin_host_folder)); + let plugin_should_float = Some(false); + let plugin_title = Some("test_plugin".to_owned()); + let run_plugin = RunPluginOrAlias::RunPlugin(RunPlugin { + _allow_exec_host_cmd: false, + location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), + configuration: Default::default(), + ..Default::default() + }); + let tab_index = 1; + let client_id = 1; + let size = Size { + cols: 121, + rows: 20, + }; + let received_screen_instructions = Arc::new(Mutex::new(vec![])); + let screen_thread = grant_permissions_and_log_actions_in_thread!( + received_screen_instructions, + ScreenInstruction::ListClientsToPlugin, + screen_receiver, + 1, + &PermissionType::ChangeApplicationState, + cache_path, + plugin_thread_sender, + client_id + ); + + let _ = plugin_thread_sender.send(PluginInstruction::AddClient(client_id)); + let _ = plugin_thread_sender.send(PluginInstruction::Load( + plugin_should_float, + false, + plugin_title, + run_plugin, + Some(tab_index), + None, + client_id, + size, + None, + false, + )); + std::thread::sleep(std::time::Duration::from_millis(500)); + let _ = plugin_thread_sender.send(PluginInstruction::Update(vec![( + None, + Some(client_id), + Event::Key(KeyWithModifier::new(BareKey::Char('z')).with_alt_modifier()), // this triggers the enent in the fixture plugin + )])); + screen_thread.join().unwrap(); // this might take a while if the cache is cold + teardown(); + let list_clients_instruction = received_screen_instructions + .lock() + .unwrap() + .iter() + .find_map(|i| { + if let ScreenInstruction::ListClientsToPlugin(..) = i { + Some(i.clone()) + } else { + None + } + }) + .unwrap(); + assert_snapshot!(format!("{:#?}", list_clients_instruction)); +} diff --git a/zellij-server/src/plugins/unit/snapshots/zellij_server__plugins__plugin_tests__list_clients_plugin_command.snap b/zellij-server/src/plugins/unit/snapshots/zellij_server__plugins__plugin_tests__list_clients_plugin_command.snap new file mode 100644 index 00000000..80f38132 --- /dev/null +++ b/zellij-server/src/plugins/unit/snapshots/zellij_server__plugins__plugin_tests__list_clients_plugin_command.snap @@ -0,0 +1,9 @@ +--- +source: zellij-server/src/plugins/./unit/plugin_tests.rs +assertion_line: 8623 +expression: "format!(\"{:#?}\", list_clients_instruction)" +--- +ListClientsToPlugin( + 0, + 1, +) diff --git a/zellij-server/src/plugins/zellij_exports.rs b/zellij-server/src/plugins/zellij_exports.rs index 5dd37485..d308689b 100644 --- a/zellij-server/src/plugins/zellij_exports.rs +++ b/zellij-server/src/plugins/zellij_exports.rs @@ -351,6 +351,7 @@ fn host_run_plugin_command(caller: Caller<'_, PluginEnv>) { keys_to_unbind, write_config_to_disk, } => rebind_keys(env, keys_to_rebind, keys_to_unbind, write_config_to_disk)?, + PluginCommand::ListClients => list_clients(env), }, (PermissionStatus::Denied, permission) => { log::error!( @@ -1474,6 +1475,15 @@ fn dump_session_layout(env: &PluginEnv) { .map(|sender| sender.send(ScreenInstruction::DumpLayoutToPlugin(env.plugin_id))); } +fn list_clients(env: &PluginEnv) { + let _ = env.senders.to_screen.as_ref().map(|sender| { + sender.send(ScreenInstruction::ListClientsToPlugin( + env.plugin_id, + env.client_id, + )) + }); +} + fn scan_host_folder(env: &PluginEnv, folder_to_scan: PathBuf) { if !folder_to_scan.starts_with("/host") { log::error!( @@ -1897,7 +1907,9 @@ fn check_command_permission( | PluginCommand::BlockCliPipeInput(..) | PluginCommand::CliPipeOutput(..) => PermissionType::ReadCliPipes, PluginCommand::MessageToPlugin(..) => PermissionType::MessageAndLaunchOtherPlugins, - PluginCommand::DumpSessionLayout => PermissionType::ReadApplicationState, + PluginCommand::ListClients | PluginCommand::DumpSessionLayout => { + PermissionType::ReadApplicationState + }, PluginCommand::RebindKeys { .. } | PluginCommand::Reconfigure(..) => { PermissionType::Reconfigure }, |
