diff options
author | Aram Drevekenin <aram@poor.dev> | 2022-03-25 15:41:08 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-25 15:41:08 +0100 |
commit | b5cb5474bb9c6d05b702873c1eac4aca955f2968 (patch) | |
tree | 4ed90c12cb6558648e3ce63248e0c5c82a117b94 | |
parent | be02f99652d53e075872ee7f54baf267474e6596 (diff) |
fix(screen): crash in intermediate no-tabs state (#1272)
* fix(screen): log error instead of crashing in intermediate state with no active tabs
* style(fmt): rustfmt
-rw-r--r-- | default-plugins/tab-bar/src/main.rs | 14 | ||||
-rw-r--r-- | zellij-server/src/screen.rs | 32 |
2 files changed, 27 insertions, 19 deletions
diff --git a/default-plugins/tab-bar/src/main.rs b/default-plugins/tab-bar/src/main.rs index 05f241bbb..2067af9d2 100644 --- a/default-plugins/tab-bar/src/main.rs +++ b/default-plugins/tab-bar/src/main.rs @@ -42,9 +42,13 @@ impl ZellijPlugin for State { match event { Event::ModeUpdate(mode_info) => self.mode_info = mode_info, Event::TabUpdate(tabs) => { - // tabs are indexed starting from 1 so we need to add 1 - self.active_tab_idx = tabs.iter().position(|t| t.active).unwrap() + 1; - self.tabs = tabs; + if let Some(active_tab_index) = tabs.iter().position(|t| t.active) { + // tabs are indexed starting from 1 so we need to add 1 + self.active_tab_idx = active_tab_index + 1; + self.tabs = tabs; + } else { + eprintln!("Could not find active tab."); + } } Event::Mouse(me) => match me { Mouse::LeftClick(_, col) => { @@ -59,7 +63,9 @@ impl ZellijPlugin for State { } _ => {} }, - _ => unimplemented!(), // FIXME: This should be unreachable, but this could be cleaner + _ => { + eprintln!("Got unrecognized event: {:?}", event); + } } } diff --git a/zellij-server/src/screen.rs b/zellij-server/src/screen.rs index 3f3c7c8b4..e5ad0edda 100644 --- a/zellij-server/src/screen.rs +++ b/zellij-server/src/screen.rs @@ -255,27 +255,29 @@ impl Screen { &mut self, client_ids_and_mode_infos: Vec<(ClientId, ModeInfo)>, ) { - // this will panic if there are no more tabs (ie. if self.tabs.is_empty() == true) + if self.tabs.is_empty() { + log::error!( + "No tabs left, cannot move clients: {:?} from closed tab", + client_ids_and_mode_infos + ); + return; + } + let first_tab_index = *self.tabs.keys().next().unwrap(); 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); - match client_tab_history.pop() { - Some(client_previous_tab) => { + 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); - self.tabs - .get_mut(&client_previous_tab) - .unwrap() - .add_client(client_id, Some(client_mode_info)); - } - None => { - let next_tab_index = *self.tabs.keys().next().unwrap(); - self.active_tab_indices.insert(client_id, next_tab_index); - self.tabs - .get_mut(&next_tab_index) - .unwrap() - .add_client(client_id, Some(client_mode_info)); + client_active_tab.add_client(client_id, Some(client_mode_info)); + 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)); } } fn move_clients_between_tabs( |