aboutsummaryrefslogtreecommitdiffstats
path: root/commands/account/align.go
blob: e4c1379a2b9598df9d7590e49c570d060b1eaa6d (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 account

import (
	"errors"

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

type Align struct {
	Pos app.AlignPosition `opt:"pos" metavar:"top|center|bottom" action:"ParsePos" complete:"CompletePos"`
}

func init() {
	commands.Register(Align{})
}

var posNames []string = []string{"top", "center", "bottom"}

func (a *Align) ParsePos(arg string) error {
	switch arg {
	case "top":
		a.Pos = app.AlignTop
	case "center":
		a.Pos = app.AlignCenter
	case "bottom":
		a.Pos = app.AlignBottom
	default:
		return errors.New("invalid alignment")
	}
	return nil
}

func (a *Align) CompletePos(arg string) []string {
	return commands.FilterList(posNames, arg, commands.QuoteSpace)
}

func (Align) Context() commands.CommandContext {
	return commands.MESSAGE_LIST
}

func (Align) Aliases() []string {
	return []string{"align"}
}

func (a Align) Execute(args []string) error {
	acct := app.SelectedAccount()
	if acct == nil {
		return errors.New("no account selected")
	}
	msgList := acct.Messages()
	if msgList == nil {
		return errors.New("no message list available")
	}
	msgList.AlignMessage(a.Pos)
	ui.Invalidate()

	return nil
}