Skip to content

Commit

Permalink
Add support for reverse tab traversal
Browse files Browse the repository at this point in the history
  • Loading branch information
heppu committed Aug 11, 2023
1 parent 4e83795 commit d414537
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions internal/ui/browser/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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"),
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
Expand All @@ -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()

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit d414537

Please sign in to comment.