diff options
author | Oleks Gnatovskyi <22867443+alekspickle@users.noreply.github.com> | 2023-03-29 23:04:57 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-29 23:04:57 +0200 |
commit | bdb39b19a6117dc344b1dfee27df778f5386bb62 (patch) | |
tree | e3adf519f4c1641bfbdbaa61929a5c8e776a3043 /zellij-server | |
parent | 638f2ad4ff5d2e80bb7cee16e8c0512fcbb52caf (diff) |
feat(terminal): cli and bindable action to clear all buffers for a specific pane (#2239)
* fix typo
* Add clear screen action
* add proper test
* added bindable action; remove pointless default impl
* add default binding
* use imsnif's variant
* remove commented example config
* remove log line
---------
Co-authored-by: Aram Drevekenin <aram@poor.dev>
Diffstat (limited to 'zellij-server')
-rw-r--r-- | zellij-server/src/panes/grid.rs | 11 | ||||
-rw-r--r-- | zellij-server/src/panes/plugin_pane.rs | 3 | ||||
-rw-r--r-- | zellij-server/src/panes/terminal_pane.rs | 3 | ||||
-rw-r--r-- | zellij-server/src/route.rs | 6 | ||||
-rw-r--r-- | zellij-server/src/screen.rs | 14 | ||||
-rw-r--r-- | zellij-server/src/tab/mod.rs | 7 | ||||
-rw-r--r-- | zellij-server/src/tab/unit/tab_integration_tests.rs | 29 |
7 files changed, 72 insertions, 1 deletions
diff --git a/zellij-server/src/panes/grid.rs b/zellij-server/src/panes/grid.rs index 16ed8aa66..b76d21ecb 100644 --- a/zellij-server/src/panes/grid.rs +++ b/zellij-server/src/panes/grid.rs @@ -1109,7 +1109,16 @@ impl Grid { Some((self.cursor.x, self.cursor.y)) } } - + /// Clears all buffers with text for a current screen + pub fn clear_screen(&mut self) { + if self.alternate_screen_state.is_some() { + log::warn!("Tried to clear pane with alternate_screen_state"); + return; + } + self.reset_terminal_state(); + self.mark_for_rerender(); + } + /// Dumps all lines above terminal vieport and the viewport itself to a string pub fn dump_screen(&mut self, full: bool) -> String { let viewport: String = dump_screen!(self.viewport); if !full { diff --git a/zellij-server/src/panes/plugin_pane.rs b/zellij-server/src/panes/plugin_pane.rs index 28828907e..980438c57 100644 --- a/zellij-server/src/panes/plugin_pane.rs +++ b/zellij-server/src/panes/plugin_pane.rs @@ -433,6 +433,9 @@ impl Pane for PluginPane { )])) .unwrap(); } + fn clear_screen(&mut self) { + // do nothing + } fn clear_scroll(&mut self) { // noop } diff --git a/zellij-server/src/panes/terminal_pane.rs b/zellij-server/src/panes/terminal_pane.rs index d48df67a7..469cfa093 100644 --- a/zellij-server/src/panes/terminal_pane.rs +++ b/zellij-server/src/panes/terminal_pane.rs @@ -499,6 +499,9 @@ impl Pane for TerminalPane { fn dump_screen(&mut self, _client_id: ClientId, full: bool) -> String { self.grid.dump_screen(full) } + fn clear_screen(&mut self) { + self.grid.clear_screen() + } fn scroll_up(&mut self, count: usize, _client_id: ClientId) { self.grid.move_viewport_up(count); self.set_should_render(true); diff --git a/zellij-server/src/route.rs b/zellij-server/src/route.rs index f63da2d21..c240ec40d 100644 --- a/zellij-server/src/route.rs +++ b/zellij-server/src/route.rs @@ -172,6 +172,12 @@ pub(crate) fn route_action( .send_to_screen(ScreenInstruction::MovePaneBackwards(client_id)) .with_context(err_context)?; }, + Action::ClearScreen => { + session + .senders + .send_to_screen(ScreenInstruction::ClearScreen(client_id)) + .with_context(err_context)?; + }, Action::DumpScreen(val, full) => { session .senders diff --git a/zellij-server/src/screen.rs b/zellij-server/src/screen.rs index f1dd38741..10f9f3451 100644 --- a/zellij-server/src/screen.rs +++ b/zellij-server/src/screen.rs @@ -161,6 +161,7 @@ pub enum ScreenInstruction { MovePaneRight(ClientId), MovePaneLeft(ClientId), Exit, + ClearScreen(ClientId), DumpScreen(String, ClientId, bool), EditScrollback(ClientId), ScrollUp(ClientId), @@ -327,6 +328,7 @@ impl From<&ScreenInstruction> for ScreenContext { ScreenInstruction::MovePaneRight(..) => ScreenContext::MovePaneRight, ScreenInstruction::MovePaneLeft(..) => ScreenContext::MovePaneLeft, ScreenInstruction::Exit => ScreenContext::Exit, + ScreenInstruction::ClearScreen(..) => ScreenContext::ClearScreen, ScreenInstruction::DumpScreen(..) => ScreenContext::DumpScreen, ScreenInstruction::EditScrollback(..) => ScreenContext::EditScrollback, ScreenInstruction::ScrollUp(..) => ScreenContext::ScrollUp, @@ -1764,6 +1766,18 @@ pub(crate) fn screen_thread_main( screen.render()?; screen.unblock_input()?; }, + ScreenInstruction::ClearScreen(client_id) => { + active_tab_and_connected_client_id!( + screen, + client_id, + |tab: &mut Tab, client_id: ClientId| tab.clear_active_terminal_screen( + client_id, + ), + ? + ); + screen.render()?; + screen.unblock_input()?; + }, ScreenInstruction::DumpScreen(file, client_id, full) => { active_tab_and_connected_client_id!( screen, diff --git a/zellij-server/src/tab/mod.rs b/zellij-server/src/tab/mod.rs index 1c1a77ac8..eab71da9c 100644 --- a/zellij-server/src/tab/mod.rs +++ b/zellij-server/src/tab/mod.rs @@ -235,6 +235,7 @@ pub trait Pane { fn push_right(&mut self, count: usize); fn pull_left(&mut self, count: usize); fn pull_up(&mut self, count: usize); + fn clear_screen(&mut self); fn dump_screen(&mut self, _client_id: ClientId, _full: bool) -> String { "".to_owned() } @@ -2419,6 +2420,12 @@ impl Tab { } Ok(()) } + pub fn clear_active_terminal_screen(&mut self, client_id: ClientId) -> Result<()> { + if let Some(active_pane) = self.get_active_pane_or_floating_pane_mut(client_id) { + active_pane.clear_screen(); + } + Ok(()) + } pub fn dump_active_terminal_screen( &mut self, file: Option<String>, diff --git a/zellij-server/src/tab/unit/tab_integration_tests.rs b/zellij-server/src/tab/unit/tab_integration_tests.rs index 8ccce263e..13ab67104 100644 --- a/zellij-server/src/tab/unit/tab_integration_tests.rs +++ b/zellij-server/src/tab/unit/tab_integration_tests.rs @@ -709,6 +709,35 @@ fn dump_screen() { } #[test] +fn clear_screen() { + let size = Size { + cols: 121, + rows: 20, + }; + let client_id = 1; + let mut tab = create_new_tab(size, ModeInfo::default()); + let map = Arc::new(Mutex::new(HashMap::new())); + tab.os_api = Box::new(FakeInputOutput { + file_dumps: map.clone(), + ..Default::default() + }); + let new_pane_id = PaneId::Terminal(2); + tab.new_pane(new_pane_id, None, None, Some(client_id)) + .unwrap(); + tab.handle_pty_bytes(2, Vec::from("scratch".as_bytes())) + .unwrap(); + let file = "/tmp/log-clear-screen.sh"; + tab.clear_active_terminal_screen(client_id).unwrap(); + tab.dump_active_terminal_screen(Some(file.to_string()), client_id, false) + .unwrap(); + assert_eq!( + map.lock().unwrap().get(file).unwrap(), + "", + "screen was cleared properly" + ); +} + +#[test] fn new_floating_pane() { let size = Size { cols: 121, |