aboutsummaryrefslogtreecommitdiffstats
path: root/commands/ct.go
blob: 2d057b4fc89f02f08cb105ccedb6fc0cce0c1bbc (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
package commands

import (
	"errors"
	"strconv"
	"strings"

	"git.sr.ht/~rjarry/aerc/app"
)

type ChangeTab struct {
	Tab string `opt:"tab" complete:"CompleteTab"`
}

func init() {
	register(ChangeTab{})
}

func (ChangeTab) Aliases() []string {
	return []string{"ct", "change-tab"}
}

func (*ChangeTab) CompleteTab(arg string) []string {
	return CompletionFromList(app.TabNames(), arg)
}

func (c ChangeTab) Execute(args []string) error {
	if c.Tab == "-" {
		ok := app.SelectPreviousTab()
		if !ok {
			return errors.New("No previous tab to return to")
		}
	} else {
		n, err := strconv.Atoi(c.Tab)
		if err == nil {
			switch {
			case strings.HasPrefix(c.Tab, "+"):
				for ; n > 0; n-- {
					app.NextTab()
				}
			case strings.HasPrefix(c.Tab, "-"):
				for ; n < 0; n++ {
					app.PrevTab()
				}
			default:
				ok := app.SelectTabIndex(n)
				if !ok {
					return errors.New(
						"No tab with that index")
				}
			}
		} else {
			ok := app.SelectTab(c.Tab)
			if !ok {
				return errors.New("No tab with that name")
			}
		}
	}
	return nil
}