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

Add Switch to Next/Prev Split or Tab command #995

Merged
merged 4 commits into from
Mar 2, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
31 changes: 17 additions & 14 deletions docs/docs/key-bindings.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,28 @@ Execute a predefined action in Rio terminal.

### [Split Actions](#split-actions)

| Action | Description |
| :-------------- | :------------------------------------------------------------------------- |
| SplitRight | Create a split by right side |
| SplitDown | Create a split by under current pane |
| SelectNextSplit | Select next split |
| SelectPrevSplit | Select previous split |
| CloseSplitOrTab | Close split, if split is the last then will close the tab |
| Action | Description |
| :------------------- | :------------------------------------------------------------------------- |
| SplitRight | Create a split by right side |
| SplitDown | Create a split by under current pane |
| SelectNextSplit | Select next split |
| SelectPrevSplit | Select previous split |
| CloseSplitOrTab | Close split, if split is the last then will close the tab |
| SelectNextSplitOrTab | Select next split if available if not next tab |
| SelectPrevSplitOrTab | Select previous split if available if not previous tab |

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

| Action | Description |
| :------------------- | :---------------------------------------------------------------------- |
| CreateTab | |
| CloseTab | |
| CloseUnfocusedTabs | |
| SelectPrevTab | |
| SelectNextTab | |
| SelectLastTab | |
| MoveCurrentTabToPrev | Move the current focused tab to the previous slot if any is available |
| CreateTab | Create new tab |
| CloseTab | Close current tab |
| CloseUnfocusedTabs | Close all tabs that are not currently focused |
| SelectNextTab | Select next tab |
| SelectPrevTab | Select pervious tab |
| SelectLastTab | Select last tab |
| MoveCurrentTabToNext | Move the current focused tab to the next slot, or first when last |
| MoveCurrentTabToPrev | Move the current focused tab to the previous slot, or last when first |
| SelectTab(tab_index) | Example: Select first tab `SelectTab(0)`, second tab `SelectTab(1)` |

### [Scroll Actions](#scroll-actions)
Expand Down
11 changes: 11 additions & 0 deletions frontends/rioterm/src/bindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ impl From<String> for Action {
"splitdown" => Some(Action::SplitDown),
"selectnextsplit" => Some(Action::SelectNextSplit),
"selectprevsplit" => Some(Action::SelectPrevSplit),
"selectnextsplitortab" => Some(Action::SelectNextSplitOrTab),
"selectprevsplitortab" => Some(Action::SelectPrevSplitOrTab),
"togglevimode" => Some(Action::ToggleViMode),
"togglefullscreen" => Some(Action::ToggleFullscreen),
"none" => Some(Action::None),
Expand Down Expand Up @@ -465,9 +467,18 @@ pub enum Action {
/// Split vertically
SplitDown,

/// Select next split
SelectNextSplit,

/// Select previous split
SelectPrevSplit,

/// Select next split if available if not next tab
SelectNextSplitOrTab,

/// Select previous split if available if not previous tab
SelectPrevSplitOrTab,

/// Allow receiving char input.
ReceiveChar,

Expand Down
29 changes: 29 additions & 0 deletions frontends/rioterm/src/context/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,21 @@ impl<T: rio_backend::event::EventListener> ContextGrid<T> {
}
}

#[inline]
pub fn select_next_split_no_loop(&mut self) -> bool {
if self.inner.len() == 1 {
return false;
}

if self.current >= self.inner.len() - 1 {
return false;
} else {
self.current += 1;
}

return true;
}

#[inline]
pub fn select_prev_split(&mut self) {
if self.inner.len() == 1 {
Expand All @@ -153,6 +168,20 @@ impl<T: rio_backend::event::EventListener> ContextGrid<T> {
}
}

#[inline]
pub fn select_prev_split_no_loop(&mut self) -> bool {
if self.inner.len() == 1 {
return false;
}

if self.current == 0 {
return false;
} else {
self.current -= 1;
}
return true;
}

#[inline]
#[allow(unused)]
pub fn current_index(&self) -> usize {
Expand Down
26 changes: 26 additions & 0 deletions frontends/rioterm/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,32 @@ impl<T: EventListener + Clone + std::marker::Send + 'static> ContextManager<T> {
self.current_route = self.current().route_id;
}

#[inline]
pub fn switch_to_next_split_or_tab(&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.

everything looks good, could you just add one test for next and prev so we make sure it always work 🙏

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Thanks for the patience.

if self.contexts[self.current_index].select_next_split_no_loop() {
self.current_route = self.current().route_id;
return;
}
self.switch_to_next();
// Make sure first split is selected
let current_tab = &mut self.contexts[self.current_index];
current_tab.current = 0;
self.current_route = self.current().route_id;
}

#[inline]
pub fn switch_to_prev_split_or_tab(&mut self) {
if self.contexts[self.current_index].select_prev_split_no_loop() {
self.current_route = self.current().route_id;
return;
}
self.switch_to_prev();
// Make sure last split is selected
let current_tab = &mut self.contexts[self.current_index];
current_tab.current = current_tab.len() - 1;
self.current_route = self.current().route_id;
}

#[inline]
pub fn select_tab(&mut self, tab_index: usize) {
if self.config.is_native {
Expand Down
10 changes: 10 additions & 0 deletions frontends/rioterm/src/screen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,16 @@ impl Screen<'_> {
self.context_manager.select_prev_split();
self.render();
}
Act::SelectNextSplitOrTab => {
self.cancel_search();
self.context_manager.switch_to_next_split_or_tab();
self.render();
}
Act::SelectPrevSplitOrTab => {
self.cancel_search();
self.context_manager.switch_to_prev_split_or_tab();
self.render();
}
Act::SelectTab(tab_index) => {
self.context_manager.select_tab(*tab_index);
self.cancel_search();
Expand Down
Loading