aboutsummaryrefslogtreecommitdiffstats
path: root/commands/account/split.go
blob: 5f09f21b88ce1e40db8e4829d2dc65f371757c63 (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
70
71
72
73
74
75
76
77
78
package account

import (
	"errors"
	"strconv"
	"strings"

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

type Split struct {
	Size  int `opt:"n" required:"false" action:"ParseSize"`
	Delta bool
}

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

func (Split) Context() commands.CommandContext {
	return commands.ACCOUNT
}

func (s *Split) ParseSize(arg string) error {
	i, err := strconv.ParseInt(arg, 10, 64)
	if err != nil {
		return err
	}
	s.Size = int(i)
	if strings.HasPrefix(arg, "+") || strings.HasPrefix(arg, "-") {
		s.Delta = true
	}
	return nil
}

func (Split) Aliases() []string {
	return []string{"split", "vsplit", "hsplit"}
}

func (s Split) Execute(args []string) error {
	acct := app.SelectedAccount()
	if acct == nil {
		return errors.New("No account selected")
	}
	store := app.SelectedAccount().Store()
	if store == nil {
		return errors.New("Cannot perform action. Messages still loading")
	}

	if s.Size == 0 && acct.SplitSize() == 0 {
		if args[0] == "split" || args[0] == "hsplit" {
			s.Size = app.SelectedAccount().Messages().Height() / 4
		} else {
			s.Size = app.SelectedAccount().Messages().Width() / 2
		}
	}
	if s.Delta {
		acct.SetSplitSize(acct.SplitSize() + s.Size)
		return nil
	}
	if s.Size == acct.SplitSize() {
		// Repeated commands of the same size have the effect of
		// toggling the split
		s.Size = 0
	}
	if s.Size < 0 {
		// Don't allow split to go negative
		s.Size = 1
	}
	switch args[0] {
	case "split", "hsplit":
		acct.Split(s.Size)
	case "vsplit":
		acct.Vsplit(s.Size)
	}
	return nil
}