diff options
author | Drew DeVault <sir@cmpwn.com> | 2019-03-10 21:23:22 -0400 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2019-03-10 21:23:22 -0400 |
commit | d394fd1f3b8625c7cc1c1f483f35d5b137a28c7c (patch) | |
tree | 88158be6dc45b9ff507aaccf8b85c04fba9e4bf6 | |
parent | b60999c39e11bf4d1e236f2b10a2f895b44d23fb (diff) |
Flesh out command parsing & handling
-rw-r--r-- | commands/commands.go | 17 | ||||
-rw-r--r-- | commands/next-folder.go | 34 | ||||
-rw-r--r-- | commands/prev-folder.go | 15 | ||||
-rw-r--r-- | go.mod | 1 | ||||
-rw-r--r-- | go.sum | 2 | ||||
-rw-r--r-- | widgets/account.go | 2 |
6 files changed, 48 insertions, 23 deletions
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 + } + 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 " + cmd) + 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 usage(cmd string) error { + return errors.New(fmt.Sprintf("Usage: %s [n]", cmd)) } -func NextFolder(aerc *widgets.Aerc, cmd string) error { +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 -} @@ -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 @@ -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() |