Flesh out command parsing & handling

This commit is contained in:
Drew DeVault 2019-03-10 21:23:22 -04:00
parent b60999c39e
commit d394fd1f3b
6 changed files with 48 additions and 23 deletions

View File

@ -3,10 +3,12 @@ package commands
import ( import (
"errors" "errors"
"github.com/google/shlex"
"git.sr.ht/~sircmpwn/aerc2/widgets" "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 ( var (
commands map[string]AercCommand commands map[string]AercCommand
@ -21,8 +23,15 @@ func Register(name string, cmd AercCommand) {
} }
func ExecuteCommand(aerc *widgets.Aerc, cmd string) error { func ExecuteCommand(aerc *widgets.Aerc, cmd string) error {
if fn, ok := commands[cmd]; ok { args, err := shlex.Split(cmd)
return fn(aerc, 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])
} }

View File

@ -1,15 +1,43 @@
package commands package commands
import ( import (
"errors"
"fmt"
"strconv"
"git.sr.ht/~sircmpwn/aerc2/widgets" "git.sr.ht/~sircmpwn/aerc2/widgets"
) )
func init() { 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 := aerc.SelectedAccount()
for ; n > 0; n-- {
if args[0] == "prev-folder" {
acct.Directories().Prev()
} else {
acct.Directories().Next() acct.Directories().Next()
}
}
return nil return nil
} }

View File

@ -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
}

1
go.mod
View File

@ -7,6 +7,7 @@ require (
github.com/gdamore/encoding v0.0.0-20151215212835-b23993cbb635 github.com/gdamore/encoding v0.0.0-20151215212835-b23993cbb635
github.com/gdamore/tcell v1.0.0 github.com/gdamore/tcell v1.0.0
github.com/go-ini/ini v1.32.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/kyoh86/xdg v0.0.0-20171127140545-8db68a8ea76a
github.com/lucasb-eyer/go-colorful v0.0.0-20180531031333-d9cec903b20c github.com/lucasb-eyer/go-colorful v0.0.0-20180531031333-d9cec903b20c
github.com/mattn/go-isatty v0.0.3 github.com/mattn/go-isatty v0.0.3

2
go.sum
View File

@ -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/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 h1:/MArBHSS0TFR28yPPDK1vPIjt4wUnPBfb81i6iiyKvA=
github.com/go-ini/ini v1.32.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8= 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 h1:vLFQnHOnCnmlySdpHAKF+mH7MhsthJgpBbfexVhHwxY=
github.com/kyoh86/xdg v0.0.0-20171127140545-8db68a8ea76a/go.mod h1:Z5mDqe0fxyxn3W2yTxsBAOQqIrXADQIh02wrTnaRM38= 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= github.com/lucasb-eyer/go-colorful v0.0.0-20180531031333-d9cec903b20c h1:b11Y3yxg40v2/9KUz76a4mSC1DMlgnPGAt+4pJSgmyU=

View File

@ -115,7 +115,7 @@ func (acct *AccountView) Event(event tcell.Event) bool {
err := acct.runCmd(command) err := acct.runCmd(command)
if err != nil { if err != nil {
acct.statusline.Push( acct.statusline.Push(
fmt.Sprintf("Error: %v", err), 3*time.Second). fmt.Sprintf("Error: %v", err), 10*time.Second).
Color(tcell.ColorRed, tcell.ColorDefault) Color(tcell.ColorRed, tcell.ColorDefault)
} }
acct.statusbar.Pop() acct.statusbar.Pop()