update keybindings to use a file similar to original dive.config file (just requires caseing changes)

Signed-off-by: dwillist <dthornton@vmware.com>
This commit is contained in:
dwillist 2021-02-01 01:32:34 -05:00
commit a7a0db6d90
4 changed files with 54 additions and 106 deletions

View file

@ -7,10 +7,8 @@ import (
"path"
"strings"
"github.com/gdamore/tcell/v2"
"github.com/wagoodman/dive/dive"
"github.com/wagoodman/dive/dive/filetree"
"github.com/wagoodman/dive/runtime/ui/components"
"github.com/mitchellh/go-homedir"
log "github.com/sirupsen/logrus"
@ -80,76 +78,24 @@ func initConfig() {
viper.SetDefault("log.path", "./dive.log")
viper.SetDefault("log.enabled", true)
// keybindings: status view / global
// This keybinding can be changed but ctrl+C may still quit the application
viper.SetDefault("keybinding.quit", components.NewKeyBinding(
"Quit",
tcell.NewEventKey(tcell.KeyCtrlC, rune(0), tcell.ModCtrl),
))
viper.SetDefault("keybinding.toggle-view", components.NewKeyBinding(
"Switch View",
tcell.NewEventKey(tcell.KeyTab, rune(0), tcell.ModNone),
))
viper.SetDefault("keybinding.filter-files", components.NewKeyBinding(
"Find",
tcell.NewEventKey(tcell.KeyCtrlF, rune(0), tcell.ModCtrl),
))
// keybindings: status view / global
viper.SetDefault("keybinding.quit", "Ctrl+C")
viper.SetDefault("keybinding.toggle-view", "Tab")
viper.SetDefault("keybinding.filter-files", "Ctrl+f")
// keybindings: layer view
viper.SetDefault("keybinding.compare-all", components.NewKeyBinding(
"Compare All",
tcell.NewEventKey(tcell.KeyCtrlA, rune(0), tcell.ModCtrl),
))
viper.SetDefault("keybinding.compare-layer", components.NewKeyBinding(
"Compare Layer",
tcell.NewEventKey(tcell.KeyCtrlL, rune(0), tcell.ModCtrl),
))
viper.SetDefault("keybinding.compare-all", "Ctrl+A")
viper.SetDefault("keybinding.compare-layer", "Ctrl+L")
// keybindings: filetree view
viper.SetDefault("keybinding.toggle-collapse-dir", components.NewKeyBinding(
"Collapse",
tcell.NewEventKey(tcell.KeyRune, ' ', tcell.ModNone),
))
viper.SetDefault("keybinding.toggle-collapse-all-dir", components.NewKeyBinding(
"Collapse All",
tcell.NewEventKey(tcell.KeyCtrlSpace, rune(0), tcell.ModCtrl),
))
viper.SetDefault("keybinding.toggle-filetree-attributes", components.NewKeyBinding(
"Attributes",
tcell.NewEventKey(tcell.KeyCtrlB, rune(0), tcell.ModCtrl),
))
viper.SetDefault("keybinding.toggle-added-files", components.NewKeyBinding(
"Added",
tcell.NewEventKey(tcell.KeyCtrlA, rune(0), tcell.ModCtrl),
))
viper.SetDefault("keybinding.toggle-removed-files", components.NewKeyBinding(
"Removed",
tcell.NewEventKey(tcell.KeyCtrlR, rune(0), tcell.ModCtrl),
))
viper.SetDefault("keybinding.toggle-modified-files", components.NewKeyBinding(
"Modified",
tcell.NewEventKey(tcell.KeyCtrlM, rune(0), tcell.ModCtrl),
))
viper.SetDefault("keybinding.toggle-unmodified-files", components.NewKeyBinding(
"Unmodified",
tcell.NewEventKey(tcell.KeyCtrlU, rune(0), tcell.ModCtrl),
))
viper.SetDefault("keybinding.page-up", components.NewKeyBinding(
"Pg Up",
tcell.NewEventKey(tcell.KeyPgUp, rune(0), tcell.ModNone),
))
viper.SetDefault("keybinding.page-down", components.NewKeyBinding(
"Pg Down",
tcell.NewEventKey(tcell.KeyPgDn, rune(0), tcell.ModNone),
))
viper.SetDefault("keybinding.toggle-collapse-dir", "Space")
viper.SetDefault("keybinding.toggle-collapse-all-dir", "Ctrl+Space")
viper.SetDefault("keybinding.toggle-filetree-attributes", "Ctrl+B")
viper.SetDefault("keybinding.toggle-added-files", "Ctrl+A")
viper.SetDefault("keybinding.toggle-removed-files", "Ctrl+R")
viper.SetDefault("keybinding.toggle-modified-files", "Ctrl+N")
viper.SetDefault("keybinding.toggle-unmodified-files", "Ctrl+U")
viper.SetDefault("keybinding.toggle-wrap-tree", "Ctrl+P")
viper.SetDefault("keybinding.page-up", "PgUp")
viper.SetDefault("keybinding.page-down", "PgDn")
viper.SetDefault("diff.hide", "")

1
go.mod
View file

@ -39,6 +39,7 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.6.1 // indirect
gitlab.com/tslocum/cbind v0.1.4
go.uber.org/zap v1.10.0
golang.org/x/net v0.0.0-20201029055024-942e2f445f3c
golang.org/x/sys v0.0.0-20201218084310-7d0127a74742 // indirect

4
go.sum
View file

@ -167,6 +167,7 @@ github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWo
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell/v2 v2.0.0-dev/go.mod h1:vSVL/GV5mCSlPC6thFP5kfOFdM9MGZcalipmpTxTgQA=
github.com/gdamore/tcell/v2 v2.0.1-0.20201017141208-acf90d56d591 h1:0WWUDZ1oxq7NxVyGo8M3KI5jbkiwNAdZFFzAdC68up4=
github.com/gdamore/tcell/v2 v2.0.1-0.20201017141208-acf90d56d591/go.mod h1:vSVL/GV5mCSlPC6thFP5kfOFdM9MGZcalipmpTxTgQA=
github.com/gdamore/tcell/v2 v2.1.0 h1:UnSmozHgBkQi2PGsFr+rpdXuAPRRucMegpQp3Z3kDro=
@ -551,6 +552,8 @@ github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:
github.com/xeipuuv/gojsonschema v0.0.0-20180618132009-1d523034197f/go.mod h1:5yf86TLmAcydyeJq5YvxkGPE2fm/u4myDekKRoLuqhs=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
gitlab.com/tslocum/cbind v0.1.4 h1:cbZXPPcieXspk8cShoT6efz7HAT8yMNQcofYWNizis4=
gitlab.com/tslocum/cbind v0.1.4/go.mod h1:RvwYE3auSjBNlCmWeGspzn+jdLUVQ8C2QGC+0nP9ChI=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.etcd.io/etcd v0.0.0-20191023171146-3cf2f69b5738/go.mod h1:dnLIgRNXwCJa5e+c6mIZCrds/GIG4ncV9HhK5PX7jPg=
@ -679,6 +682,7 @@ golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201013132646-2da7054afaeb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201017003518-b09fb700fbb7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201218084310-7d0127a74742 h1:+CBz4km/0KPU3RGTwARGh/noP3bEwtHcq+0YcBQM2JQ=
golang.org/x/sys v0.0.0-20201218084310-7d0127a74742/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

View file

@ -6,16 +6,28 @@ import (
"github.com/gdamore/tcell/v2"
"github.com/spf13/viper"
"gitlab.com/tslocum/cbind"
)
// TODO move key constants out to their own file
var DisplayNames = map[string]string{
"keybinding.quit": "Quit",
"keybinding.toggle-view": "Switch View",
"keybinding.filter-files": "Find",
"keybinding.compare-all": "Compare All",
"keybinding.compare-layer": "Compare Layer",
"keybinding.toggle-collapse-dir": "Collapse",
"keybinding.toggle-collapse-all-dir": "Collapse All",
"keybinding.toggle-filetree-attributes": "Attributes",
"keybinding.toggle-added-files": "Added",
"keybinding.toggle-removed-files": "Removed",
"keybinding.toggle-modified-files": "Modified",
"keybinding.toggle-unmodified-files": "Unmodified",
"keybinding.page-up": "Pg Up",
"keybinding.page-down": "Pg Down",
}
// TODO move this to a more appropriate place
var KeyNames = extendKeyMaps(
tcell.KeyNames,
map[tcell.Key]string{
tcell.KeyCtrlM: "Ctrl-M",
})
type KeyConfig struct{}
type KeyBinding struct {
@ -25,8 +37,8 @@ type KeyBinding struct {
type KeyBindingDisplay struct {
*KeyBinding
Selected func () bool
Hide func () bool
Selected func() bool
Hide func() bool
}
func (kb *KeyBindingDisplay) Name() string {
@ -48,7 +60,7 @@ func (kb *KeyBindingDisplay) Name() string {
ok := false
key := kb.Key()
if s, ok = KeyNames[key]; !ok {
if s, ok = tcell.KeyNames[key]; !ok {
if key == tcell.KeyRune {
if kb.Rune() == rune(' ') {
s = "Space"
@ -75,15 +87,6 @@ func NewKeyBinding(name string, key *tcell.EventKey) KeyBinding {
}
}
//func NewKeyBindingDisplay(k tcell.Key, ch rune, modMask tcell.ModMask, name string, selected bool, hide bool) KeyBindingDisplay {
// kb := NewKeyBinding(name, tcell.NewEventKey(k, ch, modMask))
// return KeyBindingDisplay{
// KeyBinding: &kb,
// Selected: selected,
// Hide: hide,
// }
//}
func (k *KeyBinding) Match(event *tcell.EventKey) bool {
if k.Key() == tcell.KeyRune {
return k.Rune() == event.Rune() && (k.Modifiers() == event.Modifiers())
@ -110,23 +113,17 @@ func NewKeyConfig() *KeyConfig {
return &KeyConfig{}
}
func (k *KeyConfig) GetKeyBinding(key string) (result KeyBinding, err error) {
err = viper.UnmarshalKey(key, &result)
func (k *KeyConfig) GetKeyBinding(key string) (KeyBinding, error) {
name, ok := DisplayNames[key]
if !ok {
return KeyBinding{}, fmt.Errorf("no name for binding %q found", key)
}
keyName := viper.GetString(key)
mod, tKey, ch, err := cbind.Decode(keyName)
if err != nil {
return KeyBinding{}, err
return KeyBinding{}, fmt.Errorf("unable to create binding from dive.config file: %q", err)
}
return result, err
fmt.Printf("creating key event for %s\n", key)
fmt.Printf("mod %d, key %d, ch %s\n", mod, tKey, string(ch))
return NewKeyBinding(name, tcell.NewEventKey(tKey, ch, mod)), nil
}
func extendKeyMaps(m, extension map[tcell.Key]string) map[tcell.Key] string {
result := map[tcell.Key]string{}
for key, val := range m {
result[key] = val
}
for key, val := range extension {
result[key] = val
}
return result
}