Browse Source

Allow multiple sets of keybindings to be provided to matchesKeys

merge-requests/4/head
Trevor Slocum 2 years ago
parent
commit
91afabde29
  1. 2
      button.go
  2. 19
      doc.go
  3. 4
      list.go
  4. 6
      treeview.go
  5. 13
      util.go

2
button.go

@ -155,7 +155,7 @@ func (b *Button) InputHandler() func(event *tcell.EventKey, setFocus func(p Prim
if b.selected != nil {
b.selected()
}
} else if matchesKeys(event, Keys.Cancel) || matchesKeys(event, Keys.PreviousField) || matchesKeys(event, Keys.NextField) {
} else if matchesKeys(event, Keys.Cancel, Keys.PreviousField, Keys.NextField) {
if b.blur != nil {
b.blur(event.Key())
}

19
doc.go

@ -36,9 +36,11 @@ each widget.
Types
This package is a fork of https://github.com/rivo/tview which is based on
https://github.com/gdamore/tcell. It uses types and constants from tcell
(e.g. colors and keyboard values).
This package is built on top of tcell, which provides the types necessary to
create a terminal-based application (e.g. EventKey). For information on
inherited types see the tcell documentation.
tcell: https://github.com/gdamore/tcell
Concurrency
@ -144,14 +146,15 @@ 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 keybindings
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.
cbind is a library which simplifies adding support for custom keybindings to
your application. It allows setting handlers for tcell KeyEvents. It also
parses human-readable strings such as "Alt+Enter" as tcell KeyEvents, or vice
versa. This makes it possible to store keybindings in a configuration file.
cbind is a library which simplifies the process of adding support for custom
keyboard shortcuts to your application. It allows setting handlers for
EventKeys. It also translates between EventKeys and human-readable strings such
as "Alt+Enter". This makes it possible to store keybindings in a configuration
file.
cbind: https://gitlab.com/tslocum/cbind

4
list.go

@ -943,9 +943,9 @@ func (l *List) InputHandler() func(event *tcell.EventKey, setFocus func(p Primit
l.transform(TransformFirstItem)
} else if matchesKeys(event, Keys.LastItem) {
l.transform(TransformLastItem)
} else if matchesKeys(event, Keys.PreviousItem) || matchesKeys(event, Keys.PreviousField) {
} else if matchesKeys(event, Keys.PreviousItem, Keys.PreviousField) {
l.transform(TransformPreviousItem)
} else if matchesKeys(event, Keys.NextItem) || matchesKeys(event, Keys.NextField) {
} else if matchesKeys(event, Keys.NextItem, Keys.NextField) {
l.transform(TransformNextItem)
} else if matchesKeys(event, Keys.PreviousPage) {
l.transform(TransformPreviousPage)

6
treeview.go

@ -889,7 +889,7 @@ 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) || matchesKeys(event, Keys.PreviousField) || matchesKeys(event, Keys.NextField) {
if matchesKeys(event, Keys.Cancel, Keys.PreviousField, Keys.NextField) {
if t.done != nil {
t.Unlock()
t.done(event.Key())
@ -899,9 +899,9 @@ func (t *TreeView) InputHandler() func(event *tcell.EventKey, setFocus func(p Pr
t.movement = treeHome
} else if matchesKeys(event, Keys.LastItem) {
t.movement = treeEnd
} else if matchesKeys(event, Keys.PreviousItem) || matchesKeys(event, Keys.PreviousField) {
} else if matchesKeys(event, Keys.PreviousItem, Keys.PreviousField) {
t.movement = treeUp
} else if matchesKeys(event, Keys.NextItem) || matchesKeys(event, Keys.NextField) {
} else if matchesKeys(event, Keys.NextItem, Keys.NextField) {
t.movement = treeDown
} else if matchesKeys(event, Keys.PreviousPage) {
t.movement = treePageUp

13
util.go

@ -696,16 +696,19 @@ func RenderScrollBar(screen tcell.Screen, visibility ScrollBarVisibility, x int,
Print(screen, scrollBar, x, y, 1, AlignLeft, color)
}
// matchesKeys returns whether the EventKey is present in the list of keybinds.
func matchesKeys(event *tcell.EventKey, keybinds []string) bool {
// 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 _, k := range keybinds {
if k == enc {
return true
for _, binds := range keybindings {
for _, key := range binds {
if key == enc {
return true
}
}
}

Loading…
Cancel
Save