aboutsummaryrefslogtreecommitdiffstats
path: root/commands/msg/fold.go
blob: 057d454e155c6058ed5196924f98418432d5fae6 (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
package msg

import (
	"errors"

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

type Fold struct {
	All    bool `opt:"-a"`
	Toggle bool `opt:"-t"`
}

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

func (Fold) Context() commands.CommandContext {
	return commands.MESSAGE
}

func (Fold) Aliases() []string {
	return []string{"fold", "unfold"}
}

func (f Fold) Execute(args []string) error {
	h := newHelper()
	store, err := h.store()
	if err != nil {
		return err
	}

	if f.All {
		point := store.SelectedUid()
		uids := store.Uids()
		for _, uid := range uids {
			t, err := store.Thread(uid)
			if err == nil && t.Parent == nil {
				switch args[0] {
				case "fold":
					err = store.Fold(uid, f.Toggle)
				case "unfold":
					err = store.Unfold(uid, f.Toggle)
				}
			}
			if err != nil {
				return err
			}
		}
		store.Select(point)
		ui.Invalidate()
		return err
	}

	msg := store.Selected()
	if msg == nil {
		return errors.New("No message selected")
	}

	switch args[0] {
	case "fold":
		err = store.Fold(msg.Uid, f.Toggle)
	case "unfold":
		err = store.Unfold(msg.Uid, f.Toggle)
	}
	ui.Invalidate()
	return err
}