From d394fd1f3b8625c7cc1c1f483f35d5b137a28c7c Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sun, 10 Mar 2019 21:23:22 -0400 Subject: [PATCH] Flesh out command parsing & handling --- commands/commands.go | 17 +++++++++++++---- commands/next-folder.go | 34 +++++++++++++++++++++++++++++++--- commands/prev-folder.go | 15 --------------- go.mod | 1 + go.sum | 2 ++ widgets/account.go | 2 +- 6 files changed, 48 insertions(+), 23 deletions(-) delete mode 100644 commands/prev-folder.go diff --git a/commands/commands.go b/commands/commands.go index 71dabe4..49a8b46 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -3,10 +3,12 @@ package commands import ( "errors" + "github.com/google/shlex" + "git.sr.ht/~sircmpwn/aerc2/widgets" ) -type AercCommand func(aerc *widgets.Aerc, cmd string) error +type AercCommand func(aerc *widgets.Aerc, args []string) error var ( commands map[string]AercCommand @@ -21,8 +23,15 @@ func Register(name string, cmd AercCommand) { } func ExecuteCommand(aerc *widgets.Aerc, cmd string) error { - if fn, ok := commands[cmd]; ok { - return fn(aerc, cmd) + args, err := shlex.Split(cmd) + if err != nil { + return err } - return errors.New("Unknown command " + cmd) + if len(args) == 0 { + return errors.New("Expected a command.") + } + if fn, ok := commands[args[0]]; ok { + return fn(aerc, args) + } + return errors.New("Unknown command " + args[0]) } diff --git a/commands/next-folder.go b/commands/next-folder.go index 02975e1..ed11615 100644 --- a/commands/next-folder.go +++ b/commands/next-folder.go @@ -1,15 +1,43 @@ package commands import ( + "errors" + "fmt" + "strconv" + "git.sr.ht/~sircmpwn/aerc2/widgets" ) func init() { - Register("next-folder", NextFolder) + Register("next-folder", NextPrevFolder) + Register("prev-folder", NextPrevFolder) } -func NextFolder(aerc *widgets.Aerc, cmd string) error { +func usage(cmd string) error { + return errors.New(fmt.Sprintf("Usage: %s [n]", cmd)) +} + +func NextPrevFolder(aerc *widgets.Aerc, args []string) error { + if len(args) > 2 { + return usage(args[0]) + } + var ( + n int = 1 + err error + ) + if len(args) > 1 { + n, err = strconv.Atoi(args[1]) + if err != nil { + return usage(args[0]) + } + } acct := aerc.SelectedAccount() - acct.Directories().Next() + for ; n > 0; n-- { + if args[0] == "prev-folder" { + acct.Directories().Prev() + } else { + acct.Directories().Next() + } + } return nil } diff --git a/commands/prev-folder.go b/commands/prev-folder.go deleted file mode 100644 index a38214f..0000000 --- a/commands/prev-folder.go +++ /dev/null @@ -1,15 +0,0 @@ -package commands - -import ( - "git.sr.ht/~sircmpwn/aerc2/widgets" -) - -func init() { - Register("prev-folder", PrevFolder) -} - -func PrevFolder(aerc *widgets.Aerc, cmd string) error { - acct := aerc.SelectedAccount() - acct.Directories().Prev() - return nil -} diff --git a/go.mod b/go.mod index f70969c..05f0880 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/gdamore/encoding v0.0.0-20151215212835-b23993cbb635 github.com/gdamore/tcell v1.0.0 github.com/go-ini/ini v1.32.0 + github.com/google/shlex v0.0.0-20181106134648-c34317bd91bf github.com/kyoh86/xdg v0.0.0-20171127140545-8db68a8ea76a github.com/lucasb-eyer/go-colorful v0.0.0-20180531031333-d9cec903b20c github.com/mattn/go-isatty v0.0.3 diff --git a/go.sum b/go.sum index c6e943f..be1bff1 100644 --- a/go.sum +++ b/go.sum @@ -10,6 +10,8 @@ github.com/gdamore/tcell v1.0.0 h1:oaly4AkxvDT5ffKHV/n4L8iy6FxG2QkAVl0M6cjryuE= github.com/gdamore/tcell v1.0.0/go.mod h1:tqyG50u7+Ctv1w5VX67kLzKcj9YXR/JSBZQq/+mLl1A= github.com/go-ini/ini v1.32.0 h1:/MArBHSS0TFR28yPPDK1vPIjt4wUnPBfb81i6iiyKvA= github.com/go-ini/ini v1.32.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8= +github.com/google/shlex v0.0.0-20181106134648-c34317bd91bf h1:7+FW5aGwISbqUtkfmIpZJGRgNFg2ioYPvFaUxdqpDsg= +github.com/google/shlex v0.0.0-20181106134648-c34317bd91bf/go.mod h1:RpwtwJQFrIEPstU94h88MWPXP2ektJZ8cZ0YntAmXiE= github.com/kyoh86/xdg v0.0.0-20171127140545-8db68a8ea76a h1:vLFQnHOnCnmlySdpHAKF+mH7MhsthJgpBbfexVhHwxY= github.com/kyoh86/xdg v0.0.0-20171127140545-8db68a8ea76a/go.mod h1:Z5mDqe0fxyxn3W2yTxsBAOQqIrXADQIh02wrTnaRM38= github.com/lucasb-eyer/go-colorful v0.0.0-20180531031333-d9cec903b20c h1:b11Y3yxg40v2/9KUz76a4mSC1DMlgnPGAt+4pJSgmyU= diff --git a/widgets/account.go b/widgets/account.go index dc339cb..be1e068 100644 --- a/widgets/account.go +++ b/widgets/account.go @@ -115,7 +115,7 @@ func (acct *AccountView) Event(event tcell.Event) bool { err := acct.runCmd(command) if err != nil { acct.statusline.Push( - fmt.Sprintf("Error: %v", err), 3*time.Second). + fmt.Sprintf("Error: %v", err), 10*time.Second). Color(tcell.ColorRed, tcell.ColorDefault) } acct.statusbar.Pop()