diff options
Diffstat (limited to 'commands/account/split.go')
-rw-r--r-- | commands/account/split.go | 61 |
1 files changed, 30 insertions, 31 deletions
diff --git a/commands/account/split.go b/commands/account/split.go index 7a5acc47..ef6d2647 100644 --- a/commands/account/split.go +++ b/commands/account/split.go @@ -8,12 +8,27 @@ import ( "git.sr.ht/~rjarry/aerc/widgets" ) -type Split struct{} +type Split struct { + Size int `opt:"N" required:"false" parse:"ParseSize"` + Delta bool +} func init() { register(Split{}) } +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"} } @@ -22,10 +37,7 @@ func (Split) Complete(aerc *widgets.Aerc, args []string) []string { return nil } -func (Split) Execute(aerc *widgets.Aerc, args []string) error { - if len(args) > 2 { - return errors.New("Usage: [v]split n") - } +func (s Split) Execute(aerc *widgets.Aerc, args []string) error { acct := aerc.SelectedAccount() if acct == nil { return errors.New("No account selected") @@ -34,45 +46,32 @@ func (Split) Execute(aerc *widgets.Aerc, args []string) error { if store == nil { return errors.New("Cannot perform action. Messages still loading") } - n := 0 - if acct.SplitSize() == 0 { + + if s.Size == 0 && acct.SplitSize() == 0 { if args[0] == "split" { - n = aerc.SelectedAccount().Messages().Height() / 4 + s.Size = aerc.SelectedAccount().Messages().Height() / 4 } else { - n = aerc.SelectedAccount().Messages().Width() / 2 + s.Size = aerc.SelectedAccount().Messages().Width() / 2 } } - - var err error - if len(args) > 1 { - delta := false - if strings.HasPrefix(args[1], "+") || strings.HasPrefix(args[1], "-") { - delta = true - } - n, err = strconv.Atoi(args[1]) - if err != nil { - return errors.New("Usage: [v]split n") - } - if delta { - n = acct.SplitSize() + n - acct.SetSplitSize(n) - return nil - } + if s.Delta { + acct.SetSplitSize(acct.SplitSize() + s.Size) + return nil } - if n == acct.SplitSize() { + if s.Size == acct.SplitSize() { // Repeated commands of the same size have the effect of // toggling the split - n = 0 + s.Size = 0 } - if n < 0 { + if s.Size < 0 { // Don't allow split to go negative - n = 1 + s.Size = 1 } switch args[0] { case "split": - return acct.Split(n) + return acct.Split(s.Size) case "vsplit": - return acct.Vsplit(n) + return acct.Vsplit(s.Size) } return nil } |