about summary refs log tree commit diff stats
path: root/commands/ct.go
blob: 4e66331c5fa1d8761f13bcf284c3128bbf32c81e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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()
	}
	out := make([]string, 0)
	for _, tab := range aerc.TabNames() {
		if strings.HasPrefix(tab, args[0]) {
			out = append(out, tab)
		}
	}
	return out
}

func (_ ChangeTab) Execute(aerc *widgets.Aerc, args []string) error {
	if len(args) != 2 {
		return errors.New(fmt.Sprintf("Usage: %s <tab>", args[0]))
	}
	if args[1] == "-" {
		ok := aerc.SelectPreviousTab()
		if !ok {
			return errors.New("No previous tab to return to")
		}
	} else {
		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")
			}
		}
	}
	return nil
}