issue-9: added clearing of existing bindings for keys

This commit is contained in:
Leon Mika 2022-08-25 22:31:33 +10:00
parent 7c5bfd27a3
commit d9c9e5d845
7 changed files with 84 additions and 68 deletions

View file

@ -6,7 +6,6 @@ import (
"fmt"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/lmika/audax/internal/common/ui/commandctrl"
@ -17,10 +16,11 @@ import (
"github.com/lmika/audax/internal/dynamo-browse/providers/dynamo"
"github.com/lmika/audax/internal/dynamo-browse/providers/workspacestore"
"github.com/lmika/audax/internal/dynamo-browse/services/itemrenderer"
"github.com/lmika/audax/internal/dynamo-browse/services/keybindings"
keybindings_service "github.com/lmika/audax/internal/dynamo-browse/services/keybindings"
"github.com/lmika/audax/internal/dynamo-browse/services/tables"
workspaces_service "github.com/lmika/audax/internal/dynamo-browse/services/workspaces"
"github.com/lmika/audax/internal/dynamo-browse/ui"
"github.com/lmika/audax/internal/dynamo-browse/ui/keybindings"
"github.com/lmika/audax/internal/dynamo-browse/ui/teamodels/styles"
"github.com/lmika/gopkgs/cli"
"log"
@ -81,24 +81,9 @@ func main() {
state := controllers.NewState()
tableReadController := controllers.NewTableReadController(state, tableService, workspaceService, itemRendererService, *flagTable, true)
tableWriteController := controllers.NewTableWriteController(state, tableService, tableReadController)
keyBindings := keybindings.Default()
defaultKeyBindings := &ui.KeyBindings{
View: &ui.ViewKeyBindings{
Mark: key.NewBinding(key.WithKeys("m"), key.WithHelp("m", "mark")),
CopyItemToClipboard: key.NewBinding(key.WithKeys("c"), key.WithHelp("c", "copy item to clipboard")),
Rescan: key.NewBinding(key.WithKeys("R"), key.WithHelp("R", "rescan")),
PromptForQuery: key.NewBinding(key.WithKeys("?"), key.WithHelp("?", "prompt for query")),
PromptForFilter: key.NewBinding(key.WithKeys("f"), key.WithHelp("/", "filter")),
ViewBack: key.NewBinding(key.WithKeys("backspace"), key.WithHelp("backspace", "go back")),
ViewForward: key.NewBinding(key.WithKeys("\\"), key.WithHelp("\\", "go forward")),
CycleLayoutForward: key.NewBinding(key.WithKeys("w"), key.WithHelp("w", "cycle layout forward")),
CycleLayoutBackwards: key.NewBinding(key.WithKeys("W"), key.WithHelp("W", "cycle layout backward")),
PromptForCommand: key.NewBinding(key.WithKeys(":"), key.WithHelp(":", "prompt for command")),
Quit: key.NewBinding(key.WithKeys("ctrl+c", "esc"), key.WithHelp("ctrl+c/esc", "quit")),
},
}
keyBindingService := keybindings.NewService(defaultKeyBindings)
keyBindingService := keybindings_service.NewService(keyBindings)
keyBindingController := controllers.NewKeyBindingController(keyBindingService)
commandController := commandctrl.NewCommandController()
@ -109,7 +94,7 @@ func main() {
itemRendererService,
commandController,
keyBindingController,
defaultKeyBindings,
keyBindings,
)
// Pre-determine if layout has dark background. This prevents calls for creating a list to hang.

View file

@ -16,21 +16,21 @@ func NewKeyBindingController(service *keybindings.Service) *KeyBindingController
return &KeyBindingController{service: service}
}
func (kb *KeyBindingController) Rebind(newKey string, bindingName string) tea.Msg {
err := kb.service.Rebind(newKey, bindingName, false)
func (kb *KeyBindingController) Rebind(bindingName string, newKey string) tea.Msg {
err := kb.service.Rebind(bindingName, newKey, false)
if err == nil {
return events.SetStatus(fmt.Sprintf("Key '%v' now bound to '%v'", newKey, bindingName))
return events.SetStatus(fmt.Sprintf("Binding '%v' now bound to '%v'", bindingName, newKey))
}
var keyAlreadyBoundErr keybindings.KeyAlreadyBoundError
if errors.As(err, &keyAlreadyBoundErr) {
promptMsg := fmt.Sprintf("Key '%v' already bound to '%v'. Continue? ", keyAlreadyBoundErr.Key, keyAlreadyBoundErr.ExistingBindingName)
return events.Confirm(promptMsg, func() tea.Msg {
err := kb.service.Rebind(newKey, bindingName, true)
err := kb.service.Rebind(bindingName, newKey, true)
if err != nil {
return events.Error(err)
}
return events.SetStatus(fmt.Sprintf("Key '%v' now bound to '%v'", newKey, bindingName))
return events.SetStatus(fmt.Sprintf("Binding '%v' now bound to '%v'", bindingName, newKey))
})
}

View file

@ -3,6 +3,7 @@ package keybindings
import (
"github.com/charmbracelet/bubbles/key"
"github.com/pkg/errors"
"log"
"reflect"
"strings"
)
@ -22,23 +23,28 @@ func NewService(keyBinding any) *Service {
}
}
func (s *Service) Rebind(newKey string, name string, force bool) error {
// Check if there already exists a binding
if !force {
var foundBinding = ""
s.walkBindingFields(func(bindingName string, binding key.Binding) bool {
for _, key := range binding.Keys() {
if key == newKey {
func (s *Service) Rebind(name string, newKey string, force bool) error {
// Check if there already exists a binding (or clear it)
var foundBinding = ""
s.walkBindingFields(func(bindingName string, binding *key.Binding) bool {
for _, boundKey := range binding.Keys() {
if boundKey == newKey {
if force {
// TODO: only filter out "boundKey" rather clear
log.Printf("clearing binding of %v", bindingName)
*binding = key.NewBinding()
return true
} else {
foundBinding = bindingName
return false
}
}
return true
})
if foundBinding != "" {
return KeyAlreadyBoundError{Key: newKey, ExistingBindingName: foundBinding}
}
return true
})
if foundBinding != "" {
return KeyAlreadyBoundError{Key: newKey, ExistingBindingName: foundBinding}
}
// Rebind
@ -80,11 +86,11 @@ func (s *Service) findFieldForBindingInGroup(group reflect.Value, name string) *
return nil
}
func (s *Service) walkBindingFields(fn func(name string, binding key.Binding) bool) {
func (s *Service) walkBindingFields(fn func(name string, binding *key.Binding) bool) {
s.walkBindingFieldsInGroup(s.keyBindingValue, "", fn)
}
func (s *Service) walkBindingFieldsInGroup(group reflect.Value, prefix string, fn func(name string, binding key.Binding) bool) bool {
func (s *Service) walkBindingFieldsInGroup(group reflect.Value, prefix string, fn func(name string, binding *key.Binding) bool) bool {
groupType := group.Type()
for i := 0; i < group.NumField(); i++ {
fieldType := groupType.Field(i)
@ -108,7 +114,7 @@ func (s *Service) walkBindingFieldsInGroup(group reflect.Value, prefix string, f
if !isBinding {
continue
}
if !fn(fullName, *binding) {
if !fn(fullName, binding) {
return false
}
}

View file

@ -0,0 +1,31 @@
package keybindings
import "github.com/charmbracelet/bubbles/key"
func Default() *KeyBindings {
return &KeyBindings{
TableView: &TableKeyBinding{
MoveUp: key.NewBinding(key.WithKeys("i", "up")),
MoveDown: key.NewBinding(key.WithKeys("k", "down")),
PageUp: key.NewBinding(key.WithKeys("I", "pgup")),
PageDown: key.NewBinding(key.WithKeys("K", "pgdown")),
Home: key.NewBinding(key.WithKeys("0", "home")),
End: key.NewBinding(key.WithKeys("$", "end")),
ColLeft: key.NewBinding(key.WithKeys("j", "left")),
ColRight: key.NewBinding(key.WithKeys("l", "right")),
},
View: &ViewKeyBindings{
Mark: key.NewBinding(key.WithKeys("m"), key.WithHelp("m", "mark")),
CopyItemToClipboard: key.NewBinding(key.WithKeys("c"), key.WithHelp("c", "copy item to clipboard")),
Rescan: key.NewBinding(key.WithKeys("R"), key.WithHelp("R", "rescan")),
PromptForQuery: key.NewBinding(key.WithKeys("?"), key.WithHelp("?", "prompt for query")),
PromptForFilter: key.NewBinding(key.WithKeys("f"), key.WithHelp("/", "filter")),
ViewBack: key.NewBinding(key.WithKeys("backspace"), key.WithHelp("backspace", "go back")),
ViewForward: key.NewBinding(key.WithKeys("\\"), key.WithHelp("\\", "go forward")),
CycleLayoutForward: key.NewBinding(key.WithKeys("w"), key.WithHelp("w", "cycle layout forward")),
CycleLayoutBackwards: key.NewBinding(key.WithKeys("W"), key.WithHelp("W", "cycle layout backward")),
PromptForCommand: key.NewBinding(key.WithKeys(":"), key.WithHelp(":", "prompt for command")),
Quit: key.NewBinding(key.WithKeys("ctrl+c", "esc"), key.WithHelp("ctrl+c/esc", "quit")),
},
}
}

View file

@ -1,9 +1,21 @@
package ui
package keybindings
import "github.com/charmbracelet/bubbles/key"
type KeyBindings struct {
View *ViewKeyBindings `keymap:"view"`
TableView *TableKeyBinding `keymap:"item-table"`
View *ViewKeyBindings `keymap:"view"`
}
type TableKeyBinding struct {
MoveUp key.Binding `keymap:"move-up"`
MoveDown key.Binding `keymap:"move-down"`
PageUp key.Binding `keymap:"page-up"`
PageDown key.Binding `keymap:"page-down"`
Home key.Binding `keymap:"goto-top"`
End key.Binding `keymap:"goto-bottom"`
ColLeft key.Binding `keymap:"move-left"`
ColRight key.Binding `keymap:"move-right"`
}
type ViewKeyBindings struct {

View file

@ -8,6 +8,7 @@ import (
"github.com/lmika/audax/internal/dynamo-browse/controllers"
"github.com/lmika/audax/internal/dynamo-browse/models"
"github.com/lmika/audax/internal/dynamo-browse/services/itemrenderer"
"github.com/lmika/audax/internal/dynamo-browse/ui/keybindings"
"github.com/lmika/audax/internal/dynamo-browse/ui/teamodels/dialogprompt"
"github.com/lmika/audax/internal/dynamo-browse/ui/teamodels/dynamoitemedit"
"github.com/lmika/audax/internal/dynamo-browse/ui/teamodels/dynamoitemview"
@ -46,7 +47,7 @@ type Model struct {
tableView *dynamotableview.Model
itemView *dynamoitemview.Model
mainView tea.Model
keyMap *ViewKeyBindings
keyMap *keybindings.ViewKeyBindings
}
func NewModel(
@ -55,11 +56,11 @@ func NewModel(
itemRendererService *itemrenderer.Service,
cc *commandctrl.CommandController,
keyBindingController *controllers.KeyBindingController,
defaultKeyMap *KeyBindings,
defaultKeyMap *keybindings.KeyBindings,
) Model {
uiStyles := styles.DefaultStyles
dtv := dynamotableview.New(uiStyles)
dtv := dynamotableview.New(defaultKeyMap.TableView, uiStyles)
div := dynamoitemview.New(itemRendererService, uiStyles)
mainView := layout.NewVBox(layout.LastChildFixedAt(14), dtv, div)

View file

@ -6,6 +6,7 @@ import (
"github.com/charmbracelet/lipgloss"
"github.com/lmika/audax/internal/dynamo-browse/controllers"
"github.com/lmika/audax/internal/dynamo-browse/models"
"github.com/lmika/audax/internal/dynamo-browse/ui/keybindings"
"github.com/lmika/audax/internal/dynamo-browse/ui/teamodels/dynamoitemview"
"github.com/lmika/audax/internal/dynamo-browse/ui/teamodels/frame"
"github.com/lmika/audax/internal/dynamo-browse/ui/teamodels/layout"
@ -20,22 +21,11 @@ var (
Background(lipgloss.Color("#4479ff"))
)
type KeyBinding struct {
MoveUp key.Binding
MoveDown key.Binding
PageUp key.Binding
PageDown key.Binding
Home key.Binding
End key.Binding
ColLeft key.Binding
ColRight key.Binding
}
type Model struct {
frameTitle frame.FrameTitle
table table.Model
w, h int
keyBinding KeyBinding
keyBinding *keybindings.TableKeyBinding
// model state
colOffset int
@ -43,7 +33,7 @@ type Model struct {
resultSet *models.ResultSet
}
func New(uiStyles styles.Styles) *Model {
func New(keyBinding *keybindings.TableKeyBinding, uiStyles styles.Styles) *Model {
tbl := table.New(table.SimpleColumns([]string{"pk", "sk"}), 100, 100)
rows := make([]table.Row, 0)
tbl.SetRows(rows)
@ -53,16 +43,7 @@ func New(uiStyles styles.Styles) *Model {
return &Model{
frameTitle: frameTitle,
table: tbl,
keyBinding: KeyBinding{
MoveUp: key.NewBinding(key.WithKeys("i", "up")),
MoveDown: key.NewBinding(key.WithKeys("k", "down")),
PageUp: key.NewBinding(key.WithKeys("I", "pgup")),
PageDown: key.NewBinding(key.WithKeys("K", "pgdown")),
Home: key.NewBinding(key.WithKeys("0", "home")),
End: key.NewBinding(key.WithKeys("$", "end")),
ColLeft: key.NewBinding(key.WithKeys("j", "left")),
ColRight: key.NewBinding(key.WithKeys("l", "right")),
},
keyBinding: keyBinding,
}
}