2019-07-19 19:12:57 +02:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-07-26 23:41:13 +02:00
|
|
|
"strconv"
|
2019-07-19 19:12:57 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.sr.ht/~sircmpwn/aerc/widgets"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ChangeTab struct{}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
register(ChangeTab{})
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (ChangeTab) Aliases() []string {
|
2019-07-19 19:12:57 +02:00
|
|
|
return []string{"ct", "change-tab"}
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (ChangeTab) Complete(aerc *widgets.Aerc, args []string) []string {
|
2019-07-26 15:29:40 +02:00
|
|
|
if len(args) == 0 {
|
|
|
|
return aerc.TabNames()
|
|
|
|
}
|
2019-07-19 19:12:57 +02:00
|
|
|
out := make([]string, 0)
|
|
|
|
for _, tab := range aerc.TabNames() {
|
|
|
|
if strings.HasPrefix(tab, args[0]) {
|
|
|
|
out = append(out, tab)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (ChangeTab) Execute(aerc *widgets.Aerc, args []string) error {
|
2019-07-19 19:12:57 +02:00
|
|
|
if len(args) != 2 {
|
2019-09-03 21:34:04 +02:00
|
|
|
return fmt.Errorf("Usage: %s <tab>", args[0])
|
2019-07-19 19:12:57 +02:00
|
|
|
}
|
|
|
|
if args[1] == "-" {
|
|
|
|
ok := aerc.SelectPreviousTab()
|
|
|
|
if !ok {
|
|
|
|
return errors.New("No previous tab to return to")
|
|
|
|
}
|
|
|
|
} else {
|
2019-07-26 23:41:13 +02:00
|
|
|
n, err := strconv.Atoi(args[1])
|
|
|
|
if err == nil {
|
|
|
|
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")
|
|
|
|
}
|
2019-07-19 19:12:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|