aboutsummaryrefslogtreecommitdiffstats
path: root/commands/account/next.go
diff options
context:
space:
mode:
authorRobin Jarry <robin@jarry.cc>2023-10-03 22:12:10 +0200
committerRobin Jarry <robin@jarry.cc>2023-10-28 19:24:49 +0200
commite54486ee40c9cedde9d4dae3e89d8b5f932188ee (patch)
tree254f032ef94920c28fbb9be8f918e61082a2015d /commands/account/next.go
parent9a4518476d8c8f28340c6b44cd808e6d58fbeb98 (diff)
downloadaerc-e54486ee40c9cedde9d4dae3e89d8b5f932188ee.tar.gz
commands: parse arguments with go-opt
Use the argument parsing framework introduced earlier to unify the parsing of (almost) all command options. Remove custom parsing code and to avoid extraneous types, add fields with `opt` tags on command structs that have options and arguments. Commands that take no argument do not need anything. Since the command objects now carry data, create a new temporary instance of them before passing them to opt.ArgsToStruct when executing a command. A few of the commands use specific semantics for parsing (:choose), or are delegating argument parsing to another function (:sort, :search, :filter). For these commands, simply add a dummy "-" passthrough argument. Since all commands still have the argument list (after split) nothing needs to be changed in this area. There should be no functional change besides the Usage strings and reported errors which are now generated automatically. Signed-off-by: Robin Jarry <robin@jarry.cc> Reviewed-by: Koni Marti <koni.marti@gmail.com> Tested-by: Moritz Poldrack <moritz@poldrack.dev> Tested-by: Inwit <inwit@sindominio.net>
Diffstat (limited to 'commands/account/next.go')
-rw-r--r--commands/account/next.go57
1 files changed, 20 insertions, 37 deletions
diff --git a/commands/account/next.go b/commands/account/next.go
index fff36bd1..14679b1b 100644
--- a/commands/account/next.go
+++ b/commands/account/next.go
@@ -2,7 +2,6 @@ package account
import (
"errors"
- "fmt"
"strconv"
"strings"
@@ -10,12 +9,28 @@ import (
"git.sr.ht/~rjarry/aerc/lib/ui"
)
-type NextPrevMsg struct{}
+type NextPrevMsg struct {
+ Amount int `opt:"n" default:"1" metavar:"<n>[%]" action:"ParseAmount"`
+ Percent bool
+}
func init() {
register(NextPrevMsg{})
}
+func (np *NextPrevMsg) ParseAmount(arg string) error {
+ if strings.HasSuffix(arg, "%") {
+ np.Percent = true
+ arg = strings.TrimSuffix(arg, "%")
+ }
+ i, err := strconv.ParseInt(arg, 10, 64)
+ if err != nil {
+ return err
+ }
+ np.Amount = int(i)
+ return nil
+}
+
func (NextPrevMsg) Aliases() []string {
return []string{"next", "next-message", "prev", "prev-message"}
}
@@ -24,42 +39,14 @@ func (NextPrevMsg) Complete(args []string) []string {
return nil
}
-func (NextPrevMsg) Execute(args []string) error {
- n, pct, err := ParseNextPrevMessage(args)
- if err != nil {
- return err
- }
+func (np NextPrevMsg) Execute(args []string) error {
acct := app.SelectedAccount()
if acct == nil {
return errors.New("No account selected")
}
- return ExecuteNextPrevMessage(args, acct, pct, n)
-}
-
-func ParseNextPrevMessage(args []string) (int, bool, error) {
- if len(args) > 2 {
- return 0, false, nextPrevMessageUsage(args[0])
- }
- var (
- n int = 1
- err error
- pct bool
- )
- if len(args) > 1 {
- if strings.HasSuffix(args[1], "%") {
- pct = true
- args[1] = args[1][:len(args[1])-1]
- }
- n, err = strconv.Atoi(args[1])
- if err != nil {
- return 0, false, nextPrevMessageUsage(args[0])
- }
- }
- return n, pct, nil
-}
-func ExecuteNextPrevMessage(args []string, acct *app.AccountView, pct bool, n int) error {
- if pct {
+ n := np.Amount
+ if np.Percent {
n = int(float64(acct.Messages().Height()) * (float64(n) / 100.0))
}
if args[0] == "prev-message" || args[0] == "prev" {
@@ -77,7 +64,3 @@ func ExecuteNextPrevMessage(args []string, acct *app.AccountView, pct bool, n in
}
return nil
}
-
-func nextPrevMessageUsage(cmd string) error {
- return fmt.Errorf("Usage: %s [<n>[%%]]", cmd)
-}