2019-07-19 17:12:57 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-07-26 21:41:13 +00:00
|
|
|
"strconv"
|
2019-07-19 17:12:57 +00:00
|
|
|
"strings"
|
|
|
|
|
2021-11-05 09:19:46 +00:00
|
|
|
"git.sr.ht/~rjarry/aerc/widgets"
|
2019-07-19 17:12:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ChangeTab struct{}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
register(ChangeTab{})
|
|
|
|
}
|
|
|
|
|
2019-09-03 19:34:03 +00:00
|
|
|
func (ChangeTab) Aliases() []string {
|
2019-07-19 17:12:57 +00:00
|
|
|
return []string{"ct", "change-tab"}
|
|
|
|
}
|
|
|
|
|
2019-09-03 19:34:03 +00:00
|
|
|
func (ChangeTab) Complete(aerc *widgets.Aerc, args []string) []string {
|
2019-07-26 13:29:40 +00:00
|
|
|
if len(args) == 0 {
|
|
|
|
return aerc.TabNames()
|
|
|
|
}
|
2019-09-20 16:16:29 +00:00
|
|
|
joinedArgs := strings.Join(args, " ")
|
2022-03-15 01:55:51 +00:00
|
|
|
return FilterList(aerc.TabNames(), joinedArgs, "", aerc.SelectedAccountUiConfig().FuzzyComplete)
|
2019-07-19 17:12:57 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 19:34:03 +00:00
|
|
|
func (ChangeTab) Execute(aerc *widgets.Aerc, args []string) error {
|
2019-09-20 16:16:29 +00:00
|
|
|
if len(args) == 1 {
|
2019-09-03 19:34:04 +00:00
|
|
|
return fmt.Errorf("Usage: %s <tab>", args[0])
|
2019-07-19 17:12:57 +00:00
|
|
|
}
|
2019-09-20 16:16:29 +00:00
|
|
|
joinedArgs := strings.Join(args[1:], " ")
|
|
|
|
if joinedArgs == "-" {
|
2019-07-19 17:12:57 +00:00
|
|
|
ok := aerc.SelectPreviousTab()
|
|
|
|
if !ok {
|
|
|
|
return errors.New("No previous tab to return to")
|
|
|
|
}
|
|
|
|
} else {
|
2019-09-20 16:16:29 +00:00
|
|
|
n, err := strconv.Atoi(joinedArgs)
|
2019-07-26 21:41:13 +00:00
|
|
|
if err == nil {
|
2019-09-20 16:16:29 +00:00
|
|
|
if strings.HasPrefix(joinedArgs, "+") {
|
2019-07-26 21:41:13 +00:00
|
|
|
for ; n > 0; n-- {
|
|
|
|
aerc.NextTab()
|
|
|
|
}
|
2019-09-20 16:16:29 +00:00
|
|
|
} else if strings.HasPrefix(joinedArgs, "-") {
|
2019-07-26 21:41:13 +00:00
|
|
|
for ; n < 0; n++ {
|
|
|
|
aerc.PrevTab()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ok := aerc.SelectTabIndex(n)
|
|
|
|
if !ok {
|
|
|
|
return errors.New(
|
|
|
|
"No tab with that index")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2019-09-20 16:16:29 +00:00
|
|
|
ok := aerc.SelectTab(joinedArgs)
|
2019-07-26 21:41:13 +00:00
|
|
|
if !ok {
|
|
|
|
return errors.New("No tab with that name")
|
|
|
|
}
|
2019-07-19 17:12:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|