Debian Codegraph @

commands/account/split.go

raw
1 package account
2
3 import (
4 "errors"
5 "strconv"
6 "strings"
7
8 "git.sr.ht/~rjarry/aerc/app"
9 "git.sr.ht/~rjarry/aerc/commands"
10 )
11
12 type Split struct {
13 Size int `opt:"n" minus:"true" required:"false" action:"ParseSize"`
14 Delta bool
15 }
16
17 func init() {
18 commands.Register(Split{})
19 }
20
21 func (Split) Description() string {
22 return "Split the message list with a preview pane."
23 }
24
25 func (Split) Context() commands.CommandContext {
26 return commands.MESSAGE_LIST
27 }
28
29 func (s *Split) ParseSize(arg string) error {
30 i, err := strconv.ParseInt(arg, 10, 64)
31 if err != nil {
32 return err
33 }
34 s.Size = int(i)
35 if strings.HasPrefix(arg, "+") || strings.HasPrefix(arg, "-") {
36 s.Delta = true
37 }
38 return nil
39 }
40
41 func (Split) Aliases() []string {
42 return []string{"split", "vsplit", "hsplit"}
43 }
44
45 func (s Split) Execute(args []string) error {
46 acct := app.SelectedAccount()
47 if acct == nil {
48 return errors.New("No account selected")
49 }
50 store := app.SelectedAccount().Store()
51 if store == nil {
52 return errors.New("Cannot perform action. Messages still loading")
53 }
54
55 if s.Size == 0 && acct.SplitSize() == 0 {
56 if args[0] == "split" || args[0] == "hsplit" {
57 s.Size = app.SelectedAccount().Messages().Height() / 4
58 } else {
59 s.Size = app.SelectedAccount().Messages().Width() / 2
60 }
61 }
62 if s.Delta {
63 acct.SetSplitSize(acct.SplitSize() + s.Size)
64 return nil
65 }
66 if s.Size == acct.SplitSize() {
67 // Repeated commands of the same size have the effect of
68 // toggling the split
69 s.Size = 0
70 }
71 if s.Size < 0 {
72 // Don't allow split to go negative
73 s.Size = 1
74 }
75 switch args[0] {
76 case "split", "hsplit":
77 acct.Split(s.Size)
78 case "vsplit":
79 acct.Vsplit(s.Size)
80 }
81 return nil
82 }