From 1ff788ffd5f552a5392fc44dfb46078dfd2ff6c5 Mon Sep 17 00:00:00 2001 From: raziman Date: Sun, 14 Feb 2021 21:37:31 +0800 Subject: [PATCH] clean up --- anko/anko.go | 67 ++++++++++++++++------------------------------------ start.go | 16 ++++++------- 2 files changed, 28 insertions(+), 55 deletions(-) diff --git a/anko/anko.go b/anko/anko.go index e793218..3e23fea 100644 --- a/anko/anko.go +++ b/anko/anko.go @@ -2,8 +2,6 @@ package anko import ( "errors" - "context" - "reflect" "fmt" "github.com/mattn/anko/core" @@ -27,23 +25,23 @@ func NewAnko() Anko { } } -// define defines new symbol and value to the Anko env +// Define defines new symbol and value to the Anko env. func (a *Anko) Define(symbol string, value interface{}) error { return a.env.DefineGlobal(symbol, value) } -// set sets new value to existing symbol. Use this when change value under an +// Set sets new value to existing symbol. Use this when change value under an // existing symbol. func (a *Anko) Set(symbol string, value interface{}) error { return a.env.Set(symbol, value) } -// get gets value from anko env, returns error if symbol is not found. +// Get gets value from anko env, returns error if symbol is not found. func (a *Anko) Get(symbol string) (interface{}, error) { return a.env.Get(symbol) } -// getInt gets int value from symbol, returns golang default value if not found +// GetInt gets int value from symbol, returns golang default value if not found. func (a *Anko) GetInt(symbol string) int { v, err := a.env.Get(symbol) if err != nil { @@ -58,8 +56,8 @@ func (a *Anko) GetInt(symbol string) int { return int(val) } -// getString gets string value from symbol, returns golang default value if not -// found +// GetString gets string value from symbol, returns golang default value if not +// found. func (a *Anko) GetString(symbol string) string { v, err := a.env.Get(symbol) if err != nil { @@ -74,8 +72,8 @@ func (a *Anko) GetString(symbol string) string { return val } -// getBool gets bool value from symbol, returns golang default value if not -// found +// GetBool gets bool value from symbol, returns golang default value if not +// found. func (a *Anko) GetBool(symbol string) bool { v, err := a.env.Get(symbol) if err != nil { @@ -90,52 +88,29 @@ func (a *Anko) GetBool(symbol string) bool { return val } -// execute executes anko script +// Execute executes anko script. func (a *Anko) Execute(src string) (interface{}, error) { return vm.Execute(a.env, nil, src) } -func (a *Anko) ExecKeybind(panel string, keybind string, cb func(error)) error { - - kb, err := a.Get("keybinds") +// KeybindExists checks if keybinding is defined. +func (a *Anko) KeybindExists(panel string, keybind string) bool { + src := fmt.Sprintf("keybinds.%s.%s", panel, keybind) + val, err := a.Execute(src) if err != nil { - return err + return false } - p, ok := kb.(map[interface{}]interface{}) - if !ok { - return fmt.Errorf("%w: require type {} got %T", ErrInvalidType, kb) - } + return val != nil +} - k, ok := p[panel] - if !ok { - return ErrNoKeybind - } +// ExecKeybind executes function bounded by the keybinding. +func (a *Anko) ExecKeybind(panel string, keybind string, cb func(error)) { - keybinds, ok := k.(map[interface{}]interface{}) - if !ok { - return fmt.Errorf("%w: require type {} got %T", ErrInvalidType, k) - } - - cmd, ok := keybinds[keybind] - if !ok { - return ErrNoKeybind - } - - f, ok := cmd.(func(context.Context) (reflect.Value, reflect.Value)) - if !ok { - return fmt.Errorf("%w: require type func()", ErrInvalidType) - } + src := fmt.Sprintf("keybinds.%s.%s()", panel, keybind) go func() { - _, execErr := f(context.Background()) - if err := execErr.Interface(); !execErr.IsNil() { - if err, ok := err.(error); ok { - cb(err) - } - } + _, err := a.Execute(src) + cb(err) }() - - - return nil } diff --git a/start.go b/start.go index 3dddd61..82aae6e 100644 --- a/start.go +++ b/start.go @@ -13,7 +13,6 @@ import ( "syscall" "github.com/gdamore/tcell/v2" - "github.com/issadarkthing/gomu/anko" "github.com/rivo/tview" "github.com/ztrue/tracerr" ) @@ -233,15 +232,14 @@ func start(application *tview.Application, args Args) { gomu.cyclePanels2() } - // check for user defined keybindings - err := gomu.anko.ExecKeybind("global", string(e.Rune()), func (err error) { - if err != nil { - errorPopup(tracerr.Wrap(err)) - } - }) + if gomu.anko.KeybindExists("global", string(e.Rune())) { + // check for user defined keybindings + gomu.anko.ExecKeybind("global", string(e.Rune()), func (err error) { + if err != nil { + errorPopup(tracerr.Wrap(err)) + } + }) - if err != nil && !errors.Is(err, anko.ErrNoKeybind) { - errorPopup(tracerr.Wrap(err)) return e }