From 6715f4629c664885e0800da76cd73ca470705b27 Mon Sep 17 00:00:00 2001 From: har7an <99636919+har7an@users.noreply.github.com> Date: Thu, 6 Oct 2022 06:46:18 +0000 Subject: Server: Remove `panic`s in `tab` module (#1748) * utils/errors: Add `ToAnyhow` trait for converting `Result` types that don't satisfy `anyhow`s trait constraints (`Display + Send + Sync + 'static`) conveniently. An example of such a Result is the `SendError` returned from `send_to_plugins`, which sends `PluginInstruction`s as message type. One of the enum variants can contain a `mpsc::Sender`, which is `!Sync` and hence makes the whole `SendError` be `!Sync` in this case. Add an implementation for this case that takes the message and converts it into an error containing the message formatted as string, with the additional `ErrorContext` as anyhow context. * server/tab: Remove calls to `unwrap()` and apply error reporting via `anyhow` instead. Make all relevant functions return `Result`s where previously a panic could occur and attach error context. * server/screen: Modify `update_tab!` to accept an optional 4th parameter, a literal "?". If present, this will append a `?` to the given closure verbatim to handle/propagate errors from within the generated macro code. * server/screen: Handle new `Result`s from `Tab` and apply appropriate error context and propagate errors further up. * server/tab/unit: `unwrap` on new `Result`s * server/unit: Unwrap `Results` in screen tests * server/tab: Better message for ad-hoc errors created with `anyhow!`. Since these errors don't have an underlying cause, we describe the cause in the macro instead and then attach the error context as usual before `?`ing the error back up. * utils/cargo: Activate `anyhow`s "backtrace" feature to capture error backtraces at the error origins (i.e. where we first receive an error and convert it to a `anyhow::Error`). Since we propagate error back up the call stack now, the place where we `unwrap` on errors doesn't match the place where the error originated. Hence, the callstack, too, is quite misleading since it contains barely any references of the functions that triggered the error. As a consequence, we have 2 backtraces now when zellij crashes: One from `anyhow` (that is implicitly attached to anyhows error reports), and one from the custom panic handler (which is displayed through `miette`). * utils/errors: Separate stack traces in the output of miette. Since we record backtraces with `anyhow` now, we end up having two backtraces in the output: One from the `anyhow` error and one from the actual call to `panic`. Adds a comment explaining the situation and another "section" to the error output of miette: We print the backtrace from anyhow as "Stack backtrace", and the output from the panic handler as "Panic backtrace". We keep both for the (hopefully unlikely) case that the anyhow backtrace isn't existent, so we still have at least something to work with. * server/screen: Remove calls to `fatal` and leave the `panic`ing to the calling function instead. * server/screen: Remove needless macro which extended `active_tab!` by passing the client IDs to the closure. However, this isn't necessary because closures capture their environment already, and the client_id needn't be mutable. * server/screen: Handle unused result * server/screen: Reintroduce arcane macro that defaults to some default client_id if it isn't valid (e.g. when the ScreenInstruction is sent via CLI). * server/tab/unit: Unwrap new results --- Cargo.lock | 3 + zellij-server/src/screen.rs | 272 ++++--- zellij-server/src/tab/mod.rs | 656 +++++++++++---- .../src/tab/unit/tab_integration_tests.rs | 876 ++++++++++++--------- zellij-server/src/tab/unit/tab_tests.rs | 565 ++++++------- zellij-server/src/unit/screen_tests.rs | 4 +- zellij-utils/Cargo.toml | 2 +- zellij-utils/src/errors.rs | 42 +- 8 files changed, 1517 insertions(+), 903 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ad8705d62..23049df6d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -40,6 +40,9 @@ name = "anyhow" version = "1.0.57" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08f9b8508dccb7687a1d6c4ce66b2b0ecef467c94667de27d8d7fe1f8d2a9cdc" +dependencies = [ + "backtrace", +] [[package]] name = "arc-swap" diff --git a/zellij-server/src/screen.rs b/zellij-server/src/screen.rs index 012cc5577..fcb1de8cf 100644 --- a/zellij-server/src/screen.rs +++ b/zellij-server/src/screen.rs @@ -40,8 +40,10 @@ use zellij_utils::{ /// /// - screen: An instance of `Screen` to operate on /// - client_id: The client_id, usually taken from the `ScreenInstruction` that's being processed -/// - closure: A closure satisfying `|tab: &mut Tab| -> ()` - +/// - closure: A closure satisfying `|tab: &mut Tab| -> ()` OR `|tab: &mut Tab| -> Result` (see +/// '?' below) +/// - ?: A literal "?", to append a `?` to the closure when it returns a `Result` type. This +/// argument is optional and not needed when the closure returns `()` macro_rules! active_tab { ($screen:ident, $client_id:ident, $closure:expr) => { if let Some(active_tab) = $screen.get_active_tab_mut($client_id) { @@ -54,7 +56,16 @@ macro_rules! active_tab { log::error!("Active tab not found for client id: {:?}", $client_id); } }; + // Same as above, but with an added `?` for when the close returns a `Result` type. + ($screen:ident, $client_id:ident, $closure:expr, ?) => { + if let Some(active_tab) = $screen.get_active_tab_mut($client_id) { + $closure(active_tab)?; + } else { + log::error!("Active tab not found for client id: {:?}", $client_id); + } + }; } + macro_rules! active_tab_and_connected_client_id { ($screen:ident, $client_id:ident, $closure:expr) => { match $screen.get_active_tab_mut($client_id) { @@ -71,6 +82,30 @@ macro_rules! active_tab_and_connected_client_id { log::error!("Active tab not found for client id: {:?}", $client_id); }, } + } else { + log::error!("No client ids in screen found"); + }; + }, + } + }; + // Same as above, but with an added `?` for when the closure returns a `Result` type. + ($screen:ident, $client_id:ident, $closure:expr, ?) => { + match $screen.get_active_tab_mut($client_id) { + Some(active_tab) => { + $closure(active_tab, $client_id)?; + }, + None => { + if let Some(client_id) = $screen.get_first_client_id() { + match $screen.get_active_tab_mut(client_id) { + Some(active_tab) => { + $closure(active_tab, client_id)?; + }, + None => { + log::error!("Active tab not found for client id: {:?}", $client_id); + }, + } + } else { + log::error!("No client ids in screen found"); }; }, } @@ -393,32 +428,44 @@ impl Screen { fn move_clients_from_closed_tab( &mut self, client_ids_and_mode_infos: Vec<(ClientId, ModeInfo)>, - ) { + ) -> Result<()> { + let err_context = || "failed to move clients from closed tab".to_string(); + if self.tabs.is_empty() { log::error!( "No tabs left, cannot move clients: {:?} from closed tab", client_ids_and_mode_infos ); - return; + return Ok(()); } - let first_tab_index = *self.tabs.keys().next().unwrap(); + let first_tab_index = *self + .tabs + .keys() + .next() + .context("screen contained no tabs") + .with_context(err_context)?; for (client_id, client_mode_info) in client_ids_and_mode_infos { let client_tab_history = self.tab_history.entry(client_id).or_insert_with(Vec::new); if let Some(client_previous_tab) = client_tab_history.pop() { if let Some(client_active_tab) = self.tabs.get_mut(&client_previous_tab) { self.active_tab_indices .insert(client_id, client_previous_tab); - client_active_tab.add_client(client_id, Some(client_mode_info)); + client_active_tab + .add_client(client_id, Some(client_mode_info)) + .with_context(err_context)?; continue; } } self.active_tab_indices.insert(client_id, first_tab_index); self.tabs .get_mut(&first_tab_index) - .unwrap() - .add_client(client_id, Some(client_mode_info)); + .with_context(err_context)? + .add_client(client_id, Some(client_mode_info)) + .with_context(err_context)?; } + Ok(()) } + fn move_clients_between_tabs( &mut self, source_tab_index: usize, @@ -440,13 +487,18 @@ impl Screen { .get_indexed_tab_mut(destination_tab_index) .context("failed to get destination tab by index") .with_context(err_context)?; - destination_tab.add_multiple_clients(client_mode_info_in_source_tab); - destination_tab.update_input_modes(); + destination_tab + .add_multiple_clients(client_mode_info_in_source_tab) + .with_context(err_context)?; + destination_tab + .update_input_modes() + .with_context(err_context)?; destination_tab.set_force_render(); - destination_tab.visible(true); + destination_tab.visible(true).with_context(err_context)?; } Ok(()) } + fn update_client_tab_focus(&mut self, client_id: ClientId, new_tab_index: usize) { match self.active_tab_indices.remove(&client_id) { Some(old_active_index) => { @@ -498,7 +550,7 @@ impl Screen { if let Some(current_tab) = self.get_indexed_tab_mut(current_tab_index) { if current_tab.has_no_connected_clients() { - current_tab.visible(false); + current_tab.visible(false).with_context(err_context)?; } } else { log::error!("Tab index: {:?} not found", current_tab_index); @@ -564,6 +616,7 @@ impl Screen { fn close_tab_at_index(&mut self, tab_index: usize) -> Result<()> { let err_context = || format!("failed to close tab at index {tab_index:?}"); + let mut tab_to_close = self.tabs.remove(&tab_index).with_context(err_context)?; let pane_ids = tab_to_close.get_all_pane_ids(); // below we don't check the result of sending the CloseTab instruction to the pty thread @@ -581,13 +634,14 @@ impl Screen { .with_context(err_context) } else { let client_mode_infos_in_closed_tab = tab_to_close.drain_connected_clients(None); - self.move_clients_from_closed_tab(client_mode_infos_in_closed_tab); + self.move_clients_from_closed_tab(client_mode_infos_in_closed_tab) + .with_context(err_context)?; let visible_tab_indices: HashSet = self.active_tab_indices.values().copied().collect(); for t in self.tabs.values_mut() { if visible_tab_indices.contains(&t.index) { t.set_force_render(); - t.visible(true); + t.visible(true).with_context(err_context)?; } if t.position > tab_to_close.position { t.position -= 1; @@ -683,7 +737,8 @@ impl Screen { for (tab_index, tab) in &mut self.tabs { if tab.has_selectable_tiled_panes() { let vte_overlay = overlay.generate_overlay(size); - tab.render(&mut output, Some(vte_overlay)); + tab.render(&mut output, Some(vte_overlay)) + .context(err_context)?; } else { tabs_to_close.push(*tab_index); } @@ -793,13 +848,15 @@ impl Screen { self.terminal_emulator_colors.clone(), self.terminal_emulator_color_codes.clone(), ); - tab.apply_layout(layout, new_pids, tab_index, client_id); + tab.apply_layout(layout, new_pids, tab_index, client_id) + .with_context(err_context)?; if self.session_is_mirrored { if let Some(active_tab) = self.get_active_tab_mut(client_id) { let client_mode_infos_in_source_tab = active_tab.drain_connected_clients(None); - tab.add_multiple_clients(client_mode_infos_in_source_tab); + tab.add_multiple_clients(client_mode_infos_in_source_tab) + .with_context(err_context)?; if active_tab.has_no_connected_clients() { - active_tab.visible(false); + active_tab.visible(false).with_context(err_context)?; } } let all_connected_clients: Vec = @@ -810,14 +867,15 @@ impl Screen { } else if let Some(active_tab) = self.get_active_tab_mut(client_id) { let client_mode_info_in_source_tab = active_tab.drain_connected_clients(Some(vec![client_id])); - tab.add_multiple_clients(client_mode_info_in_source_tab); + tab.add_multiple_clients(client_mode_info_in_source_tab) + .with_context(err_context)?; if active_tab.has_no_connected_clients() { - active_tab.visible(false); + active_tab.visible(false).with_context(err_context)?; } self.update_client_tab_focus(client_id, tab_index); } - tab.update_input_modes(); - tab.visible(true); + tab.update_input_modes().with_context(err_context)?; + tab.visible(true).with_context(err_context)?; self.tabs.insert(tab_index, tab); if !self.active_tab_indices.contains_key(&client_id) { // this means this is a new client and we need to add it to our state properly @@ -829,6 +887,10 @@ impl Screen { } pub fn add_client(&mut self, client_id: ClientId) -> Result<()> { + let err_context = |tab_index| { + format!("failed to attach client {client_id} to tab with index {tab_index}") + }; + let mut tab_history = vec![]; if let Some((_first_client, first_tab_history)) = self.tab_history.iter().next() { tab_history = first_tab_history.clone(); @@ -843,7 +905,7 @@ impl Screen { } else if let Some(tab_index) = self.tabs.keys().next() { tab_index.to_owned() } else { - panic!("Can't find a valid tab to attach client to!"); + bail!("Can't find a valid tab to attach client to!"); }; self.active_tab_indices.insert(client_id, tab_index); @@ -851,18 +913,20 @@ impl Screen { self.tab_history.insert(client_id, tab_history); self.tabs .get_mut(&tab_index) - .with_context(|| format!("Failed to attach client to tab with index {tab_index}"))? - .add_client(client_id, None); - Ok(()) + .with_context(|| err_context(tab_index))? + .add_client(client_id, None) + .with_context(|| err_context(tab_index)) } pub fn remove_client(&mut self, client_id: ClientId) -> Result<()> { - self.tabs.iter_mut().for_each(|(_, tab)| { + let err_context = || format!("failed to remove client {client_id}"); + + for (_, tab) in self.tabs.iter_mut() { tab.remove_client(client_id); if tab.has_no_connected_clients() { - tab.visible(false); + tab.visible(false).with_context(err_context)?; } - }); + } if self.active_tab_indices.contains_key(&client_id) { self.active_tab_indices.remove(&client_id); } @@ -870,8 +934,7 @@ impl Screen { self.tab_history.remove(&client_id); } self.connected_clients.borrow_mut().remove(&client_id); - self.update_tabs() - .with_context(|| format!("failed to remove client {client_id:?}")) + self.update_tabs().with_context(err_context) } pub fn update_tabs(&self) -> Result<()> { @@ -908,12 +971,8 @@ impl Screen { Some(*client_id), Event::TabUpdate(tab_data), )) - .or_else(|err| { - let (_, error_context) = err.0; - Err(anyhow!("failed to send data to plugins")) - .context(error_context) - .context("failed to update tabs") - })?; + .to_anyhow() + .context("failed to update tabs")?; } Ok(()) } @@ -978,13 +1037,20 @@ impl Screen { } } - pub fn change_mode(&mut self, mode_info: ModeInfo, client_id: ClientId) { + pub fn change_mode(&mut self, mode_info: ModeInfo, client_id: ClientId) -> Result<()> { let previous_mode = self .mode_info .get(&client_id) .unwrap_or(&self.default_mode_info) .mode; + let err_context = || { + format!( + "failed to change from mode '{:?}' to mode '{:?}' for client {client_id}", + previous_mode, mode_info.mode + ) + }; + // If we leave the Search-related modes, we need to clear all previous searches let search_related_modes = [InputMode::EnterSearch, InputMode::Search, InputMode::Scroll]; if search_related_modes.contains(&previous_mode) @@ -997,7 +1063,9 @@ impl Screen { && (mode_info.mode == InputMode::Normal || mode_info.mode == InputMode::Locked) { if let Some(active_tab) = self.get_active_tab_mut(client_id) { - active_tab.clear_active_terminal_scroll(client_id); + active_tab + .clear_active_terminal_scroll(client_id) + .with_context(err_context)?; } } @@ -1023,16 +1091,27 @@ impl Screen { tab.change_mode_info(mode_info.clone(), client_id); tab.mark_active_pane_for_rerender(client_id); } + Ok(()) } - pub fn change_mode_for_all_clients(&mut self, mode_info: ModeInfo) { + + pub fn change_mode_for_all_clients(&mut self, mode_info: ModeInfo) -> Result<()> { + let err_context = || { + format!( + "failed to change input mode to {:?} for all clients", + mode_info.mode + ) + }; + let connected_client_ids: Vec = self.active_tab_indices.keys().copied().collect(); for client_id in connected_client_ids { - self.change_mode(mode_info.clone(), client_id); + self.change_mode(mode_info.clone(), client_id) + .with_context(err_context)?; if let Some(os_input) = &mut self.bus.os_input { let _ = os_input .send_to_client(client_id, ServerToClientMsg::SwitchToMode(mode_info.mode)); } } + Ok(()) } pub fn move_focus_left_or_previous_tab(&mut self, client_id: ClientId) -> Result<()> { let client_id = if self.get_active_tab(client_id).is_some() { @@ -1131,8 +1210,7 @@ pub(crate) fn screen_thread_main( let (event, mut err_ctx) = screen .bus .recv() - .context("failed to receive event on channel") - .fatal(); + .context("failed to receive event on channel")?; err_ctx.add_call(ContextType::Screen((&event).into())); match event { @@ -1140,7 +1218,8 @@ pub(crate) fn screen_thread_main( let all_tabs = screen.get_tabs_mut(); for tab in all_tabs.values_mut() { if tab.has_terminal_pid(pid) { - tab.handle_pty_bytes(pid, vte_bytes); + tab.handle_pty_bytes(pid, vte_bytes) + .context("failed to process pty bytes")?; break; } } @@ -1151,15 +1230,14 @@ pub(crate) fn screen_thread_main( ScreenInstruction::NewPane(pid, client_or_tab_index) => { match client_or_tab_index { ClientOrTabIndex::ClientId(client_id) => { - active_tab_and_connected_client_id!( - screen, - client_id, - |tab: &mut Tab, client_id: ClientId| tab.new_pane(pid, Some(client_id)) - ); + active_tab_and_connected_client_id!(screen, client_id, |tab: &mut Tab, + client_id: ClientId| tab .new_pane(pid, + Some(client_id)), + ?); }, ClientOrTabIndex::TabIndex(tab_index) => { if let Some(active_tab) = screen.tabs.get_mut(&tab_index) { - active_tab.new_pane(pid, None); + active_tab.new_pane(pid, None)?; } else { log::error!("Tab index not found: {:?}", tab_index); } @@ -1172,32 +1250,26 @@ pub(crate) fn screen_thread_main( }, ScreenInstruction::OpenInPlaceEditor(pid, client_id) => { active_tab!(screen, client_id, |tab: &mut Tab| tab - .suppress_active_pane(pid, client_id)); + .suppress_active_pane(pid, client_id), ?); screen.unblock_input()?; screen.update_tabs()?; screen.render()?; }, ScreenInstruction::TogglePaneEmbedOrFloating(client_id) => { - active_tab_and_connected_client_id!( - screen, - client_id, - |tab: &mut Tab, client_id: ClientId| tab - .toggle_pane_embed_or_floating(client_id) - ); + active_tab_and_connected_client_id!(screen, client_id, |tab: &mut Tab, client_id: ClientId| tab + .toggle_pane_embed_or_floating(client_id), ?); screen.unblock_input()?; screen.update_tabs()?; // update tabs so that the ui indication will be send to the plugins + screen.render()?; }, ScreenInstruction::ToggleFloatingPanes(client_id, default_shell) => { - active_tab_and_connected_client_id!( - screen, - client_id, - |tab: &mut Tab, client_id: ClientId| tab - .toggle_floating_panes(client_id, default_shell) - ); + active_tab_and_connected_client_id!(screen, client_id, |tab: &mut Tab, client_id: ClientId| tab + .toggle_floating_panes(client_id, default_shell), ?); screen.unblock_input()?; screen.update_tabs()?; // update tabs so that the ui indication will be send to the plugins + screen.render()?; }, ScreenInstruction::ShowFloatingPanes(client_id) => { @@ -1208,6 +1280,7 @@ pub(crate) fn screen_thread_main( ); screen.unblock_input()?; screen.update_tabs()?; // update tabs so that the ui indication will be send to the plugins + screen.render()?; }, ScreenInstruction::HideFloatingPanes(client_id) => { @@ -1218,13 +1291,15 @@ pub(crate) fn screen_thread_main( ); screen.unblock_input()?; screen.update_tabs()?; // update tabs so that the ui indication will be send to the plugins + screen.render()?; }, ScreenInstruction::HorizontalSplit(pid, client_id) => { active_tab_and_connected_client_id!( screen, client_id, - |tab: &mut Tab, client_id: ClientId| tab.horizontal_split(pid, client_id) + |tab: &mut Tab, client_id: ClientId| tab.horizontal_split(pid, client_id), + ? ); screen.unblock_input()?; screen.update_tabs()?; @@ -1234,7 +1309,8 @@ pub(crate) fn screen_thread_main( active_tab_and_connected_client_id!( screen, client_id, - |tab: &mut Tab, client_id: ClientId| tab.vertical_split(pid, client_id) + |tab: &mut Tab, client_id: ClientId| tab.vertical_split(pid, client_id), + ? ); screen.unblock_input()?; screen.update_tabs()?; @@ -1249,7 +1325,8 @@ pub(crate) fn screen_thread_main( true => tab.write_to_terminals_on_current_tab(bytes), false => tab.write_to_active_terminal(bytes, client_id), } - } + }, + ? ); }, ScreenInstruction::ResizeLeft(client_id) => { @@ -1393,7 +1470,8 @@ pub(crate) fn screen_thread_main( active_tab_and_connected_client_id!( screen, client_id, - |tab: &mut Tab, client_id: ClientId| tab.edit_scrollback(client_id) + |tab: &mut Tab, client_id: ClientId| tab.edit_scrollback(client_id), + ? ); screen.render()?; }, @@ -1456,7 +1534,7 @@ pub(crate) fn screen_thread_main( screen, client_id, |tab: &mut Tab, client_id: ClientId| tab - .handle_scrollwheel_up(&point, 3, client_id) + .handle_scrollwheel_up(&point, 3, client_id), ? ); screen.render()?; screen.unblock_input()?; @@ -1465,7 +1543,7 @@ pub(crate) fn screen_thread_main( active_tab_and_connected_client_id!( screen, client_id, - |tab: &mut Tab, client_id: ClientId| tab.scroll_active_terminal_down(client_id) + |tab: &mut Tab, client_id: ClientId| tab.scroll_active_terminal_down(client_id), ? ); screen.render()?; screen.unblock_input()?; @@ -1475,7 +1553,7 @@ pub(crate) fn screen_thread_main( screen, client_id, |tab: &mut Tab, client_id: ClientId| tab - .handle_scrollwheel_down(&point, 3, client_id) + .handle_scrollwheel_down(&point, 3, client_id), ? ); screen.render()?; screen.unblock_input()?; @@ -1485,7 +1563,7 @@ pub(crate) fn screen_thread_main( screen, client_id, |tab: &mut Tab, client_id: ClientId| tab - .scroll_active_terminal_to_bottom(client_id) + .scroll_active_terminal_to_bottom(client_id), ? ); screen.render()?; screen.unblock_input()?; @@ -1505,7 +1583,7 @@ pub(crate) fn screen_thread_main( screen, client_id, |tab: &mut Tab, client_id: ClientId| tab - .scroll_active_terminal_down_page(client_id) + .scroll_active_terminal_down_page(client_id), ? ); screen.render()?; screen.unblock_input()?; @@ -1525,7 +1603,7 @@ pub(crate) fn screen_thread_main( screen, client_id, |tab: &mut Tab, client_id: ClientId| tab - .scroll_active_terminal_down_half_page(client_id) + .scroll_active_terminal_down_half_page(client_id), ? ); screen.render()?; screen.unblock_input()?; @@ -1535,7 +1613,7 @@ pub(crate) fn screen_thread_main( screen, client_id, |tab: &mut Tab, client_id: ClientId| tab - .clear_active_terminal_scroll(client_id) + .clear_active_terminal_scroll(client_id), ? ); screen.render()?; screen.unblock_input()?; @@ -1544,7 +1622,7 @@ pub(crate) fn screen_thread_main( active_tab_and_connected_client_id!( screen, client_id, - |tab: &mut Tab, client_id: ClientId| tab.close_focused_pane(client_id) + |tab: &mut Tab, client_id: ClientId| tab.close_focused_pane(client_id), ? ); screen.update_tabs()?; screen.render()?; @@ -1585,7 +1663,7 @@ pub(crate) fn screen_thread_main( active_tab_and_connected_client_id!( screen, client_id, - |tab: &mut Tab, client_id: ClientId| tab.update_active_pane_name(c, client_id) + |tab: &mut Tab, client_id: ClientId| tab.update_active_pane_name(c, client_id), ? ); screen.render()?; screen.unblock_input()?; @@ -1594,7 +1672,7 @@ pub(crate) fn screen_thread_main( active_tab_and_connected_client_id!( screen, client_id, - |tab: &mut Tab, client_id: ClientId| tab.undo_active_rename_pane(client_id) + |tab: &mut Tab, client_id: ClientId| tab.undo_active_rename_pane(client_id), ? ); screen.render()?; screen.unblock_input()?; @@ -1641,7 +1719,10 @@ pub(crate) fn screen_thread_main( ScreenInstruction::GoToTab(tab_index, client_id) => { let client_id = if client_id.is_none() { None - } else if screen.active_tab_indices.contains_key(&client_id.unwrap()) { + } else if screen + .active_tab_indices + .contains_key(&client_id.expect("This is checked above")) + { client_id } else { screen.active_tab_indices.keys().next().copied() @@ -1679,12 +1760,12 @@ pub(crate) fn screen_thread_main( screen.update_terminal_color_registers(color_registers); }, ScreenInstruction::ChangeMode(mode_info, client_id) => { - screen.change_mode(mode_info, client_id); + screen.change_mode(mode_info, client_id)?; screen.render()?; screen.unblock_input()?; }, ScreenInstruction::ChangeModeForAllClients(mode_info) => { - screen.change_mode_for_all_clients(mode_info); + screen.change_mode_for_all_clients(mode_info)?; screen.render()?; screen.unblock_input()?; }, @@ -1700,62 +1781,59 @@ pub(crate) fn screen_thread_main( }, ScreenInstruction::LeftClick(point, client_id) => { active_tab!(screen, client_id, |tab: &mut Tab| tab - .handle_left_click(&point, client_id)); + .handle_left_click(&point, client_id), ?); screen.update_tabs()?; screen.render()?; screen.unblock_input()?; }, ScreenInstruction::RightClick(point, client_id) => { active_tab!(screen, client_id, |tab: &mut Tab| tab - .handle_right_click(&point, client_id)); + .handle_right_click(&point, client_id), ?); screen.update_tabs()?; screen.render()?; screen.unblock_input()?; }, ScreenInstruction::MiddleClick(point, client_id) => { active_tab!(screen, client_id, |tab: &mut Tab| tab - .handle_middle_click(&point, client_id)); + .handle_middle_click(&point, client_id), ?); screen.update_tabs()?; screen.render()?; screen.unblock_input()?; }, ScreenInstruction::LeftMouseRelease(point, client_id) => { active_tab!(screen, client_id, |tab: &mut Tab| tab - .handle_left_mouse_release(&point, client_id)); + .handle_left_mouse_release(&point, client_id), ?); screen.render()?; screen.unblock_input()?; }, ScreenInstruction::RightMouseRelease(point, client_id) => { active_tab!(screen, client_id, |tab: &mut Tab| tab - .handle_right_mouse_release(&point, client_id)); + .handle_right_mouse_release(&point, client_id), ?); screen.render()?; }, ScreenInstruction::MiddleMouseRelease(point, client_id) => { active_tab!(screen, client_id, |tab: &mut Tab| tab - .handle_middle_mouse_release(&point, client_id)); + .handle_middle_mouse_release(&point, client_id), ?); screen.render()?; }, ScreenInstruction::MouseHoldLeft(point, client_id) => { - active_tab!(screen, client_id, |tab: &mut Tab| { - tab.handle_mouse_hold_left(&point, client_id); - }); + active_tab!(screen, client_id, |tab: &mut Tab| tab + .handle_mouse_hold_left(&point, client_id), ?); screen.render()?; }, ScreenInstruction::MouseHoldRight(point, client_id) => { - active_tab!(screen, client_id, |tab: &mut Tab| { - tab.handle_mouse_hold_right(&point, client_id); - }); + active_tab!(screen, client_id, |tab: &mut Tab| tab + .handle_mouse_hold_right(&point, client_id), ?); screen.render()?; }, ScreenInstruction::MouseHoldMiddle(point, client_id) => { - active_tab!(screen, client_id, |tab: &mut Tab| { - tab.handle_mouse_hold_middle(&point, client_id); - }); + active_tab!(screen, client_id, |tab: &mut Tab| tab + .handle_mouse_hold_middle(&point, client_id), ?); screen.render()?; }, ScreenInstruction::Copy(client_id) => { active_tab!(screen, client_id, |tab: &mut Tab| tab - .copy_selection(client_id)); + .copy_selection(client_id), ?); screen.render()?; }, ScreenInstruction::Exit => { @@ -1807,7 +1885,7 @@ pub(crate) fn screen_thread_main( active_tab_and_connected_client_id!( screen, client_id, - |tab: &mut Tab, client_id: ClientId| tab.update_search_term(c, client_id) + |tab: &mut Tab, client_id: ClientId| tab.update_search_term(c, client_id), ? ); screen.render()?; }, diff --git a/zellij-server/src/tab/mod.rs b/zellij-server/src/tab/mod.rs index 883caf0d1..7a3554df2 100644 --- a/zellij-server/src/tab/mod.rs +++ b/zellij-server/src/tab/mod.rs @@ -7,6 +7,7 @@ mod copy_command; use copy_command::CopyCommand; use std::env::temp_dir; use uuid::Uuid; +use zellij_utils::errors::prelude::*; use zellij_utils::position::{Column, Line}; use zellij_utils::{position::Position, serde}; @@ -458,7 +459,12 @@ impl Tab { new_pids: Vec, tab_index: usize, client_id: ClientId, - ) { + ) -> Result<()> { + let err_context = || { + format!( + "failed to apply layout {layout:#?} in tab {tab_index} for client id {client_id}" + ) + }; if self.tiled_panes.has_panes() { log::error!( "Applying a layout to a tab with existing panes - this is not yet supported!" @@ -491,12 +497,17 @@ impl Tab { let pane_title = run.location.to_string(); self.senders .send_to_plugin(PluginInstruction::Load(pid_tx, run, tab_index, client_id)) - .unwrap(); - let pid = pid_rx.recv().unwrap(); + .to_anyhow() + .with_context(err_context)?; + let pid = pid_rx.recv().with_context(err_context)?; let mut new_plugin = PluginPane::new( pid, *position_and_size, - self.senders.to_plugin.as_ref().unwrap().clone(), + self.senders + .to_plugin + .as_ref() + .with_context(err_context)? + .clone(), pane_title, layout.name.clone().unwrap_or_default(), ); @@ -506,7 +517,7 @@ impl Tab { set_focus_pane_id(layout, PaneId::Plugin(pid)); } else { // there are still panes left to fill, use the pids we received in this method - let pid = new_pids.next().unwrap(); // if this crashes it means we got less pids than there are panes in this layout + let pid = new_pids.next().with_context(err_context)?; // if this crashes it means we got less pids than there are panes in this layout let next_terminal_position = self.get_next_terminal_position(); let mut new_pane = TerminalPane::new( *pid, @@ -532,7 +543,7 @@ impl Tab { // fixing this will require a bit of an architecture change self.senders .send_to_pty(PtyInstruction::ClosePane(PaneId::Terminal(*unused_pid))) - .unwrap(); + .with_context(err_context)?; } // FIXME: This is another hack to crop the viewport to fixed-size panes. Once you can have // non-fixed panes that are part of the viewport, get rid of this! @@ -564,8 +575,9 @@ impl Tab { }, } } + Ok(()) } - pub fn update_input_modes(&mut self) { + pub fn update_input_modes(&mut self) -> Result<()> { // this updates all plugins with the client's input mode let mode_infos = self.mode_info.borrow(); for client_id in self.connected_clients.borrow().iter() { @@ -576,10 +588,17 @@ impl Tab { Some(*client_id), Event::ModeUpdate(mode_info.clone()), )) - .unwrap(); + .to_anyhow() + .with_context(|| { + format!( + "failed to update plugins with mode info {:?}", + mode_info.mode + ) + })?; } + Ok(()) } - pub fn add_client(&mut self, client_id: ClientId, mode_info: Option) { + pub fn add_client(&mut self, client_id: ClientId, mode_info: Option) -> Result<()> { let other_clients_exist_in_tab = { !self.connected_clients.borrow().is_empty() }; if other_clients_exist_in_tab { if let Some(first_active_floating_pane_id) = @@ -601,13 +620,17 @@ impl Tab { let mut pane_ids: Vec = self.tiled_panes.pane_ids().copied().collect(); if pane_ids.is_empty() { // no panes here, bye bye - return; + return Ok(()); } - let focus_pane_id = self.focus_pane_id.unwrap_or_else(|| { + let focus_pane_id = if let Some(id) = self.focus_pane_id { + id + } else { pane_ids.sort(); // TODO: make this predictable pane_ids.retain(|p| !self.tiled_panes.panes_to_hide_contains(*p)); - *pane_ids.get(0).unwrap() - }); + *(pane_ids.get(0).with_context(|| { + format!("failed to acquire id of focused pane while adding client {client_id}",) + })?) + }; self.tiled_panes.focus_pane(focus_pane_id, client_id); self.connected_clients.borrow_mut().insert(client_id); self.mode_info.borrow_mut().insert( @@ -616,18 +639,26 @@ impl Tab { ); } self.set_force_render(); - self.update_input_modes(); + self.update_input_modes() + .with_context(|| format!("failed to add client {client_id} to tab")) } + pub fn change_mode_info(&mut self, mode_info: ModeInfo, client_id: ClientId) { self.mode_info.borrow_mut().insert(client_id, mode_info); } - pub fn add_multiple_clients(&mut self, client_ids_to_mode_infos: Vec<(ClientId, ModeInfo)>) { + + pub fn add_multiple_clients( + &mut self, + client_ids_to_mode_infos: Vec<(ClientId, ModeInfo)>, + ) -> Result<()> { for (client_id, client_mode_info) in client_ids_to_mode_infos { - self.add_client(client_id, None); + self.add_client(client_id, None) + .context("failed to add clients")?; self.mode_info .borrow_mut() .insert(client_id, client_mode_info); } + Ok(()) } pub fn remove_client(&mut self, client_id: ClientId) { self.focus_pane_id = None; @@ -659,16 +690,18 @@ impl Tab { pub fn has_no_connected_clients(&self) -> bool { self.connected_clients.borrow().is_empty() } - pub fn toggle_pane_embed_or_floating(&mut self, client_id: ClientId) { + pub fn toggle_pane_embed_or_floating(&mut self, client_id: ClientId) -> Result<()> { if self.tiled_panes.fullscreen_is_active() { self.tiled_panes.unset_fullscreen(); } if self.floating_panes.panes_are_visible() { if let Some(focused_floating_pane_id) = self.floating_panes.active_pane_id(client_id) { if self.tiled_panes.has_room_for_new_pane() { - // this unwrap is safe because floating panes should not be visible if there are no floating panes - let floating_pane_to_embed = - self.close_pane(focused_floating_pane_id, true).unwrap(); + let floating_pane_to_embed = self + .close_pane(focused_floating_pane_id, true) + .with_context(|| format!( + "failed to find floating pane (ID: {focused_floating_pane_id:?}) to embed for client {client_id}", + ))?; self.tiled_panes .insert_pane(focused_floating_pane_id, floating_pane_to_embed); self.should_clear_display_before_rendering = true; @@ -681,7 +714,7 @@ impl Tab { if let Some(new_pane_geom) = self.floating_panes.find_room_for_new_pane() { if self.get_selectable_tiled_panes().count() <= 1 { // don't close the only pane on screen... - return; + return Ok(()); } if let Some(mut embedded_pane_to_float) = self.close_pane(focused_pane_id, true) { embedded_pane_to_float.set_geom(new_pane_geom); @@ -694,12 +727,13 @@ impl Tab { } } } + Ok(()) } pub fn toggle_floating_panes( &mut self, client_id: ClientId, default_shell: Option, - ) { + ) -> Result<()> { if self.floating_panes.panes_are_visible() { self.floating_panes.toggle_show_panes(false); self.set_force_render(); @@ -727,23 +761,30 @@ impl Tab { default_shell, ClientOrTabIndex::ClientId(client_id), ); - self.senders.send_to_pty(instruction).unwrap(); + self.senders.send_to_pty(instruction).with_context(|| { + format!("failed to open a floating pane for client {client_id}") + })?; }, } self.floating_panes.set_force_render(); } self.set_force_render(); + Ok(()) } + pub fn show_floating_panes(&mut self) { self.floating_panes.toggle_show_panes(true); self.set_force_render(); } + pub fn hide_floating_panes(&mut self) { self.floating_panes.toggle_show_panes(false); self.set_force_render(); } - pub fn new_pane(&mut self, pid: PaneId, client_id: Option) { - self.close_down_to_max_terminals(); + + pub fn new_pane(&mut self, pid: PaneId, client_id: Option) -> Result<()> { + self.close_down_to_max_terminals() + .with_context(|| format!("failed to create new pane with id {pid:?}"))?; if self.floating_panes.panes_are_visible() { if let Some(new_pane_geom) = self.floating_panes.find_room_for_new_pane() { let next_terminal_position = self.get_next_terminal_position(); @@ -793,8 +834,9 @@ impl Tab { } } } + Ok(()) } - pub fn suppress_active_pane(&mut self, pid: PaneId, client_id: ClientId) { + pub fn suppress_active_pane(&mut self, pid: PaneId, client_id: ClientId) -> Result<()> { // this method creates a new pane from pid and replaces it with the active pane // the active pane is then suppressed (hidden and not rendered) until the current // created pane is closed, in which case it will be replaced back by it @@ -824,7 +866,11 @@ impl Tab { Some(replaced_pane) => { self.suppressed_panes .insert(PaneId::Terminal(pid), replaced_pane); - let current_active_pane = self.get_active_pane(client_id).unwrap(); // this will be the newly replaced pane we just created + // this will be the newly replaced pane we just created + let current_active_pane = + self.get_active_pane(client_id).with_context(|| { + format!("failed to suppress active pane for client {client_id}") + })?; resize_pty!(current_active_pane, self.os_api); }, None => { @@ -836,12 +882,18 @@ impl Tab { // TBD, currently unsupported }, } + Ok(()) } - pub fn horizontal_split(&mut self, pid: PaneId, client_id: ClientId) { + + pub fn horizontal_split(&mut self, pid: PaneId, client_id: ClientId) -> Result<()> { + let err_context = + || format!("failed to split pane {pid:?} horizontally for client {client_id}"); + if self.floating_panes.panes_are_visible() { - return; + return Ok(()); } - self.close_down_to_max_terminals(); + self.close_down_to_max_terminals() + .with_context(err_context)?; if self.tiled_panes.fullscreen_is_active() { self.toggle_active_pane_fullscreen(client_id); } @@ -866,12 +918,18 @@ impl Tab { self.tiled_panes.focus_pane(pid, client_id); } } + Ok(()) } - pub fn vertical_split(&mut self, pid: PaneId, client_id: ClientId) { + + pub fn vertical_split(&mut self, pid: PaneId, client_id: ClientId) -> Result<()> { + let err_context = + || format!("failed to split pane {pid:?} vertically for client {client_id}"); + if self.floating_panes.panes_are_visible() { - return; + return Ok(()); } - self.close_down_to_max_terminals(); + self.close_down_to_max_terminals() + .with_context(err_context)?; if self.tiled_panes.fullscreen_is_active() { self.toggle_active_pane_fullscreen(client_id); } @@ -896,7 +954,9 @@ impl Tab { self.tiled_panes.focus_pane(pid, client_id); } } + Ok(()) } + pub fn get_active_pane(&self, client_id: ClientId) -> Option<&dyn Pane> { self.get_active_pane_id(client_id).and_then(|ap| { if self.floating_panes.panes_are_visible() { @@ -947,7 +1007,9 @@ impl Tab { .values() .any(|s_p| s_p.pid() == PaneId::Terminal(pid)) } - pub fn handle_pty_bytes(&mut self, pid: RawFd, bytes: VteBytes) { + pub fn handle_pty_bytes(&mut self, pid: RawFd, bytes: VteBytes) -> Result<()> { + let err_context = || format!("failed to handle pty bytes from fd {pid}"); + if let Some(terminal_output) = self .tiled_panes .get_pane_mut(PaneId::Terminal(pid)) @@ -965,23 +1027,30 @@ impl Tab { // Reset scroll - and process all pending events for this pane if evs.len() >= MAX_PENDING_VTE_EVENTS { terminal_output.clear_scroll(); - self.process_pending_vte_events(pid); + self.process_pending_vte_events(pid) + .with_context(err_context)?; } } - return; + return Ok(()); } } - self.process_pty_bytes(pid, bytes); + self.process_pty_bytes(pid, bytes).with_context(err_context) } - pub fn process_pending_vte_events(&mut self, pid: RawFd) { + + pub fn process_pending_vte_events(&mut self, pid: RawFd) -> Result<()> { if let Some(pending_vte_events) = self.pending_vte_events.get_mut(&pid) { let vte_events: Vec = pending_vte_events.drain(..).collect(); for vte_event in vte_events { - self.process_pty_bytes(pid, vte_event); + self.process_pty_bytes(pid, vte_event) + .context("failed to process pending vte events")?; } } + Ok(()) } - fn process_pty_bytes(&mut self, pid: RawFd, bytes: VteBytes) { + + fn process_pty_bytes(&mut self, pid: RawFd, bytes: VteBytes) -> Result<()> { + let err_context = || format!("failed to process pty bytes from pid {pid}"); + if let Some(terminal_output) = self .tiled_panes .get_pane_mut(PaneId::Terminal(pid)) @@ -996,45 +1065,88 @@ impl Tab { let messages_to_pty = terminal_output.drain_messages_to_pty(); let clipboard_update = terminal_output.drain_clipboard_update(); for message in messages_to_pty { - self.write_to_pane_id(message, PaneId::Terminal(pid)); + self.write_to_pane_id(message, PaneId::Terminal(pid)) + .with_context(err_context)?; } if let Some(string) = clipboard_update { - self.write_selection_to_clipboard(&string); + self.write_selection_to_clipboard(&string) + .with_context(err_context)?; } } + Ok(()) } - pub fn write_to_terminals_on_current_tab(&mut self, input_bytes: Vec) { + + pub fn write_to_terminals_on_current_tab(&mut self, input_bytes: Vec) -> Result<()> { let pane_ids = self.get_static_and_floating_pane_ids(); - pane_ids.iter().for_each(|&pane_id| { - self.write_to_pane_id(input_bytes.clone(), pane_id); - }); + for pane_id in pane_ids { + self.write_to_pane_id(input_bytes.clone(), pane_id) + .context("failed to write to terminals on current tab")?; + } + Ok(()) } - pub fn write_to_active_terminal(&mut self, input_bytes: Vec, client_id: ClientId) { + + pub fn write_to_active_terminal( + &mut self, + input_bytes: Vec, + client_id: ClientId, + ) -> Result<()> { + let err_context = || { + format!( + "failed to write to active terminal for client {client_id} - msg: {input_bytes:?}" + ) + }; + self.clear_search(client_id); // this is an inexpensive operation if empty, if we need more such cleanups we should consider moving this and the rest to some sort of cleanup method let pane_id = if self.floating_panes.panes_are_visible() { self.floating_panes .get_active_pane_id(client_id) - .unwrap_or_else(|| self.tiled_panes.get_active_pane_id(client_id).unwrap()) + .or_else(|| self.tiled_panes.get_active_pane_id(client_id)) + .ok_or_else(|| { + anyhow!(format!( + "failed to find active pane id for client {client_id}" + )) + }) + .with_context(err_context)? } else { - self.tiled_panes.get_active_pane_id(client_id).unwrap() + self.tiled_panes + .get_active_pane_id(client_id) + .with_context(err_context)? }; - self.write_to_pane_id(input_bytes, pane_id); + // Can't use 'err_context' here since it borrows 'input_bytes' + self.write_to_pane_id(input_bytes, pane_id) + .with_context(|| format!("failed to write to active terminal for client {client_id}")) } - pub fn write_to_terminal_at(&mut self, input_bytes: Vec, position: &Position) { + + pub fn write_to_terminal_at( + &mut self, + input_bytes: Vec, + position: &Position, + ) -> Result<()> { + let err_context = || format!("failed to write to terminal at position {position:?}"); + if self.floating_panes.panes_are_visible() { let pane_id = self.floating_panes.get_pane_id_at(position, false); if let Some(pane_id) = pane_id { - self.write_to_pane_id(input_bytes, pane_id); - return; + return self + .write_to_pane_id(input_bytes, pane_id) + .with_context(err_context); } } - let pane_id = self.get_pane_id_at(position, false); + let pane_id = self + .get_pane_id_at(position, false) + .with_context(err_context)?; if let Some(pane_id) = pane_id { - self.write_to_pane_id(input_bytes, pane_id); + return self + .write_to_pane_id(input_bytes, pane_id) + .with_context(err_context); } + Ok(()) } - pub fn write_to_pane_id(&mut self, input_bytes: Vec, pane_id: PaneId) { + + pub fn write_to_pane_id(&mut self, input_bytes: Vec, pane_id: PaneId) -> Result<()> { + let err_context = || format!("failed to write to pane with id {pane_id:?}"); + match pane_id { PaneId::Terminal(active_terminal_id) => { let active_terminal = self @@ -1042,7 +1154,8 @@ impl Tab { .get(&pane_id) .or_else(|| self.tiled_panes.get_pane(pane_id)) .or_else(|| self.suppressed_panes.get(&pane_id)) - .unwrap(); + .ok_or_else(|| anyhow!(format!("failed to find pane with id {pane_id:?}"))) + .with_context(err_context)?; let adjusted_input = active_terminal.adjust_input_to_terminal(input_bytes); self.senders @@ -1050,16 +1163,18 @@ impl Tab { adjusted_input, active_terminal_id, )) - .unwrap(); + .with_context(err_context)?; }, PaneId::Plugin(pid) => { for key in parse_keys(&input_bytes) { self.senders .send_to_plugin(PluginInstruction::Update(Some(pid), None, Event::Key(key))) - .unwrap() + .to_anyhow() + .with_context(err_context)?; } }, } + Ok(()) } pub fn get_active_terminal_cursor_position( &self, @@ -1124,7 +1239,7 @@ impl Tab { active_pane.set_should_render(true); } } - fn update_active_panes_in_pty_thread(&self) { + fn update_active_panes_in_pty_thread(&self) -> Result<()> { // this is a bit hacky and we should ideally not keep this state in two different places at // some point let connected_clients: Vec = @@ -1135,16 +1250,21 @@ impl Tab { self.get_active_pane_id(client_id), client_id, )) - .unwrap(); + .with_context(|| format!("failed to update active pane for client {client_id}"))?; } + Ok(()) } - pub fn render(&mut self, output: &mut Output, overlay: Option) { + + pub fn render(&mut self, output: &mut Output, overlay: Option) -> Result<()> { + let err_context = || "failed to render tab".to_string(); + let connected_clients: HashSet = { self.connected_clients.borrow().iter().copied().collect() }; if connected_clients.is_empty() || !self.tiled_panes.has_active_panes() { - return; + return Ok(()); } - self.update_active_panes_in_pty_thread(); + self.update_active_panes_in_pty_thread() + .with_context(err_context)?; let floating_panes_stack = self.floating_panes.stack(); output.add_clients( @@ -1169,7 +1289,9 @@ impl Tab { } self.render_cursor(output); + Ok(()) } + fn hide_cursor_and_clear_display_as_needed(&mut self, output: &mut Output) { let hide_cursor = "\u{1b}[?25l"; let connected_clients: Vec = @@ -1489,16 +1611,17 @@ impl Tab { self.tiled_panes.move_active_pane_left(client_id); } } - fn close_down_to_max_terminals(&mut self) { + fn close_down_to_max_terminals(&mut self) -> Result<()> { if let Some(max_panes) = self.max_panes { let terminals = self.get_tiled_pane_ids(); for &pid in terminals.iter().skip(max_panes - 1) { self.senders .send_to_pty(PtyInstruction::ClosePane(pid)) - .unwrap(); + .context("failed to close down to max terminals")?; self.close_pane(pid, false); } } + Ok(()) } pub fn get_tiled_pane_ids(&self) -> Vec { self.get_tiled_panes().map(|(&pid, _)| pid).collect() @@ -1594,22 +1717,27 @@ impl Tab { replaced_pane }) } - pub fn close_focused_pane(&mut self, client_id: ClientId) { + pub fn close_focused_pane(&mut self, client_id: ClientId) -> Result<()> { + let err_context = |pane_id| { + format!("failed to close focused pane (ID {pane_id:?}) for client {client_id}") + }; + if self.floating_panes.panes_are_visible() { if let Some(active_floating_pane_id) = self.floating_panes.active_pane_id(client_id) { self.close_pane(active_floating_pane_id, false); self.senders .send_to_pty(PtyInstruction::ClosePane(active_floating_pane_id)) - .unwrap(); - return; + .with_context(|| err_context(active_floating_pane_id))?; + return Ok(()); } } if let Some(active_pane_id) = self.tiled_panes.get_active_pane_id(client_id) { self.close_pane(active_pane_id, false); self.senders .send_to_pty(PtyInstruction::ClosePane(active_pane_id)) - .unwrap(); + .with_context(|| err_context(active_pane_id))?; } + Ok(()) } pub fn dump_active_terminal_screen(&mut self, file: Option, client_id: ClientId) { if let Some(active_pane) = self.get_active_pane_or_floating_pane_mut(client_id) { @@ -1617,7 +1745,7 @@ impl Tab { self.os_api.write_to_file(dump, file); } } - pub fn edit_scrollback(&mut self, client_id: ClientId) { + pub fn edit_scrollback(&mut self, client_id: ClientId) -> Result<()> { let mut file = temp_dir(); file.push(format!("{}.dump", Uuid::new_v4())); self.dump_active_terminal_screen(Some(String::from(file.to_string_lossy())), client_id); @@ -1630,23 +1758,29 @@ impl Tab { line_number, client_id, )) - .unwrap(); + .with_context(|| format!("failed to edit scrollback for client {client_id}")) } pub fn scroll_active_terminal_up(&mut self, client_id: ClientId) { if let Some(active_pane) = self.get_active_pane_or_floating_pane_mut(client_id) { active_pane.scroll_up(1, client_id); } } - pub fn scroll_active_terminal_down(&mut self, client_id: ClientId) { + + pub fn scroll_active_terminal_down(&mut self, client_id: ClientId) -> Result<()> { + let err_context = || format!("failed to scroll down active pane for client {client_id}"); + if let Some(active_pane) = self.get_active_pane_or_floating_pane_mut(client_id) { active_pane.scroll_down(1, client_id); if !active_pane.is_scrolled() { if let PaneId::Terminal(raw_fd) = active_pane.pid() { - self.process_pending_vte_events(raw_fd); + self.process_pending_vte_events(raw_fd) + .with_context(err_context)?; } } } + Ok(()) } + pub fn scroll_active_terminal_up_page(&mut self, client_id: ClientId) { if let Some(active_pane) = self.get_active_pane_or_floating_pane_mut(client_id) { // prevent overflow when row == 0 @@ -1654,17 +1788,24 @@ impl Tab { active_pane.scroll_up(scroll_rows, client_id); } } - pub fn scroll_active_terminal_down_page(&mut self, client_id: ClientId) { + + pub fn scroll_active_terminal_down_page(&mut self, client_id: ClientId) -> Result<()> { + let err_context = + || format!("failed to scroll down one page in active pane for client {client_id}"); + if let Some(active_pane) = self.get_active_pane_or_floating_pane_mut(client_id) { let scroll_rows = active_pane.get_content_rows(); active_pane.scroll_down(scroll_rows, client_id); if !active_pane.is_scrolled() { if let PaneId::Terminal(raw_fd) = active_pane.pid() { - self.process_pending_vte_events(raw_fd); + self.process_pending_vte_events(raw_fd) + .with_context(err_context)?; } } } + Ok(()) } + pub fn scroll_active_terminal_up_half_page(&mut self, client_id: ClientId) { if let Some(active_pa