From 081dbcb8cde54a0ec06537a2028c37f34821ace8 Mon Sep 17 00:00:00 2001 From: Sagittarius-a Date: Fri, 23 Jul 2021 22:38:02 +0200 Subject: [PATCH] fix: fix toggle to previous tab when deleting tabs --- zellij-server/src/screen.rs | 19 ++++- zellij-server/src/unit/screen_tests.rs | 104 +++++++++++++++++++++++-- zellij-utils/src/input/mod.rs | 2 +- 3 files changed, 115 insertions(+), 10 deletions(-) diff --git a/zellij-server/src/screen.rs b/zellij-server/src/screen.rs index 33027db9b3..5cc16abc9e 100644 --- a/zellij-server/src/screen.rs +++ b/zellij-server/src/screen.rs @@ -268,6 +268,7 @@ impl Screen { /// Closes this [`Screen`]'s active [`Tab`], exiting the application if it happens /// to be the last tab. pub fn close_tab(&mut self) { + let future_previous = self.previous_active_tab_index; let active_tab_index = self.active_tab_index.unwrap(); if self.tabs.len() > 1 { self.switch_tab_prev(); @@ -298,6 +299,7 @@ impl Screen { } self.update_tabs(); } + self.previous_active_tab_index = future_previous; } pub fn resize_to_screen(&mut self, new_screen_size: PositionAndSize) { @@ -336,6 +338,14 @@ impl Screen { } } + /// Returns an immutable reference to this [`Screen`]'s previous active [`Tab`]. + pub fn get_previous_tab(&self) -> Option<&Tab> { + match self.previous_active_tab_index { + Some(tab) => self.tabs.get(&tab), + None => None, + } + } + /// Returns a mutable reference to this [`Screen`]'s active [`Tab`]. pub fn get_active_tab_mut(&mut self) -> Option<&mut Tab> { match self.active_tab_index { @@ -420,10 +430,11 @@ impl Screen { self.switch_tab_next(); } } - pub fn go_to_last_tab(&mut self) { + pub fn toggle_tab(&mut self) { let active_tab_index = self.active_tab_index.unwrap(); - if let Some(i) = self.previous_active_tab_index { - self.go_to_tab(i + 1); + if let Some(_) = self.previous_active_tab_index { + let position = self.get_previous_tab().unwrap().position; + self.go_to_tab(position + 1); } self.previous_active_tab_index = Some(active_tab_index); self.update_tabs(); @@ -734,7 +745,7 @@ pub(crate) fn screen_thread_main( break; } ScreenInstruction::ToggleTab => { - screen.go_to_last_tab(); + screen.toggle_tab(); screen .bus .senders diff --git a/zellij-server/src/unit/screen_tests.rs b/zellij-server/src/unit/screen_tests.rs index 0ddbdf0b0f..83d8887b00 100644 --- a/zellij-server/src/unit/screen_tests.rs +++ b/zellij-server/src/unit/screen_tests.rs @@ -249,7 +249,7 @@ fn move_focus_right_at_right_screen_edge_changes_tab() { } #[test] -pub fn switch_to_last_tab() { +pub fn toggle_to_previous_tab_simple() { let position_and_size = PositionAndSize { cols: 121, rows: 20, @@ -264,17 +264,111 @@ pub fn switch_to_last_tab() { screen.go_to_tab(1); screen.go_to_tab(2); - screen.go_to_last_tab(); + screen.toggle_tab(); assert_eq!( screen.get_active_tab().unwrap().position, 0, - "Active tab switched to last tab" + "Active tab toggler to previous tab" ); - screen.go_to_last_tab(); + screen.toggle_tab(); assert_eq!( screen.get_active_tab().unwrap().position, 1, - "Active tab switched to last tab" + "Active tab toggler to previous tab" + ); +} + +#[test] +pub fn toggle_to_previous_tab_create_tabs_only() { + let position_and_size = PositionAndSize { + cols: 121, + rows: 20, + x: 0, + y: 0, + ..Default::default() + }; + let mut screen = create_new_screen(position_and_size); + + screen.new_tab(1); + screen.new_tab(2); + screen.new_tab(3); + + screen.toggle_tab(); + assert_eq!( + screen.get_active_tab().unwrap().position, + 1, + "Active tab toggler to previous tab" + ); + + screen.toggle_tab(); + assert_eq!( + screen.get_active_tab().unwrap().position, + 2, + "Active tab toggler to previous tab" + ); + + screen.toggle_tab(); + assert_eq!( + screen.get_active_tab().unwrap().position, + 1, + "Active tab toggler to previous tab" + ); +} + +#[test] +pub fn toggle_to_previous_tab_delete() { + let position_and_size = PositionAndSize { + cols: 121, + rows: 20, + x: 0, + y: 0, + ..Default::default() + }; + let mut screen = create_new_screen(position_and_size); + + screen.new_tab(1); + screen.new_tab(2); + screen.new_tab(3); + + screen.toggle_tab(); + assert_eq!( + screen.get_active_tab().unwrap().position, + 1, + "Active tab toggler to previous tab" + ); + assert_eq!( + screen.get_previous_tab().unwrap().position, + 2, + "Previous active tab invalid" + ); + + screen.close_tab(); + assert_eq!( + screen.get_active_tab().unwrap().position, + 0, + "Active tab toggler to previous tab" + ); + assert_eq!( + screen.get_previous_tab().unwrap().position, + 1, + "Previous active tab invalid" + ); + assert_eq!( + screen.get_previous_tab().unwrap().index, + 2, + "Previous active tab invalid" + ); + + screen.toggle_tab(); + assert_eq!( + screen.get_active_tab().unwrap().position, + 1, + "Active tab toggler to previous tab" + ); + assert_eq!( + screen.get_previous_tab().unwrap().position, + 0, + "Previous active tab invalid" ); } diff --git a/zellij-utils/src/input/mod.rs b/zellij-utils/src/input/mod.rs index 8601a63ec4..ad728747f9 100644 --- a/zellij-utils/src/input/mod.rs +++ b/zellij-utils/src/input/mod.rs @@ -37,7 +37,7 @@ pub fn get_mode_info( ("x".to_string(), "Close".to_string()), ("r".to_string(), "Rename".to_string()), ("s".to_string(), "Sync".to_string()), - ("Tab".to_string(), "Last".to_string()), + ("Tab".to_string(), "Toggle".to_string()), ], InputMode::Scroll => vec![ ("↓↑".to_string(), "Scroll".to_string()),