Skip to content

Commit

Permalink
screen: Don't crash when first tab doesn't exist
Browse files Browse the repository at this point in the history
while trying to attach a new client. Instead, check whether the first
tab does exist and if not, take the first tab index from the tabs
present in the session. If no tabs exist, panic with a better error
message.
  • Loading branch information
har7an committed Aug 10, 2022
1 parent 01adc08 commit 4643384
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions zellij-server/src/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -702,21 +702,29 @@ impl Screen {
}

pub fn add_client(&mut self, client_id: ClientId) {
let mut tab_index = 0;
let mut tab_history = vec![];
if let Some((_first_client, first_active_tab_index)) = self.active_tab_indices.iter().next()
{
tab_index = *first_active_tab_index;
}
if let Some((_first_client, first_tab_history)) = self.tab_history.iter().next() {
tab_history = first_tab_history.clone();
}

let tab_index = if let Some((_first_client, first_active_tab_index)) =
self.active_tab_indices.iter().next()
{
*first_active_tab_index
} else if self.tabs.contains_key(&0) {
0
} 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!");
};

self.active_tab_indices.insert(client_id, tab_index);
self.connected_clients.borrow_mut().insert(client_id);
self.tab_history.insert(client_id, tab_history);
self.tabs
.get_mut(&tab_index)
.unwrap()
.expect("Failed to attach client to tab with index {tab_index}")
.add_client(client_id, None);
}
pub fn remove_client(&mut self, client_id: ClientId) {
Expand Down

0 comments on commit 4643384

Please sign in to comment.