From d41453726b923ced2a38ad8421146cda3a8e173c Mon Sep 17 00:00:00 2001 From: Henri Koski Date: Fri, 11 Aug 2023 12:57:06 +0300 Subject: [PATCH] Add support for reverse tab traversal --- internal/ui/browser/browser.go | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/internal/ui/browser/browser.go b/internal/ui/browser/browser.go index 35c58df..dfb16fb 100644 --- a/internal/ui/browser/browser.go +++ b/internal/ui/browser/browser.go @@ -23,7 +23,8 @@ import ( // Keymap contains the key bindings for the browser type Keymap struct { CloseTab key.Binding - CycleTabs key.Binding + NextTab key.Binding + PrevTab key.Binding ShowHelp key.Binding ToggleOfflineMode key.Binding } @@ -34,9 +35,13 @@ var DefaultKeymap = Keymap{ key.WithKeys("c", "ctrl+w"), key.WithHelp("c", "Close tab"), ), - CycleTabs: key.NewBinding( + NextTab: key.NewBinding( key.WithKeys("tab"), - key.WithHelp("Tab", "Cycle tabs"), + key.WithHelp("Tab", "Next tab"), + ), + PrevTab: key.NewBinding( + key.WithKeys("shift+tab"), + key.WithHelp("Shift+Tab", "Previous tab"), ), ShowHelp: key.NewBinding( key.WithKeys("h", "ctrl+h"), @@ -51,7 +56,8 @@ var DefaultKeymap = Keymap{ // SetEnabled allows to disable/enable shortcuts func (k *Keymap) SetEnabled(enabled bool) { k.CloseTab.SetEnabled(enabled) - k.CycleTabs.SetEnabled(enabled) + k.NextTab.SetEnabled(enabled) + k.PrevTab.SetEnabled(enabled) k.ShowHelp.SetEnabled(enabled) k.ToggleOfflineMode.SetEnabled(enabled) } @@ -257,7 +263,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.msg = fmt.Sprintf("Closed tab - %s", m.tabs[m.activeTab].Title()) return m, nil - case key.Matches(msg, m.keymap.CycleTabs): + case key.Matches(msg, m.keymap.NextTab): m.activeTab++ if m.activeTab > len(m.tabs)-1 { m.activeTab = 0 @@ -266,6 +272,15 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.msg = "" return m, nil + case key.Matches(msg, m.keymap.PrevTab): + m.activeTab-- + if m.activeTab < 0 { + m.activeTab = len(m.tabs) - 1 + } + + m.msg = "" + return m, nil + case key.Matches(msg, m.keymap.ShowHelp): return m.showHelp() @@ -319,7 +334,7 @@ func (m Model) View() string { // ShortHelp returns the short help for the browser. func (m Model) ShortHelp() []key.Binding { - return []key.Binding{m.keymap.CloseTab, m.keymap.CycleTabs, m.keymap.ToggleOfflineMode} + return []key.Binding{m.keymap.CloseTab, m.keymap.NextTab, m.keymap.PrevTab, m.keymap.ToggleOfflineMode} } // FullHelp returns the full help for the browser.