~sircmpwn/aerc

ref: a1467af03df18c01f7ac69f4bd787c53d2ff15f1 aerc/commands/move-tab.go -rw-r--r-- 862 bytes
a1467af0 — Daniel Sockwell Document pin-tab and unpin-tab commands 11 months ago
                                                                                
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
package commands

import (
	"fmt"
	"strconv"
	"strings"

	"git.sr.ht/~sircmpwn/aerc/widgets"
)

type MoveTab struct{}

func init() {
	register(MoveTab{})
}

func (MoveTab) Aliases() []string {
	return []string{"move-tab"}
}

func (MoveTab) Complete(aerc *widgets.Aerc, args []string) []string {
	return nil
}

func (MoveTab) Execute(aerc *widgets.Aerc, args []string) error {
	if len(args) == 1 {
		return fmt.Errorf("Usage: %s [+|-]<index>", args[0])
	}

	joinedArgs := strings.Join(args[1:], "")

	n, err := strconv.Atoi(joinedArgs)
	if err != nil {
		return fmt.Errorf("failed to parse index argument: %v", err)
	}

	i := aerc.SelectedTabIndex()
	l := aerc.NumTabs()

	if strings.HasPrefix(joinedArgs, "+") {
		i = (i + n) % l
	} else if strings.HasPrefix(joinedArgs, "-") {
		i = (((i + n) % l) + l) % l
	} else {
		i = n
	}

	aerc.MoveTab(i)

	return nil
}