Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce SwitchCurrentTabToPrev/SwitchCurrentTabToNext actions #854

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions docs/docs/config/bindings.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,16 @@ Execute a predefined action in Rio terminal.

### [Tab Actions](#tab-actions)

| Action | Description |
| :------------------- | :------------------------------------------------------------------ |
| CreateTab | |
| CloseTab | |
| CloseUnfocusedTabs | |
| SelectPrevTab | |
| SelectNextTab | |
| SelectLastTab | |
| SelectTab(tab_index) | Example: Select first tab `SelectTab(0)`, second tab `SelectTab(1)` |
| Action | Description |
| :------------------- | :---------------------------------------------------------------------- |
| CreateTab | |
| CloseTab | |
| CloseUnfocusedTabs | |
| SelectPrevTab | |
| SelectNextTab | |
| SelectLastTab | |
| MoveCurrentTabToPrev | Move the current focused tab to the previous slot if any is available |
| SelectTab(tab_index) | Example: Select first tab `SelectTab(0)`, second tab `SelectTab(1)` |

### [Scroll Actions](#scroll-actions)

Expand Down
8 changes: 8 additions & 0 deletions frontends/rioterm/src/bindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ impl From<String> for Action {
"decreasefontsize" => Some(Action::DecreaseFontSize),
"createwindow" => Some(Action::WindowCreateNew),
"createtab" => Some(Action::TabCreateNew),
"movecurrenttabtoprev" => Some(Action::MoveCurrentTabToPrev),
"movecurrenttabtonext" => Some(Action::MoveCurrentTabToNext),
"closetab" => Some(Action::TabCloseCurrent),
"closecurrenttaborsplit" => Some(Action::CloseCurrentSplitOrTab),
"closeunfocusedtabs" => Some(Action::TabCloseUnfocused),
Expand Down Expand Up @@ -407,6 +409,12 @@ pub enum Action {
/// Create a new Rio tab.
TabCreateNew,

/// Move current tab to previous slot.
MoveCurrentTabToPrev,

/// Move current tab to next slot.
MoveCurrentTabToNext,

/// Switch to next tab.
SelectNextTab,

Expand Down
112 changes: 112 additions & 0 deletions frontends/rioterm/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,32 @@ impl<T: EventListener + Clone + std::marker::Send + 'static> ContextManager<T> {
self.current_route = self.current().route_id;
}

#[inline]
pub fn move_current_to_prev(&mut self) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add just one test to ensure the functionality?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added two tests

let len = self.contexts.len();
if len <= 1 {
return;
}

let current = self.current_index;
let target_index = if current == 0 { len - 1 } else { current - 1 };
self.contexts.swap(current, target_index);
self.select_tab(target_index);
}

#[inline]
pub fn move_current_to_next(&mut self) {
let len = self.contexts.len();
if len <= 1 {
return;
}

let current = self.current_index;
let target_index = if current == len - 1 { 0 } else { current + 1 };
self.contexts.swap(current, target_index);
self.select_tab(target_index);
}

pub fn split(&mut self, rich_text_id: usize, split_down: bool) {
let mut working_dir = self.config.working_dir.clone();
if self.config.use_current_path {
Expand Down Expand Up @@ -1244,4 +1270,90 @@ pub mod test {
context_manager.switch_to_next();
assert_eq!(context_manager.current_index, 1);
}

#[test]
fn test_move_current_to_next() {
let window_id = WindowId::from(0);

let mut context_manager =
ContextManager::start_with_capacity(5, VoidListener {}, window_id).unwrap();
let should_redirect = false;

context_manager.current_mut().rich_text_id = 1;
context_manager.add_context(should_redirect, 0);
context_manager.add_context(should_redirect, 0);
context_manager.add_context(should_redirect, 0);
context_manager.add_context(should_redirect, 0);

assert_eq!(context_manager.len(), 5);
assert_eq!(context_manager.current_index, 0);
assert_eq!(context_manager.current().rich_text_id, 1);

context_manager.move_current_to_next();
assert_eq!(context_manager.current_index, 1);
assert_eq!(context_manager.current().rich_text_id, 1);

context_manager.move_current_to_next();
assert_eq!(context_manager.current_index, 2);
assert_eq!(context_manager.current().rich_text_id, 1);

context_manager.move_current_to_next();
assert_eq!(context_manager.current_index, 3);
assert_eq!(context_manager.current().rich_text_id, 1);

context_manager.move_current_to_next();
assert_eq!(context_manager.current_index, 4);
assert_eq!(context_manager.current().rich_text_id, 1);

context_manager.move_current_to_next();
assert_eq!(context_manager.current_index, 0);
assert_eq!(context_manager.current().rich_text_id, 1);

context_manager.move_current_to_next();
assert_eq!(context_manager.current_index, 1);
assert_eq!(context_manager.current().rich_text_id, 1);
}

#[test]
fn test_move_current_to_prev() {
let window_id = WindowId::from(0);

let mut context_manager =
ContextManager::start_with_capacity(5, VoidListener {}, window_id).unwrap();
let should_redirect = false;

context_manager.current_mut().rich_text_id = 1;
context_manager.add_context(should_redirect, 0);
context_manager.add_context(should_redirect, 0);
context_manager.add_context(should_redirect, 0);
context_manager.add_context(should_redirect, 0);

assert_eq!(context_manager.len(), 5);
assert_eq!(context_manager.current_index, 0);
assert_eq!(context_manager.current().rich_text_id, 1);

context_manager.move_current_to_prev();
assert_eq!(context_manager.current_index, 4);
assert_eq!(context_manager.current().rich_text_id, 1);

context_manager.move_current_to_prev();
assert_eq!(context_manager.current_index, 3);
assert_eq!(context_manager.current().rich_text_id, 1);

context_manager.move_current_to_prev();
assert_eq!(context_manager.current_index, 2);
assert_eq!(context_manager.current().rich_text_id, 1);

context_manager.move_current_to_prev();
assert_eq!(context_manager.current_index, 1);
assert_eq!(context_manager.current().rich_text_id, 1);

context_manager.move_current_to_prev();
assert_eq!(context_manager.current_index, 0);
assert_eq!(context_manager.current().rich_text_id, 1);

context_manager.move_current_to_prev();
assert_eq!(context_manager.current_index, 4);
assert_eq!(context_manager.current().rich_text_id, 1);
}
}
12 changes: 12 additions & 0 deletions frontends/rioterm/src/screen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,18 @@ impl Screen<'_> {
self.context_manager.switch_to_next();
self.render();
}
Act::MoveCurrentTabToPrev => {
self.cancel_search();
self.clear_selection();
self.context_manager.move_current_to_prev();
self.render();
}
Act::MoveCurrentTabToNext => {
self.cancel_search();
self.clear_selection();
self.context_manager.move_current_to_next();
self.render();
}
Act::SelectPrevTab => {
self.cancel_search();
self.clear_selection();
Expand Down
Loading