aboutsummaryrefslogtreecommitdiff
path: root/commands/account/next.go
blob: 870487ff9508dfe9c53585453c3969cfc387ae22 (plain)
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
package account

import (
	"errors"
	"strconv"
	"strings"

	"git.sr.ht/~rjarry/aerc/lib/ui"
	"git.sr.ht/~rjarry/aerc/widgets"
)

type NextPrevMsg struct {
	Amount  int `opt:"N" default:"1" metavar:"N[%]" parse:"ParseAmount"`
	Percent bool
}

func init() {
	register(NextPrevMsg{})
}

func (np *NextPrevMsg) ParseAmount(arg string) error {
	if strings.HasSuffix(arg, "%") {
		np.Percent = true
		arg = strings.TrimSuffix(arg, "%")
	}
	i, err := strconv.ParseInt(arg, 10, 64)
	if err != nil {
		return err
	}
	np.Amount = int(i)
	return nil
}

func (NextPrevMsg) Aliases() []string {
	return []string{"next", "next-message", "prev", "prev-message"}
}

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

func (np NextPrevMsg) Execute(aerc *widgets.Aerc, args []string) error {
	acct := aerc.SelectedAccount()
	if acct == nil {
		return errors.New("No account selected")
	}

	n := np.Amount
	if np.Percent {
		n = int(float64(acct.Messages().Height()) * (float64(n) / 100.0))
	}
	if args[0] == "prev-message" || args[0] == "prev" {
		store := acct.Store()
		if store != nil {
			store.NextPrev(-n)
			ui.Invalidate()
		}
	} else {
		store := acct.Store()
		if store != nil {
			store.NextPrev(n)
			ui.Invalidate()
		}
	}
	return nil
}