Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

key: optimize Matches #261

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
92 changes: 86 additions & 6 deletions key/key.go
Expand Up @@ -37,13 +37,15 @@
package key

import (
"strings"

tea "github.com/charmbracelet/bubbletea"
)

// Binding describes a set of keybindings and, optionally, their associated
// help text.
type Binding struct {
keys []string
keys []tea.Key
help Help
disabled bool
}
Expand All @@ -64,7 +66,7 @@ func NewBinding(opts ...BindingOpt) Binding {
// WithKeys initializes a keybinding with the given keystrokes.
func WithKeys(keys ...string) BindingOpt {
return func(b *Binding) {
b.keys = keys
b.SetKeys(keys...)
}
}

Expand All @@ -84,12 +86,21 @@ func WithDisabled() BindingOpt {

// SetKeys sets the keys for the keybinding.
func (b *Binding) SetKeys(keys ...string) {
b.keys = keys
b.keys = make([]tea.Key, 0, len(keys))
for _, k := range keys {
if tk, ok := MakeKey(k); ok {
b.keys = append(b.keys, tk)
}
}
}

// Keys returns the keys for the keybinding.
func (b Binding) Keys() []string {
return b.keys
kn := make([]string, len(b.keys))
for i, tk := range b.keys {
kn[i] = tk.String()
}
return kn
}

// SetHelp sets the help text for the keybinding.
Expand Down Expand Up @@ -130,13 +141,82 @@ type Help struct {

// Matches checks if the given KeyMsg matches the given bindings.
func Matches(k tea.KeyMsg, b ...Binding) bool {
keys := k.String()
for _, binding := range b {
for _, v := range binding.keys {
if keys == v && binding.Enabled() {
if keyEq(v, tea.Key(k)) && binding.Enabled() {
return true
}
}
}
return false
}

func keyEq(a, b tea.Key) bool {
if a.Type != b.Type {
return false
}
if a.Alt != b.Alt {
return false
}
if len(a.Runes) != len(b.Runes) {
return false
}
for i, ar := range a.Runes {
if b.Runes[i] != ar {
return false
}
}
return true
}

// MakeKey returns a tea.Key for the given keyName.
func MakeKey(keyName string) (tea.Key, bool) {
alt := false
if strings.HasPrefix(keyName, "alt+") {
alt = true
keyName = keyName[4:]
}
// Is this a special key?
k, ok := allKeys[keyName]
if ok {
k.Alt = alt
return k, true
}
// Not a special key: either a simple key "a" or with an alt
// modifier "alt+a".
r := []rune(keyName)
if len(r) != 1 {
// Caller used a key name which we don't understand, bail.
return tea.Key{}, false
}
return tea.Key{
Type: tea.KeyRunes,
Runes: r,
Alt: alt,
}, true
}

// allKeys contains the map of all "special" keys and their
// ctrl/shift/alt combinations.
var allKeys = func() map[string]tea.Key {
result := make(map[string]tea.Key)
for i := 0; ; i++ {
k := tea.Key{Type: tea.KeyType(i)}
keyName := k.String()
// fmt.Println("found key:", keyName)
if keyName == "" {
break
}
result[keyName] = k
}
for i := -2; ; i-- {
k := tea.Key{Type: tea.KeyType(i)}
keyName := k.String()
// fmt.Println("found key:", keyName)
if keyName == "" {
break
}
result[keyName] = k
}
return result
}()
18 changes: 18 additions & 0 deletions key/key_test.go
Expand Up @@ -2,6 +2,8 @@ package key

import (
"testing"

tea "github.com/charmbracelet/bubbletea"
)

func TestBinding_Enabled(t *testing.T) {
Expand All @@ -24,3 +26,19 @@ func TestBinding_Enabled(t *testing.T) {
t.Errorf("expected key not to be Enabled")
}
}

func BenchmarkMatches(b *testing.B) {
msg1 := tea.KeyMsg(tea.Key{Type: tea.KeyRunes, Runes: []rune("c"), Alt: true})
msg2 := tea.KeyMsg(tea.Key{Type: tea.KeyEnter})
kb := NewBinding(WithKeys("alt+c"))
b.Run("success", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Matches(msg1, kb)
}
})
b.Run("fail", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Matches(msg2, kb)
}
})
}