Add index option to change-tab

This allows selection of a tab using its index. It attempts to parse the
given argument as a number, if it fails then it uses it as a name.

Also supports relative indexes using prefixed + or -.
This commit is contained in:
Jeffas 2019-07-26 22:41:13 +01:00 committed by Drew DeVault
parent 0ee7d30187
commit 989730d470
3 changed files with 37 additions and 6 deletions

View File

@ -3,6 +3,7 @@ package commands
import ( import (
"errors" "errors"
"fmt" "fmt"
"strconv"
"strings" "strings"
"git.sr.ht/~sircmpwn/aerc/widgets" "git.sr.ht/~sircmpwn/aerc/widgets"
@ -35,16 +36,34 @@ func (_ ChangeTab) Execute(aerc *widgets.Aerc, args []string) error {
if len(args) != 2 { if len(args) != 2 {
return errors.New(fmt.Sprintf("Usage: %s <tab>", args[0])) return errors.New(fmt.Sprintf("Usage: %s <tab>", args[0]))
} }
if args[1] == "-" { if args[1] == "-" {
ok := aerc.SelectPreviousTab() ok := aerc.SelectPreviousTab()
if !ok { if !ok {
return errors.New("No previous tab to return to") return errors.New("No previous tab to return to")
} }
} else { } else {
ok := aerc.SelectTab(args[1]) n, err := strconv.Atoi(args[1])
if !ok { if err == nil {
return errors.New("No tab with that name") if strings.HasPrefix(args[1], "+") {
for ; n > 0; n-- {
aerc.NextTab()
}
} else if strings.HasPrefix(args[1], "-") {
for ; n < 0; n++ {
aerc.PrevTab()
}
} else {
ok := aerc.SelectTabIndex(n)
if !ok {
return errors.New(
"No tab with that index")
}
}
} else {
ok := aerc.SelectTab(args[1])
if !ok {
return errors.New("No tab with that name")
}
} }
} }
return nil return nil

View File

@ -36,8 +36,10 @@ These commands work in any context.
*cd* <directory> *cd* <directory>
Changes aerc's current working directory. Changes aerc's current working directory.
*change-tab* <tabname> *change-tab* [+|-]<tab name or index>
Changes the focus to the tab with the name. Changes the focus to the tab with the given name. If a number is given,
it's treated as an index. If + or - is specified, the number is interpreted
as a delta from the selected tab.
*exec* <command...> *exec* <command...>
Executes an arbitrary command in the background. Executes an arbitrary command in the background.

View File

@ -268,6 +268,16 @@ func (aerc *Aerc) SelectTab(name string) bool {
return false return false
} }
func (aerc *Aerc) SelectTabIndex(index int) bool {
for i, _ := range aerc.tabs.Tabs {
if i == index {
aerc.tabs.Select(i)
return true
}
}
return false
}
func (aerc *Aerc) TabNames() []string { func (aerc *Aerc) TabNames() []string {
var names []string var names []string
for _, tab := range aerc.tabs.Tabs { for _, tab := range aerc.tabs.Tabs {