diff options
| author | Aram Drevekenin <aram@poor.dev> | 2026-02-10 17:03:42 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-02-10 17:03:42 +0100 |
| commit | e160e9e7ee742f84d6f793ba4741cc422d3fae44 (patch) | |
| tree | c3473faad0c4914c64052c02d059142b1a7d8f1a | |
| parent | Improve plugin API ergonomics and add CLI goodies (#4690) (diff) | |
| download | sl0ppij-e160e9e7ee742f84d6f793ba4741cc422d3fae44.tar.gz sl0ppij-e160e9e7ee742f84d6f793ba4741cc422d3fae44.tar.xz sl0ppij-e160e9e7ee742f84d6f793ba4741cc422d3fae44.zip | |
Allow writing characters, bytes and keys to specific panes from the CLI (#4691)
* initial implementation
* fix tests
* implement zellij action send-keys
* rustfmt
* add test
* fix tests and add pr
21 files changed, 509 insertions, 35 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 433d748c..216f0d5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) * fix: prevent selection from scrolling up after double/triple-click (https://github.com/zellij-org/zellij/pull/4681) * refactor: canonicalize tab_id vs. tab_position and expose tab_id to plugins (https://github.com/zellij-org/zellij/pull/4685) * feat: `zellij action list-panes` + return pane/tab IDs from plugin and CLI methods creating them (https://github.com/zellij-org/zellij/pull/4690) +* feat: allow sending keys to other panes from the CLI (https://github.com/zellij-org/zellij/pull/4691) ## [0.43.1] - 2025-08-08 diff --git a/zellij-server/src/plugins/unit/snapshots/zellij_server__plugins__plugin_tests__write_chars_to_pane_id_plugin_command.snap b/zellij-server/src/plugins/unit/snapshots/zellij_server__plugins__plugin_tests__write_chars_to_pane_id_plugin_command.snap index 60904fab..7a1ccab1 100644 --- a/zellij-server/src/plugins/unit/snapshots/zellij_server__plugins__plugin_tests__write_chars_to_pane_id_plugin_command.snap +++ b/zellij-server/src/plugins/unit/snapshots/zellij_server__plugins__plugin_tests__write_chars_to_pane_id_plugin_command.snap @@ -1,6 +1,5 @@ --- source: zellij-server/src/plugins/./unit/plugin_tests.rs -assertion_line: 7269 expression: "format!(\"{:#?}\", rerun_command_pane_event)" --- Some( @@ -14,5 +13,14 @@ Some( Terminal( 2, ), + Some( + NotificationEnd { + channel: None, + exit_status: None, + unblock_condition: None, + affected_pane_id: None, + affected_tab_id: None, + }, + ), ), ) diff --git a/zellij-server/src/plugins/unit/snapshots/zellij_server__plugins__plugin_tests__write_to_pane_id_plugin_command.snap b/zellij-server/src/plugins/unit/snapshots/zellij_server__plugins__plugin_tests__write_to_pane_id_plugin_command.snap index 6d9b051e..90209815 100644 --- a/zellij-server/src/plugins/unit/snapshots/zellij_server__plugins__plugin_tests__write_to_pane_id_plugin_command.snap +++ b/zellij-server/src/plugins/unit/snapshots/zellij_server__plugins__plugin_tests__write_to_pane_id_plugin_command.snap @@ -1,6 +1,5 @@ --- source: zellij-server/src/plugins/./unit/plugin_tests.rs -assertion_line: 7198 expression: "format!(\"{:#?}\", rerun_command_pane_event)" --- Some( @@ -13,5 +12,14 @@ Some( Terminal( 2, ), + Some( + NotificationEnd { + channel: None, + exit_status: None, + unblock_condition: None, + affected_pane_id: None, + affected_tab_id: None, + }, + ), ), ) diff --git a/zellij-server/src/plugins/zellij_exports.rs b/zellij-server/src/plugins/zellij_exports.rs index abc78254..6968741a 100644 --- a/zellij-server/src/plugins/zellij_exports.rs +++ b/zellij-server/src/plugins/zellij_exports.rs @@ -3249,16 +3249,30 @@ fn get_pane_scrollback(env: &PluginEnv, pane_id: PaneId, get_full_scrollback: bo } fn write_to_pane_id(env: &PluginEnv, bytes: Vec<u8>, pane_id: PaneId) { - let _ = env - .senders - .send_to_screen(ScreenInstruction::WriteToPaneId(bytes, pane_id)); + let (completion_tx, completion_rx) = oneshot::channel(); + let send_result = env.senders.send_to_screen(ScreenInstruction::WriteToPaneId( + bytes, + pane_id, + Some(NotificationEnd::new(completion_tx)), + )); + if send_result.is_ok() { + let wait_forever = false; + let _ = wait_for_action_completion(completion_rx, "write_to_pane_id", wait_forever); + } } fn write_chars_to_pane_id(env: &PluginEnv, chars: String, pane_id: PaneId) { let bytes = chars.into_bytes(); - let _ = env - .senders - .send_to_screen(ScreenInstruction::WriteToPaneId(bytes, pane_id)); + let (completion_tx, completion_rx) = oneshot::channel(); + let send_result = env.senders.send_to_screen(ScreenInstruction::WriteToPaneId( + bytes, + pane_id, + Some(NotificationEnd::new(completion_tx)), + )); + if send_result.is_ok() { + let wait_forever = false; + let _ = wait_for_action_completion(completion_rx, "write_chars_to_pane_id", wait_forever); + } } fn send_sigint_to_pane_id(env: &PluginEnv, pane_id: PaneId) { diff --git a/zellij-server/src/pty_writer.rs b/zellij-server/src/pty_writer.rs index 017fed39..7cdfaadc 100644 --- a/zellij-server/src/pty_writer.rs +++ b/zellij-server/src/pty_writer.rs @@ -1,15 +1,16 @@ use zellij_utils::errors::{prelude::*, ContextType, PtyWriteContext}; +use crate::route::NotificationEnd; use crate::thread_bus::Bus; // we separate these instruction to a different thread because some programs get deadlocked if // you write into their STDIN while reading from their STDOUT (I'm looking at you, vim) // while the same has not been observed to happen with resizes, it could conceivably happen and we have this // here anyway, so -#[derive(Debug, Clone, Eq, PartialEq)] +#[derive(Debug, Clone)] pub enum PtyWriteInstruction { - Write(Vec<u8>, u32), - ResizePty(u32, u16, u16, Option<u16>, Option<u16>), // terminal_id, columns, rows, pixel width, pixel height + Write(Vec<u8>, u32, Option<NotificationEnd>), + ResizePty(u32, u16, u16, Option<u16>, Option<u16>), StartCachingResizes, ApplyCachedResizes, Exit, @@ -39,7 +40,7 @@ pub(crate) fn pty_writer_main(bus: Bus<PtyWriteInstruction>) -> Result<()> { .context("no OS input API found") .with_context(err_context)?; match event { - PtyWriteInstruction::Write(bytes, terminal_id) => { + PtyWriteInstruction::Write(bytes, terminal_id, _completion) => { os_input .write_to_tty_stdin(terminal_id, &bytes) .with_context(err_context) diff --git a/zellij-server/src/route.rs b/zellij-server/src/route.rs index c16600af..a81f9ecc 100644 --- a/zellij-server/src/route.rs +++ b/zellij-server/src/route.rs @@ -251,6 +251,31 @@ pub(crate) fn route_action( )) .with_context(err_context)?; }, + Action::WriteToPaneId { bytes, pane_id } => { + senders + .send_to_screen(ScreenInstruction::ClearScroll(client_id)) + .with_context(err_context)?; + senders + .send_to_screen(ScreenInstruction::WriteToPaneId( + bytes, + pane_id.into(), + Some(NotificationEnd::new(completion_tx)), + )) + .with_context(err_context)?; + }, + Action::WriteCharsToPaneId { chars, pane_id } => { + senders + .send_to_screen(ScreenInstruction::ClearScroll(client_id)) + .with_context(err_context)?; + let bytes = chars.into_bytes(); + senders + .send_to_screen(ScreenInstruction::WriteToPaneId( + bytes, + pane_id.into(), + Some(NotificationEnd::new(completion_tx)), + )) + .with_context(err_context)?; + }, Action::SwitchToMode { input_mode } => { let attrs = &client_attributes; senders diff --git a/zellij-server/src/screen.rs b/zellij-server/src/screen.rs index 761a7a4e..40200523 100644 --- a/zellij-server/src/screen.rs +++ b/zellij-server/src/screen.rs @@ -564,7 +564,14 @@ pub enum ScreenInstruction { RerunCommandPane(u32, Option<NotificationEnd>), // u32 - terminal pane id ResizePaneWithId(ResizeStrategy, PaneId), EditScrollbackForPaneWithId(PaneId, Option<NotificationEnd>), - WriteToPaneId(Vec<u8>, PaneId), + WriteToPaneId(Vec<u8>, PaneId, Option<NotificationEnd>), + WriteKeyToPaneId( + Option<KeyWithModifier>, + Vec<u8>, + bool, // is_kitty_keyboard_protocol + PaneId, + Option<NotificationEnd>, + ), CopyTextToClipboard(String, u32), // String - text to copy, u32 - plugin_id MovePaneWithPaneId(PaneId), MovePaneWithPaneIdInDirection(PaneId, Direction), @@ -812,6 +819,7 @@ impl From<&ScreenInstruction> for ScreenContext { ScreenContext::EditScrollbackForPaneWithId }, ScreenInstruction::WriteToPaneId(..) => ScreenContext::WriteToPaneId, + ScreenInstruction::WriteKeyToPaneId(..) => ScreenContext::WriteKeyToPaneId, ScreenInstruction::CopyTextToClipboard(..) => ScreenContext::CopyTextToClipboard, ScreenInstruction::MovePaneWithPaneId(..) => ScreenContext::MovePaneWithPaneId, ScreenInstruction::MovePaneWithPaneIdInDirection(..) => { @@ -6638,7 +6646,7 @@ pub(crate) fn screen_thread_main( } screen.render(None)?; }, - ScreenInstruction::WriteToPaneId(bytes, pane_id) => { + ScreenInstruction::WriteToPaneId(bytes, pane_id, _completion) => { let all_tabs = screen.get_tabs_mut(); for tab in all_tabs.values_mut() { if tab.has_pane_with_pid(&pane_id) { @@ -6649,6 +6657,30 @@ pub(crate) fn screen_thread_main( } screen.render(None)?; }, + ScreenInstruction::WriteKeyToPaneId( + key_with_modifier, + bytes, + is_kitty, + pane_id, + _completion, + ) => { + let all_tabs = screen.get_tabs_mut(); + for tab in all_tabs.values_mut() { + if tab.has_pane_with_pid(&pane_id) { + tab.write_to_pane_id( + &key_with_modifier, + bytes, + is_kitty, + pane_id, + None, // client_id not needed for targeted write + None, // completion handled by instruction + ) + .non_fatal(); + break; + } + } + screen.render(None)?; + }, ScreenInstruction::CopyTextToClipboard(text, plugin_id) => { let plugin_pane_id = PaneId::Plugin(plugin_id); let all_tabs = screen.get_tabs_mut(); diff --git a/zellij-server/src/tab/mod.rs b/zellij-server/src/tab/mod.rs index 2b061c11..dd2a8f50 100644 --- a/zellij-server/src/tab/mod.rs +++ b/zellij-server/src/tab/mod.rs @@ -2706,6 +2706,7 @@ impl Tab { .send_to_pty_writer(PtyWriteInstruction::Write( adjusted_input, active_terminal_id, + completion_tx, )) .with_context(err_context)?; }, @@ -2803,6 +2804,7 @@ impl Tab { .send_to_pty_writer(PtyWriteInstruction::Write( raw_input_bytes, active_terminal_id, + None, )) .with_context(err_context)?; should_update_ui = true; diff --git a/zellij-server/src/tab/unit/tab_integration_tests.rs b/zellij-server/src/tab/unit/tab_integration_tests.rs index 7b75223a..28ff97a0 100644 --- a/zellij-server/src/tab/unit/tab_integration_tests.rs +++ b/zellij-server/src/tab/unit/tab_integration_tests.rs @@ -177,7 +177,7 @@ impl MockPtyInstructionBus { .recv() .expect("failed to receive event on channel"); match event { - PtyWriteInstruction::Write(msg, _) => output + PtyWriteInstruction::Write(msg, _, _) => output .lock() .unwrap() .push(String::from_utf8_lossy(&msg).to_string()), diff --git a/zellij-server/src/unit/screen_tests.rs b/zellij-server/src/unit/screen_tests.rs index 0e1ce219..6af94ddd 100644 --- a/zellij-server/src/unit/screen_tests.rs +++ b/zellij-server/src/unit/screen_tests.rs @@ -2137,6 +2137,7 @@ pub fn send_cli_write_chars_action_to_screen() { ); let cli_action = CliAction::WriteChars { chars: "input from the cli".into(), + pane_id: None, }; send_cli_action_to_server(&session_metadata, cli_action, client_id); std::thread::sleep(std::time::Duration::from_millis(100)); // give time for actions to be @@ -2163,6 +2164,7 @@ pub fn send_cli_write_action_to_screen() { ); let cli_action = CliAction::Write { bytes: vec![102, 111, 111], + pane_id: None, }; send_cli_action_to_server(&session_metadata, cli_action, client_id); std::thread::sleep(std::time::Duration::from_millis(100)); // give time for actions to be @@ -2171,6 +2173,33 @@ pub fn send_cli_write_action_to_screen() { } #[test] +pub fn send_cli_send_keys_action_to_screen() { + let size = Size { + cols: 121, + rows: 20, + }; + let client_id = 10; + let mut mock_screen = MockScreen::new(size); + let pty_writer_receiver = mock_screen.pty_writer_receiver.take().unwrap(); + let session_metadata = mock_screen.clone_session_metadata(); + let screen_thread = mock_screen.run(None, vec![]); + let received_pty_instructions = Arc::new(Mutex::new(vec![])); + let pty_writer_thread = log_actions_in_thread!( + received_pty_instructions, + PtyWriteInstruction::Exit, + pty_writer_receiver + ); + let cli_action = CliAction::SendKeys { + keys: vec!["Ctrl a".to_string(), "x".to_string()], + pane_id: None, + }; + send_cli_action_to_server(&session_metadata, cli_action, client_id); + std::thread::sleep(std::time::Duration::from_millis(100)); + mock_screen.teardown(vec![pty_writer_thread, screen_thread]); + assert_snapshot!(format!("{:?}", *received_pty_instructions.lock().unwrap())); +} + +#[test] pub fn send_cli_resize_action_to_screen() { let size = Size { cols: 80, rows: 20 }; let client_id = 10; // fake client id should not appear in the screen's state @@ -2926,6 +2955,7 @@ pub fn send_cli_toggle_active_tab_sync_action() { let cli_toggle_active_tab_sync_action = CliAction::ToggleActiveSyncTab; let cli_write_action = CliAction::Write { bytes: vec![102, 111, 111], + pane_id: None, }; send_cli_action_to_server( &session_metadata, diff --git a/zellij-server/src/unit/snapshots/zellij_server__screen__screen_tests__send_cli_send_keys_action_to_screen.snap b/zellij-server/src/unit/snapshots/zellij_server__screen__screen_tests__send_cli_send_keys_action_to_screen.snap new file mode 100644 index 00000000..75e4aeef --- /dev/null +++ b/zellij-server/src/unit/snapshots/zellij_server__screen__screen_tests__send_cli_send_keys_action_to_screen.snap @@ -0,0 +1,5 @@ +--- +source: zellij-server/src/./unit/screen_tests.rs +expression: "format!(\"{:?}\", *received_pty_instructions.lock().unwrap())" +--- +[StartCachingResizes, ApplyCachedResizes, StartCachingResizes, ResizePty(0, 119, 18, None, None), ResizePty(0, 119, 18, None, None), ResizePty(0, 119, 18, None, None), ResizePty(0, 119, 18, None, None), ApplyCachedResizes, ResizePty(0, 119, 18, None, None), ResizePty(0, 119, 18, None, None), ResizePty(0, 119, 18, None, None), ResizePty(0, 119, 18, None, None), ResizePty(0, 119, 18, None, None), ApplyCachedResizes, ApplyCachedResizes, ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, StartCachingResizes, Write([1], 0, None), ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, StartCachingResizes, Write([120], 0, None), ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, Exit] diff --git a/zellij-server/src/unit/snapshots/zellij_server__screen__screen_tests__send_cli_toggle_active_tab_sync_action.snap b/zellij-server/src/unit/snapshots/zellij_server__screen__screen_tests__send_cli_toggle_active_tab_sync_action.snap index 14f77c44..d9fe6929 100644 --- a/zellij-server/src/unit/snapshots/zellij_server__screen__screen_tests__send_cli_toggle_active_tab_sync_action.snap +++ b/zellij-server/src/unit/snapshots/zellij_server__screen__screen_tests__send_cli_toggle_active_tab_sync_action.snap @@ -2,4 +2,4 @@ source: zellij-server/src/./unit/screen_tests.rs expression: "format!(\"{:?}\", *received_pty_instructions.lock().unwrap())" --- -[StartCachingResizes, ApplyCachedResizes, StartCachingResizes, ResizePty(0, 59, 18, None, None), ResizePty(1, 58, 18, None, None), ResizePty(0, 59, 18, None, None), ResizePty(1, 58, 18, None, None), ResizePty(0, 59, 18, None, None), ResizePty(1, 58, 18, None, None), ResizePty(0, 59, 18, None, None), ResizePty(1, 58, 18, None, None), ApplyCachedResizes, ResizePty(0, 58, 18, None, None), ResizePty(1, 59, 18, None, None), ResizePty(0, 58, 18, None, None), ResizePty(1, 59, 18, None, None), ResizePty(0, 58, 18, None, None), ResizePty(1, 59, 18, None, None), ResizePty(0, 58, 18, None, None), ResizePty(1, 59, 18, None, None), ResizePty(0, 59, 18, None, None), ResizePty(1, 58, 18, None, None), ApplyCachedResizes, ApplyCachedResizes, ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, StartCachingResizes, Write([102, 111, 111], 0), Write([102, 111, 111], 1), ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, Exit] +[StartCachingResizes, ApplyCachedResizes, StartCachingResizes, ResizePty(0, 59, 18, None, None), ResizePty(1, 58, 18, None, None), ResizePty(0, 59, 18, None, None), ResizePty(1, 58, 18, None, None), ResizePty(0, 59, 18, None, None), ResizePty(1, 58, 18, None, None), ResizePty(0, 59, 18, None, None), ResizePty(1, 58, 18, None, None), ApplyCachedResizes, ResizePty(0, 58, 18, None, None), ResizePty(1, 59, 18, None, None), ResizePty(0, 58, 18, None, None), ResizePty(1, 59, 18, None, None), ResizePty(0, 58, 18, None, None), ResizePty(1, 59, 18, None, None), ResizePty(0, 58, 18, None, None), ResizePty(1, 59, 18, None, None), ResizePty(0, 59, 18, None, None), ResizePty(1, 58, 18, None, None), ApplyCachedResizes, ApplyCachedResizes, ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, StartCachingResizes, Write([102, 111, 111], 0, None), Write([102, 111, 111], 1, None), ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, Exit] diff --git a/zellij-server/src/unit/snapshots/zellij_server__screen__screen_tests__send_cli_write_action_to_screen.snap b/zellij-server/src/unit/snapshots/zellij_server__screen__screen_tests__send_cli_write_action_to_screen.snap index 06163127..8f35fe9f 100644 --- a/zellij-server/src/unit/snapshots/zellij_server__screen__screen_tests__send_cli_write_action_to_screen.snap +++ b/zellij-server/src/unit/snapshots/zellij_server__screen__screen_tests__send_cli_write_action_to_screen.snap @@ -2,4 +2,4 @@ source: zellij-server/src/./unit/screen_tests.rs expression: "format!(\"{:?}\", *received_pty_instructions.lock().unwrap())" --- -[StartCachingResizes, ApplyCachedResizes, StartCachingResizes, ResizePty(0, 119, 18, None, None), ResizePty(0, 119, 18, None, None), ResizePty(0, 119, 18, None, None), ResizePty(0, 119, 18, None, None), ApplyCachedResizes, ResizePty(0, 119, 18, None, None), ResizePty(0, 119, 18, None, None), ResizePty(0, 119, 18, None, None), ResizePty(0, 119, 18, None, None), ResizePty(0, 119, 18, None, None), ApplyCachedResizes, ApplyCachedResizes, ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, StartCachingResizes, Write([102, 111, 111], 0), ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, Exit] +[StartCachingResizes, ApplyCachedResizes, StartCachingResizes, ResizePty(0, 119, 18, None, None), ResizePty(0, 119, 18, None, None), ResizePty(0, 119, 18, None, None), ResizePty(0, 119, 18, None, None), ApplyCachedResizes, ResizePty(0, 119, 18, None, None), ResizePty(0, 119, 18, None, None), ResizePty(0, 119, 18, None, None), ResizePty(0, 119, 18, None, None), ResizePty(0, 119, 18, None, None), ApplyCachedResizes, ApplyCachedResizes, ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, StartCachingResizes, Write([102, 111, 111], 0, None), ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, Exit] diff --git a/zellij-server/src/unit/snapshots/zellij_server__screen__screen_tests__send_cli_write_chars_action_to_screen.snap b/zellij-server/src/unit/snapshots/zellij_server__screen__screen_tests__send_cli_write_chars_action_to_screen.snap index dded3206..0feae52d 100644 --- a/zellij-server/src/unit/snapshots/zellij_server__screen__screen_tests__send_cli_write_chars_action_to_screen.snap +++ b/zellij-server/src/unit/snapshots/zellij_server__screen__screen_tests__send_cli_write_chars_action_to_screen.snap @@ -2,4 +2,4 @@ source: zellij-server/src/./unit/screen_tests.rs expression: "format!(\"{:?}\", *received_pty_instructions.lock().unwrap())" --- -[StartCachingResizes, ApplyCachedResizes, StartCachingResizes, ResizePty(0, 119, 18, None, None), ResizePty(0, 119, 18, None, None), ResizePty(0, 119, 18, None, None), ResizePty(0, 119, 18, None, None), ApplyCachedResizes, ResizePty(0, 119, 18, None, None), ResizePty(0, 119, 18, None, None), ResizePty(0, 119, 18, None, None), ResizePty(0, 119, 18, None, None), ResizePty(0, 119, 18, None, None), ApplyCachedResizes, ApplyCachedResizes, ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, StartCachingResizes, Write([105, 110, 112, 117, 116, 32, 102, 114, 111, 109, 32, 116, 104, 101, 32, 99, 108, 105], 0), ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, Exit] +[StartCachingResizes, ApplyCachedResizes, StartCachingResizes, ResizePty(0, 119, 18, None, None), ResizePty(0, 119, 18, None, None), ResizePty(0, 119, 18, None, None), ResizePty(0, 119, 18, None, None), ApplyCachedResizes, ResizePty(0, 119, 18, None, None), ResizePty(0, 119, 18, None, None), ResizePty(0, 119, 18, None, None), ResizePty(0, 119, 18, None, None), ResizePty(0, 119, 18, None, None), ApplyCachedResizes, ApplyCachedResizes, ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, StartCachingResizes, Write([105, 110, 112, 117, 116, 32, 102, 114, 111, 109, 32, 116, 104, 101, 32, 99, 108, 105], 0, None), ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, StartCachingResizes, ApplyCachedResizes, Exit] diff --git a/zellij-utils/assets/prost_ipc/client_server_contract.rs b/zellij-utils/assets/prost_ipc/client_server_contract.rs index 7bb1d58d..90754a11 100644 --- a/zellij-utils/assets/prost_ipc/client_server_contract.rs +++ b/zellij-utils/assets/prost_ipc/client_server_contract.rs @@ -119,7 +119,7 @@ pub struct RgbColor { #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct Action { - #[prost(oneof="action::ActionType", tags="1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 98, 87, 88, 89, 95, 96, 90, 91, 92, 93, 94, 97")] + #[prost(oneof="action::ActionType", tags="1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100")] pub action_type: ::core::option::Option<action::ActionType>, } /// Nested message and enum types in `Action`. @@ -299,18 +299,12 @@ pub mod action { KeybindPipe(super::KeybindPipeAction), #[prost(message, tag="86")] ListClients(super::ListClientsAction), - #[prost(message, tag="98")] - ListPanes(super::ListPanesAction), #[prost(message, tag="87")] TogglePanePinned(super::TogglePanePinnedAction), #[prost(message, tag="88")] StackPanes(super::StackPanesAction), #[prost(message, tag="89")] ChangeFloatingPaneCoordinates(super::ChangeFloatingPaneCoordinatesAction), - #[prost(message, tag="95")] - TogglePaneBorderless(super::TogglePaneBorderlessAction), - #[prost(message, tag="96")] - SetPaneBorderless(super::SetPaneBorderlessAction), #[prost(message, tag="90")] TogglePaneInGroup(super::TogglePaneInGroupAction), #[prost(message, tag="91")] @@ -321,8 +315,18 @@ pub mod action { NewBlockingPane(super::NewBlockingPaneAction), #[prost(message, tag="94")] OverrideLayout(super::OverrideLayoutAction), + #[prost(message, tag="95")] + TogglePaneBorderless(super::TogglePaneBorderlessAction), + #[prost(message, tag="96")] + SetPaneBorderless(super::SetPaneBorderlessAction), #[prost(message, tag="97")] SaveSession(super::SaveSessionAction), + #[prost(message, tag="98")] + ListPanes(super::ListPanesAction), + #[prost(message, tag="99")] + WriteCharsToPaneId(super::WriteCharsToPaneIdAction), + #[prost(message, tag="100")] + WriteToPaneId(super::WriteToPaneIdAction), } } // Action message definitions (all 92 variants) @@ -571,6 +575,22 @@ pub struct WriteCharsAction { } #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] +pub struct WriteToPaneIdAction { + #[prost(message, optional, tag="1")] + pub pane_id: ::core::option::Option<PaneId>, + #[prost(uint32, repeated, tag="2")] + pub bytes: ::prost::alloc::vec::Vec<u32>, +} +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct WriteCharsToPaneIdAction { + #[prost(message, optional, tag="1")] + pub pane_id: ::core::option::Option<PaneId>, + #[prost(string, tag="2")] + pub chars: ::prost::alloc::string::String, +} +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] pub struct SwitchToModeAction { #[prost(enumeration="InputMode", tag="1")] pub input_mode: i32, diff --git a/zellij-utils/src/cli.rs b/zellij-utils/src/cli.rs index 1b0bd7c8..376eafa1 100644 --- a/zellij-utils/src/cli.rs +++ b/zellij-utils/src/cli.rs @@ -603,10 +603,26 @@ pub enum CliAction { /// Write bytes to the terminal. Write { bytes: Vec<u8>, + /// The pane_id of the pane, eg. terminal_1, plugin_2 or 3 (equivalent to terminal_3) + #[clap(short, long, value_parser)] + pane_id: Option<String>, }, /// Write characters to the terminal. WriteChars { chars: String, + /// The pane_id of the pane, eg. terminal_1, plugin_2 or 3 (equivalent to terminal_3) + #[clap(short, long, value_parser)] + pane_id: Option<String>, + }, + /// Send one or more keys to the terminal (e.g., "Ctrl a", "F1", "Alt Shift b") + SendKeys { + /// Keys to send as space-separated strings + #[clap(value_parser, required = true)] + keys: Vec<String>, + + /// The pane_id of the pane, eg. terminal_1, plugin_2 or 3 (equivalent to terminal_3) + #[clap(short, long, value_parser)] + pane_id: Option<String>, }, /// [increase|decrease] the focused panes area at the [left|down|up|right] border. Resize { diff --git a/zellij-utils/src/client_server_contract/common_types.proto b/zellij-utils/src/client_server_contract/common_types.proto index a0393034..2154dde4 100644 --- a/zellij-utils/src/client_server_contract/common_types.proto +++ b/zellij-utils/src/client_server_contract/common_types.proto @@ -222,18 +222,20 @@ message Action { CliPipeAction cli_pipe = 84; KeybindPipeAction keybind_pipe = 85; ListClientsAction list_clients = 86; - ListPanesAction list_panes = 98; TogglePanePinnedAction toggle_pane_pinned = 87; StackPanesAction stack_panes = 88; ChangeFloatingPaneCoordinatesAction change_floating_pane_coordinates = 89; - TogglePaneBorderlessAction toggle_pane_borderless = 95; - SetPaneBorderlessAction set_pane_borderless = 96; TogglePaneInGroupAction toggle_pane_in_group = 90; ToggleGroupMarkingAction toggle_group_marking = 91; SwitchSessionAction switch_session = 92; NewBlockingPaneAction new_blocking_pane = 93; OverrideLayoutAction override_layout = 94; + TogglePaneBorderlessAction toggle_pane_borderless = 95; + SetPaneBorderlessAction set_pane_borderless = 96; SaveSessionAction save_session = 97; + ListPanesAction list_panes = 98; + WriteCharsToPaneIdAction write_chars_to_pane_id = 99; + WriteToPaneIdAction write_to_pane_id = 100; } } @@ -320,6 +322,16 @@ message WriteCharsAction { string chars = 1; } +message WriteToPaneIdAction { + PaneId pane_id = 1; + repeated uint32 bytes = 2; +} + +message WriteCharsToPaneIdAction { + PaneId pane_id = 1; + string chars = 2; +} + message SwitchToModeAction { InputMode input_mode = 1; } diff --git a/zellij-utils/src/errors.rs b/zellij-utils/src/errors.rs index db1734f4..06bb6801 100644 --- a/zellij-utils/src/errors.rs +++ b/zellij-utils/src/errors.rs @@ -364,6 +364,7 @@ pub enum ScreenContext { ResizePaneWithId, EditScrollbackForPaneWithId, WriteToPaneId, + WriteKeyToPaneId, CopyTextToClipboard, MovePaneWithPaneId, MovePaneWithPaneIdInDirection, diff --git a/zellij-utils/src/input/actions.rs b/zellij-utils/src/input/actions.rs index c526e617..bf77fe4d 100644 --- a/zellij-utils/src/input/actions.rs +++ b/zellij-utils/src/input/actions.rs @@ -127,6 +127,16 @@ pub enum Action { WriteChars { chars: String, }, + /// Write to a specific pane by ID. + WriteToPaneId { + bytes: Vec<u8>, + pane_id: PaneId, + }, + /// Write Characters to a specific pane by ID. + WriteCharsToPaneId { + chars: String, + pane_id: PaneId, + }, /// Switch to the specified input mode. SwitchToMode { input_mode: InputMode, @@ -520,12 +530,98 @@ impl Action { config: Option<Config>, ) -> Result<Vec<Action>, String> { match cli_action { - CliAction::Write { bytes } => Ok(vec![Action::Write { - key_with_modifier: None, - bytes, - is_kitty_keyboard_protocol: false, - }]), - CliAction::WriteChars { chars } => Ok(vec![Action::WriteChars { chars }]), + CliAction::Write { bytes, pane_id } => match pane_id { + Some(pane_id_str) => { + let parsed_pane_id = PaneId::from_str(&pane_id_str); + match parsed_pane_id { + Ok(parsed_pane_id) => { + Ok(vec![Action::WriteToPaneId { + bytes, + pane_id: parsed_pane_id, + }]) + }, + Err(_e) => { + Err(format!( + "Malformed pane id: {}, expecting either a bare integer (eg. 1), a terminal pane id (eg. terminal_1) or a plugin pane id (eg. plugin_1)", + pane_id_str + )) + } + } + }, + None => Ok(vec![Action::Write { + key_with_modifier: None, + bytes, + is_kitty_keyboard_protocol: false, + }]), + }, + CliAction::WriteChars { chars, pane_id } => match pane_id { + Some(pane_id_str) => { + let parsed_pane_id = PaneId::from_str(&pane_id_str); + match parsed_pane_id { + Ok(parsed_pane_id) => { + Ok(vec![Action::WriteCharsToPaneId { + chars, + pane_id: parsed_pane_id, + }]) + }, + Err(_e) => { + Err(format!( + "Malformed pane id: {}, expecting either a bare integer (eg. 1), a terminal pane id (eg. terminal_1) or a plugin pane id (eg. plugin_1)", + pane_id_str + )) + } + } + }, + None => Ok(vec![Action::WriteChars { chars }]), + }, + CliAction::SendKeys { keys, pane_id } => { + let mut actions = Vec::new(); + + for (index, key_str) in keys.iter().enumerate() { + let key = KeyWithModifier::from_str(key_str).map_err(|e| { + let suggestion = suggest_key_fix(key_str); + format!( + "Invalid key at position {}: \"{}\"\n Error: {}\n{}", + index + 1, + key_str, + e, + suggestion + ) + })?; + + #[cfg(not(target_family = "wasm"))] + let bytes = key + .serialize_kitty() + .map(|s| s.into_bytes()) + .unwrap_or_else(Vec::new); + + #[cfg(target_family = "wasm")] + let bytes = vec![]; + + match &pane_id { + Some(pane_id_str) => { + let parsed_pane_id = PaneId::from_str(pane_id_str) + .map_err(|_| format!( + "Malformed pane id: {}, expecting either a bare integer (eg. 1), a terminal pane id (eg. terminal_1) or a plugin pane id (eg. plugin_1)", + pane_id_str + ))?; + actions.push(Action::WriteToPaneId { + bytes, + pane_id: parsed_pane_id, + }); + }, + None => { + actions.push(Action::Write { + key_with_modifier: Some(key), + bytes, + is_kitty_keyboard_protocol: true, + }); + }, + } + } + + Ok(actions) + }, CliAction::Resize { resize, direction } => { Ok(vec![Action::Resize { resize, direction }]) }, @@ -1426,6 +1522,32 @@ impl Action { } } +fn suggest_key_fix(key_str: &str) -> String { + if key_str.contains('-') { + return " Hint: Use spaces instead of hyphens (e.g., \"Ctrl a\" not \"Ctrl-a\")" + .to_string(); + } + + if key_str.trim().is_empty() { + return " Hint: Key string cannot be empty".to_string(); + } + + let parts: Vec<&str> = key_str.split_whitespace().collect(); + if parts.len() > 1 { + for part in &parts[..parts.len() - 1] { + let lower = part.to_ascii_lowercase(); + if lower.starts_with("ctr") && lower != "ctrl" { + return format!(" Hint: Did you mean \"Ctrl\" instead of \"{}\"?", part); + } + if !matches!(lower.as_str(), "ctrl" | "alt" | "shift" | "super") { + return " Hint: Valid modifiers are: Ctrl, Alt, Shift, Super".to_string(); + } + } + } + + " Hint: Use format like \"Ctrl a\", \"Alt Shift F1\", or \"Enter\"".to_string() +} + impl From<OnForceClose> for Action { fn from(ofc: OnForceClose) -> Action { match ofc { @@ -1434,3 +1556,142 @@ impl From<OnForceClose> for Action { } } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::data::BareKey; + use crate::data::KeyModifier; + use std::path::PathBuf; + + #[test] + fn test_send_keys_single_key() { + let cli_action = CliAction::SendKeys { + keys: vec!["Enter".to_string()], + pane_id: None, + }; + let result = Action::actions_from_cli(cli_action, Box::new(|| PathBuf::from("/tmp")), None); + assert!(result.is_ok()); + let actions = result.unwrap(); + assert_eq!(actions.len(), 1); + match &actions[0] { + Action::Write { + key_with_modifier, + bytes, + is_kitty_keyboard_protocol, + } => { + assert!(key_with_modifier.is_some()); + let key = key_with_modifier.as_ref().unwrap(); + assert_eq!(key.bare_key, BareKey::Enter); + assert!(key.key_modifiers.is_empty()); + assert!(!bytes.is_empty()); + assert_eq!(*is_kitty_keyboard_protocol, true); + }, + _ => panic!("Expected Write action"), + } + } + + #[test] + fn test_send_keys_with_modifier() { + let cli_action = CliAction::SendKeys { + keys: vec!["Ctrl a".to_string()], + pane_id: None, + }; + let result = Action::actions_from_cli(cli_action, Box::new(|| PathBuf::from("/tmp")), None); + assert!(result.is_ok()); + let actions = result.unwrap(); + assert_eq!(actions.len(), 1); + match &actions[0] { + Action::Write { + key_with_modifier, + is_kitty_keyboard_protocol, + .. + } => { + assert!(key_with_modifier.is_some()); + let key = key_with_modifier.as_ref().unwrap(); + assert_eq!(key.bare_key, BareKey::Char('a')); + assert!(key.key_modifiers.contains(&KeyModifier::Ctrl)); + assert_eq!(*is_kitty_keyboard_protocol, true); + }, + _ => panic!("Expected Write action"), + } + } + + #[test] + fn test_send_keys_multiple_keys() { + let cli_action = CliAction::SendKeys { + keys: vec!["Ctrl a".to_string(), "F1".to_string(), "Enter".to_string()], + pane_id: None, + }; + let result = Action::actions_from_cli(cli_action, Box::new(|| PathBuf::from("/tmp")), None); + assert!(result.is_ok()); + let actions = result.unwrap(); + assert_eq!(actions.len(), 3); + for action in &actions { + match action { + Action::Write { + is_kitty_keyboard_protocol, + .. + } => { + assert_eq!(*is_kitty_keyboard_protocol, true); + }, + _ => panic!("Expected Write action"), + } + } + } + + #[test] + fn test_send_keys_error_hyphen_syntax() { + let cli_action = CliAction::SendKeys { + keys: vec!["Ctrl-a".to_string()], + pane_id: None, + }; + let result = Action::actions_from_cli(cli_action, Box::new(|| PathBuf::from("/tmp")), None); + assert!(result.is_err()); + let err = result.unwrap_err(); + assert!(err.contains("Use spaces instead of hyphens")); + } + + #[test] + fn test_send_keys_error_typo() { + let cli_action = CliAction::SendKeys { + keys: vec!["Ctrll a".to_string()], + pane_id: None, + }; + let result = Action::actions_from_cli(cli_action, Box::new(|| PathBuf::from("/tmp")), None); + assert!(result.is_err()); + let err = result.unwrap_err(); + assert!(err.contains("Ctrl") || err.contains("modifier")); + } + + #[test] + fn test_send_keys_with_pane_id() { + let cli_action = CliAction::SendKeys { + keys: vec!["a".to_string()], + pane_id: Some("terminal_1".to_string()), + }; + let result = Action::actions_from_cli(cli_action, Box::new(|| PathBuf::from("/tmp")), None); + assert!(result.is_ok()); + let actions = result.unwrap(); + assert_eq!(actions.len(), 1); + match &actions[0] { + Action::WriteToPaneId { pane_id, bytes } => { + assert!(matches!(pane_id, PaneId::Terminal(1))); + assert!(!bytes.is_empty()); + }, + _ => panic!("Expected WriteToPaneId action"), + } + } + + #[test] + fn test_send_keys_error_invalid_pane_id() { + let cli_action = CliAction::SendKeys { + keys: vec!["a".to_string()], + pane_id: Some("invalid_id".to_string()), + }; + let result = Action::actions_from_cli(cli_action, Box::new(|| PathBuf::from("/tmp")), None); + assert!(result.is_err()); + let err = result.unwrap_err(); + assert!(err.contains("Malformed pane id")); + } +} diff --git a/zellij-utils/src/ipc/protobuf_conversion.rs b/zellij-utils/src/ipc/protobuf_conversion.rs index 5d7429c9..3c127e78 100644 --- a/zellij-utils/src/ipc/protobuf_conversion.rs +++ b/zellij-utils/src/ipc/protobuf_conversion.rs @@ -741,7 +741,8 @@ impl From<crate::input::actions::Action> ToggleFocusFullscreenAction, ToggleGroupMarkingAction, ToggleMouseModeAction, TogglePaneBorderlessAction, TogglePaneEmbedOrFloatingAction, TogglePaneFramesAction, TogglePaneInGroupAction, TogglePanePinnedAction, ToggleTabAction, UndoRenamePaneAction, - UndoRenameTabAction, WriteAction, WriteCharsAction, + UndoRenameTabAction, WriteAction, WriteCharsAction, WriteCharsToPaneIdAction, + WriteToPaneIdAction, }; use std::collections::HashMap; @@ -759,6 +760,18 @@ impl From<crate::input::actions::Action> crate::input::actions::Action::WriteChars { chars } => { ActionType::WriteChars(WriteCharsAction { chars }) }, + crate::input::actions::Action::WriteToPaneId { bytes, pane_id } => { + ActionType::WriteToPaneId(WriteToPaneIdAction { + pane_id: Some(pane_id.into()), + bytes: bytes.into_iter().map(|b| b as u32).collect(), + }) + }, + crate::input::actions::Action::WriteCharsToPaneId { chars, pane_id } => { + ActionType::WriteCharsToPaneId(WriteCharsToPaneIdAction { + pane_id: Some(pane_id.into()), + chars, + }) + }, crate::input::actions::Action::SwitchToMode { input_mode } => { ActionType::SwitchToMode(SwitchToModeAction { input_mode: input_mode_to_proto_i32(input_mode), @@ -1361,6 +1374,28 @@ impl TryFrom<crate::client_server_contract::client_server_contract::Action> chars: write_chars_action.chars, }) }, + ActionType::WriteToPaneId(write_to_pane_id_action) => { + Ok(crate::input::actions::Action::WriteToPaneId { + bytes: write_to_pane_id_action + .bytes + .into_iter() + .map(|b| b as u8) + .collect(), + pane_id: write_to_pane_id_action + .pane_id + .ok_or_else(|| anyhow!("WriteToPaneId missing pane_id"))? + .try_into()?, + }) + }, + ActionType::WriteCharsToPaneId(write_chars_to_pane_id_action) => { + Ok(crate::input::actions::Action::WriteCharsToPaneId { + chars: write_chars_to_pane_id_action.chars, + pane_id: write_chars_to_pane_id_action + .pane_id + .ok_or_else(|| anyhow!("WriteCharsToPaneId missing pane_id"))? + .try_into()?, + }) + }, ActionType::SwitchToMode(switch_mode_action) => { Ok(crate::input::actions::Action::SwitchToMode { input_mode: proto_i32_to_input_mode(switch_mode_action.input_mode)?, diff --git a/zellij-utils/src/plugin_api/action.rs b/zellij-utils/src/plugin_api/action.rs index 534c0082..92dc618c 100644 --- a/zellij-utils/src/plugin_api/action.rs +++ b/zellij-utils/src/plugin_api/action.rs @@ -1030,6 +1030,9 @@ impl TryFrom<Action> for ProtobufAction { chars: chars_to_write, })), }), + Action::WriteToPaneId { .. } | Action::WriteCharsToPaneId { .. } => { + Err("WriteToPaneId and WriteCharsToPaneId are CLI-only actions, not available in keybindings") + }, Action::SwitchToMode { input_mode } => { let input_mode: ProtobufInputMode = input_mode.try_into()?; Ok(ProtobufAction { |
