mirror of
https://github.com/issadarkthing/gomu.git
synced 2025-04-26 13:49:21 +08:00
clean up
This commit is contained in:
parent
a358b3a2b2
commit
1ff788ffd5
65
anko/anko.go
65
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 {
|
||||
_, err := a.Execute(src)
|
||||
cb(err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
|
||||
return nil
|
||||
}
|
||||
|
6
start.go
6
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()
|
||||
}
|
||||
|
||||
if gomu.anko.KeybindExists("global", string(e.Rune())) {
|
||||
// check for user defined keybindings
|
||||
err := gomu.anko.ExecKeybind("global", string(e.Rune()), func (err error) {
|
||||
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
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user