From cc066f218fb9915bf8df50247b482e15d6df148c Mon Sep 17 00:00:00 2001 From: Ryan Aidan Date: Thu, 20 Jun 2024 19:12:06 +0800 Subject: [PATCH] feat: change tabs with `h`, `l` and arrow keys (#12) * chore: Release * feat: switch tabs with "l", "r", right arrow and left arrow * feat: add keys to global keys popup --- Cargo.lock | 2 +- rm-main/Cargo.toml | 2 +- rm-main/src/action.rs | 4 ++++ rm-main/src/ui/components/tabs.rs | 17 ++++++++++++++--- rm-main/src/ui/global_popups/help.rs | 6 ++++-- rm-main/src/ui/mod.rs | 2 +- 6 files changed, 25 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ea876c5..79d0a1a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1252,7 +1252,7 @@ dependencies = [ [[package]] name = "rustmission" -version = "0.3.0" +version = "0.3.1" dependencies = [ "anyhow", "base64 0.22.1", diff --git a/rm-main/Cargo.toml b/rm-main/Cargo.toml index cc96ac2..e7166e5 100644 --- a/rm-main/Cargo.toml +++ b/rm-main/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rustmission" -version = "0.3.0" +version = "0.3.1" edition = "2021" description = "TUI for Transmission daemon" repository = "https://github.com/intuis/rustmission" diff --git a/rm-main/src/action.rs b/rm-main/src/action.rs index 1ec46f1..07a5d05 100644 --- a/rm-main/src/action.rs +++ b/rm-main/src/action.rs @@ -10,6 +10,8 @@ pub(crate) enum Action { Render, Up, Down, + Left, + Right, ScrollDownPage, ScrollUpPage, Home, @@ -95,6 +97,8 @@ fn keycode_to_action(key: KeyCode) -> Option { KeyCode::PageDown => Some(A::ScrollDownPage), KeyCode::Char('j') | KeyCode::Down => Some(A::Down), KeyCode::Char('k') | KeyCode::Up => Some(A::Up), + KeyCode::Char('h') | KeyCode::Left => Some(A::Left), + KeyCode::Char('l') | KeyCode::Right => Some(A::Right), KeyCode::Char('?') | KeyCode::F(1) => Some(A::ShowHelp), KeyCode::Char('s') => Some(A::ShowStats), KeyCode::Char('f') => Some(A::ShowFiles), diff --git a/rm-main/src/ui/components/tabs.rs b/rm-main/src/ui/components/tabs.rs index c5a2afb..af52d16 100644 --- a/rm-main/src/ui/components/tabs.rs +++ b/rm-main/src/ui/components/tabs.rs @@ -56,12 +56,23 @@ impl Component for TabComponent { } fn handle_actions(&mut self, action: Action) -> Option { - if let Action::ChangeTab(tab) = action { - match tab { + match action { + Action::ChangeTab(tab) => match tab { 1 => self.current_tab = CurrentTab::Torrents, 2 => self.current_tab = CurrentTab::Search, _ => (), - } + }, + // left only works on right-most tab (search) + Action::Left => match self.current_tab { + CurrentTab::Search => self.current_tab = CurrentTab::Torrents, + _ => (), + }, + // right only works on left-most tab (torrents) + Action::Right => match self.current_tab { + CurrentTab::Torrents => self.current_tab = CurrentTab::Search, + _ => (), + }, + _ => (), } None } diff --git a/rm-main/src/ui/global_popups/help.rs b/rm-main/src/ui/global_popups/help.rs index 381fe40..28cf734 100644 --- a/rm-main/src/ui/global_popups/help.rs +++ b/rm-main/src/ui/global_popups/help.rs @@ -72,6 +72,10 @@ impl Component for HelpPopup { add_line!(lines, "ESC", "close a popup / task"); add_line!(lines, "1", "switch to torrents tab"); add_line!(lines, "2", "switch to search tab"); + add_line!(lines, "h / ←", "switch to tab left of current tab"); + add_line!(lines, "l / →", "switch to tab right of current tab"); + add_line!(lines, "j / ↓", "move down"); + add_line!(lines, "k / ↑", "move up"); add_line!(lines, "/", "search or filter"); add_line!(lines, "TAB", "switch focus"); add_line!(lines, "Enter", "confirm"); @@ -79,8 +83,6 @@ impl Component for HelpPopup { add_line!(lines, "CTRL-u", "scroll page up"); add_line!(lines, "Home", "scroll to the beginning"); add_line!(lines, "End", "scroll to the end"); - add_line!(lines, "j / ↓", "move down"); - add_line!(lines, "k / ↑", "move up"); lines.push( Line::from(vec![Span::styled( diff --git a/rm-main/src/ui/mod.rs b/rm-main/src/ui/mod.rs index 279d33e..3db2bde 100644 --- a/rm-main/src/ui/mod.rs +++ b/rm-main/src/ui/mod.rs @@ -53,7 +53,7 @@ impl Component for MainWindow { _ if self.global_popup_manager.needs_action() => { self.global_popup_manager.handle_actions(action) } - A::ChangeTab(_) => { + A::ChangeTab(_) | A::Left | A::Right => { self.tabs.handle_actions(action); Some(A::Render) }