Unbind forward slash keybinding when not in table view. #150 #149

pull/153/head
Miguel Mota 3 years ago
parent 08e81cabb8
commit b0dd16f813
No known key found for this signature in database
GPG Key ID: 67EC1161588A00F9

@ -10,5 +10,17 @@ func (ct *Cointop) SetActiveView(v string) error {
} else if v == ct.Views.Table.Name() {
ct.g.SetViewOnTop(ct.Views.Statusbar.Name())
}
// TODO: better way to map/unmap shortcut key actions based on active view
if v == ct.Views.Table.Name() {
if err := ct.SetKeybindingAction("/", "open_search"); err != nil {
return err
}
} else {
// deletes binding to allow using "/" key on input fields
if err := ct.DeleteKeybinding("/"); err != nil {
return err
}
}
return nil
}

@ -486,7 +486,7 @@ func (ct *Cointop) Run() error {
ui.SetMouse(true)
ui.SetHighlight(true)
ui.SetManagerFunc(ct.layout)
if err := ct.Keybindings(ct.g); err != nil {
if err := ct.SetKeybindings(); err != nil {
return fmt.Errorf("keybindings: %v", err)
}

@ -167,17 +167,16 @@ func (ct *Cointop) ParseKeys(s string) (interface{}, gocui.Modifier) {
return key, mod
}
// Keybindings sets keyboard shortcut key bindings
func (ct *Cointop) Keybindings(g *gocui.Gui) error {
for k, v := range ct.State.shortcutKeys {
if k == "" {
continue
// SetKeybindingAction maps a shortcut key to an action
func (ct *Cointop) SetKeybindingAction(shortcutKey string, action string) error {
if shortcutKey == "" {
return nil
}
v = strings.TrimSpace(strings.ToLower(v))
action = strings.TrimSpace(strings.ToLower(action))
var fn func(g *gocui.Gui, v *gocui.View) error
key, mod := ct.ParseKeys(k)
key, mod := ct.ParseKeys(shortcutKey)
view := "table"
switch v {
switch action {
case "move_up":
fn = ct.Keyfn(ct.CursorUp)
case "move_down":
@ -327,6 +326,15 @@ func (ct *Cointop) Keybindings(g *gocui.Gui) error {
}
ct.SetKeybindingMod(key, mod, fn, view)
return nil
}
// SetKeybindings sets keyboard shortcut key bindings
func (ct *Cointop) SetKeybindings() error {
for k, v := range ct.State.shortcutKeys {
if err := ct.SetKeybindingAction(k, v); err != nil {
return err
}
}
// keys to force quit
@ -352,6 +360,9 @@ func (ct *Cointop) Keybindings(g *gocui.Gui) error {
// keys to update portfolio holdings
ct.SetKeybindingMod(gocui.KeyEnter, gocui.ModNone, ct.Keyfn(ct.EnterKeyPressHandler), ct.Views.Input.Name())
key, mod := ct.ParseKeys("/")
ct.DeleteKeybindingMod(key, mod, "")
// mouse events
ct.SetKeybindingMod(gocui.MouseRelease, gocui.ModNone, ct.Keyfn(ct.MouseRelease), "")
ct.SetKeybindingMod(gocui.MouseLeft, gocui.ModNone, ct.Keyfn(ct.MouseLeftClick), "")
@ -382,11 +393,32 @@ func (ct *Cointop) SetKeybindingMod(key interface{}, mod gocui.Modifier, callbac
return err
}
// DeleteKeybinding ...
func (ct *Cointop) DeleteKeybinding(shortcutKey string) error {
key, mod := ct.ParseKeys(shortcutKey)
return ct.DeleteKeybindingMod(key, mod, "")
}
// DeleteKeybindingMod ...
func (ct *Cointop) DeleteKeybindingMod(key interface{}, mod gocui.Modifier, view string) error {
var err error
switch t := key.(type) {
case gocui.Key:
err = ct.g.DeleteKeybinding(view, t, mod)
case rune:
err = ct.g.DeleteKeybinding(view, t, mod)
}
return err
}
// Keyfn returns the keybinding function as a wrapped gocui view function
func (ct *Cointop) Keyfn(fn func() error) func(g *gocui.Gui, v *gocui.View) error {
return func(g *gocui.Gui, v *gocui.View) error {
if fn != nil {
return fn()
}
return nil
}
}
// handleHkey handles the h key

@ -7,13 +7,13 @@ import (
"github.com/Knetic/govaluate"
)
// EvaluateExpression ...
func EvaluateExpressionToFloat64(expressionInput string) (float64, error) {
expressionInput = strings.TrimSpace(expressionInput) // remove trailing \0s
if expressionInput == "" {
// EvaluateExpression evaulates a simple math expression string to a float64
func EvaluateExpressionToFloat64(input string) (float64, error) {
input = strings.TrimSpace(input) // remove trailing \0s
if input == "" {
return 0, nil
}
expression, err := govaluate.NewEvaluableExpression(expressionInput)
expression, err := govaluate.NewEvaluableExpression(input)
if err != nil {
return 0, err
}

Loading…
Cancel
Save