diff --git a/button.go b/button.go index 689baf7..38400f7 100644 --- a/button.go +++ b/button.go @@ -151,11 +151,11 @@ func (b *Button) Draw(screen tcell.Screen) { func (b *Button) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) { return b.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) { // Process key event. - if matchesKeys(event, Keys.Select) { + if HitShortcut(event, Keys.Select) { if b.selected != nil { b.selected() } - } else if matchesKeys(event, Keys.Cancel, Keys.PreviousField, Keys.NextField) { + } else if HitShortcut(event, Keys.Cancel, Keys.PreviousField, Keys.NextField) { if b.blur != nil { b.blur(event.Key()) } diff --git a/doc.go b/doc.go index 8569ccc..a8bc622 100644 --- a/doc.go +++ b/doc.go @@ -146,7 +146,7 @@ feel of the primitives to your preferred style. Keyboard Shortcuts Widgets use keyboard shortcuts (a.k.a. keybindings) such as arrow keys and -H/J/k/L by default. You may replace these defaults by modifying the shortcuts +H/J/K/L by default. You may replace these defaults by modifying the shortcuts listed in Keys. You may also override keyboard shortcuts globally by setting a handler with Application.SetInputCapture. diff --git a/keys.go b/keys.go index be291c6..0a1b1db 100644 --- a/keys.go +++ b/keys.go @@ -1,5 +1,10 @@ package cview +import ( + "github.com/gdamore/tcell" + "gitlab.com/tslocum/cbind" +) + // Key defines the keyboard shortcuts of an application. type Key struct { Cancel []string @@ -39,3 +44,22 @@ var Keys = Key{ ShowContextMenu: []string{"Alt+Enter"}, } + +// HitShortcut returns whether the EventKey provided is present in one or more +// sets of keybindings. +func HitShortcut(event *tcell.EventKey, keybindings ...[]string) bool { + enc, err := cbind.Encode(event.Modifiers(), event.Key(), event.Rune()) + if err != nil { + return false + } + + for _, binds := range keybindings { + for _, key := range binds { + if key == enc { + return true + } + } + } + + return false +} diff --git a/list.go b/list.go index 95a42ce..beed54f 100644 --- a/list.go +++ b/list.go @@ -852,7 +852,7 @@ func (l *List) InputHandler() func(event *tcell.EventKey, setFocus func(p Primit previousItem := l.currentItem - if matchesKeys(event, Keys.Cancel) { + if HitShortcut(event, Keys.Cancel) { if l.ContextMenu.open { l.Unlock() @@ -867,7 +867,7 @@ func (l *List) InputHandler() func(event *tcell.EventKey, setFocus func(p Primit l.Unlock() } return - } else if matchesKeys(event, Keys.Select) { + } else if HitShortcut(event, Keys.Select) { if l.currentItem >= 0 && l.currentItem < len(l.items) { item := l.items[l.currentItem] if item.Enabled { @@ -883,7 +883,7 @@ func (l *List) InputHandler() func(event *tcell.EventKey, setFocus func(p Primit } } } - } else if matchesKeys(event, Keys.ShowContextMenu) { + } else if HitShortcut(event, Keys.ShowContextMenu) { // Do we show any shortcuts? var showShortcuts bool for _, item := range l.items { @@ -939,17 +939,17 @@ func (l *List) InputHandler() func(event *tcell.EventKey, setFocus func(p Primit } if !matchesShortcut { - if matchesKeys(event, Keys.FirstItem) { + if HitShortcut(event, Keys.FirstItem) { l.transform(TransformFirstItem) - } else if matchesKeys(event, Keys.LastItem) { + } else if HitShortcut(event, Keys.LastItem) { l.transform(TransformLastItem) - } else if matchesKeys(event, Keys.PreviousItem, Keys.PreviousField) { + } else if HitShortcut(event, Keys.PreviousItem, Keys.PreviousField) { l.transform(TransformPreviousItem) - } else if matchesKeys(event, Keys.NextItem, Keys.NextField) { + } else if HitShortcut(event, Keys.NextItem, Keys.NextField) { l.transform(TransformNextItem) - } else if matchesKeys(event, Keys.PreviousPage) { + } else if HitShortcut(event, Keys.PreviousPage) { l.transform(TransformPreviousPage) - } else if matchesKeys(event, Keys.NextPage) { + } else if HitShortcut(event, Keys.NextPage) { l.transform(TransformNextPage) } } diff --git a/treeview.go b/treeview.go index 0328b4c..71807c5 100644 --- a/treeview.go +++ b/treeview.go @@ -889,25 +889,25 @@ func (t *TreeView) InputHandler() func(event *tcell.EventKey, setFocus func(p Pr // Because the tree is flattened into a list only at drawing time, we also // postpone the (selection) movement to drawing time. - if matchesKeys(event, Keys.Cancel, Keys.PreviousField, Keys.NextField) { + if HitShortcut(event, Keys.Cancel, Keys.PreviousField, Keys.NextField) { if t.done != nil { t.Unlock() t.done(event.Key()) t.Lock() } - } else if matchesKeys(event, Keys.FirstItem) { + } else if HitShortcut(event, Keys.FirstItem) { t.movement = treeHome - } else if matchesKeys(event, Keys.LastItem) { + } else if HitShortcut(event, Keys.LastItem) { t.movement = treeEnd - } else if matchesKeys(event, Keys.PreviousItem, Keys.PreviousField) { + } else if HitShortcut(event, Keys.PreviousItem, Keys.PreviousField) { t.movement = treeUp - } else if matchesKeys(event, Keys.NextItem, Keys.NextField) { + } else if HitShortcut(event, Keys.NextItem, Keys.NextField) { t.movement = treeDown - } else if matchesKeys(event, Keys.PreviousPage) { + } else if HitShortcut(event, Keys.PreviousPage) { t.movement = treePageUp - } else if matchesKeys(event, Keys.NextPage) { + } else if HitShortcut(event, Keys.NextPage) { t.movement = treePageDown - } else if matchesKeys(event, Keys.Select) || event.Rune() == ' ' { // TODO space is hardcoded + } else if HitShortcut(event, Keys.Select) || event.Rune() == ' ' { // TODO space is hardcoded t.Unlock() selectNode() t.Lock() diff --git a/util.go b/util.go index cc10270..574cfda 100644 --- a/util.go +++ b/util.go @@ -9,7 +9,6 @@ import ( "github.com/gdamore/tcell" runewidth "github.com/mattn/go-runewidth" "github.com/rivo/uniseg" - "gitlab.com/tslocum/cbind" ) // Text alignment within a box. @@ -695,22 +694,3 @@ func RenderScrollBar(screen tcell.Screen, visibility ScrollBarVisibility, x int, } Print(screen, scrollBar, x, y, 1, AlignLeft, color) } - -// matchesKeys returns whether the EventKey provided is present in one or more -// set of keybindings. -func matchesKeys(event *tcell.EventKey, keybindings ...[]string) bool { - enc, err := cbind.Encode(event.Modifiers(), event.Key(), event.Rune()) - if err != nil { - return false - } - - for _, binds := range keybindings { - for _, key := range binds { - if key == enc { - return true - } - } - } - - return false -}