1
0
mirror of https://git.sr.ht/~sircmpwn/aerc synced 2024-11-23 16:42:07 +01:00
aerc/commands/ct.go
Jeffas 39307a6fa7 Make commands join args with spaces
This patch ensures the following commands join their arguments with
spaces to make it easier to interact with:

- cf
- mkdir
- cd
- attach
- detach
- ct
- copy
- move
- save
2019-09-20 15:06:34 -04:00

73 lines
1.4 KiB
Go

package commands
import (
"errors"
"fmt"
"strconv"
"strings"
"git.sr.ht/~sircmpwn/aerc/widgets"
)
type ChangeTab struct{}
func init() {
register(ChangeTab{})
}
func (ChangeTab) Aliases() []string {
return []string{"ct", "change-tab"}
}
func (ChangeTab) Complete(aerc *widgets.Aerc, args []string) []string {
if len(args) == 0 {
return aerc.TabNames()
}
joinedArgs := strings.Join(args, " ")
out := make([]string, 0)
for _, tab := range aerc.TabNames() {
if strings.HasPrefix(tab, joinedArgs) {
out = append(out, tab)
}
}
return out
}
func (ChangeTab) Execute(aerc *widgets.Aerc, args []string) error {
if len(args) == 1 {
return fmt.Errorf("Usage: %s <tab>", args[0])
}
joinedArgs := strings.Join(args[1:], " ")
if joinedArgs == "-" {
ok := aerc.SelectPreviousTab()
if !ok {
return errors.New("No previous tab to return to")
}
} else {
n, err := strconv.Atoi(joinedArgs)
if err == nil {
if strings.HasPrefix(joinedArgs, "+") {
for ; n > 0; n-- {
aerc.NextTab()
}
} else if strings.HasPrefix(joinedArgs, "-") {
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(joinedArgs)
if !ok {
return errors.New("No tab with that name")
}
}
}
return nil
}