Skip to content

Commit

Permalink
feat(key): generate unique IDs for keybindings
Browse files Browse the repository at this point in the history
  • Loading branch information
meowgorithm committed Jan 1, 2025
1 parent 2e3a423 commit 1ba3766
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion key/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,25 @@
// to render help text for keystrokes in your views.
package key

import "fmt"
import (
"fmt"
"sync/atomic"
)

// Internal ID management. Used to uniqely identify keybindings.
var lastID int64

func nextID() int {
return int(atomic.AddInt64(&lastID, 1))
}

// Binding describes a set of keybindings and, optionally, their associated
// help text.
type Binding struct {
keys []string
help Help
disabled bool
id int
}

// BindingOpt is an initialization option for a keybinding. It's used as an
Expand All @@ -56,6 +67,7 @@ func NewBinding(opts ...BindingOpt) Binding {
for _, opt := range opts {
opt(b)
}
b.id = nextID()
return *b
}

Expand All @@ -80,6 +92,35 @@ func WithDisabled() BindingOpt {
}
}

// ID returns the unique ID for the keybinding.
//
// An ID can be used to uniquely identify a keybinding, which becomes
// particularly important when multiple keys are bound to one keybinding.
//
// Consider the following:
//
// b := key.NewBinding(key.WithKeys("k", "up"))
//
// func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// switch msg := msg.(type) {
// case tea.KeyPressMsg:
// switch {
// case key.Matches(msg, b):
// m.keysDown[b.ID()] = struct{}{}
// }
// case tea.KeyReleaseMsg:
// switch {
// case key.Matches(msg, b):
// delete(m.keysDown, b.ID())
// }
// }
//
// // ...
// }
func (b Binding) ID() int {
return b.id
}

// SetKeys sets the keys for the keybinding.
func (b *Binding) SetKeys(keys ...string) {
b.keys = keys
Expand Down

0 comments on commit 1ba3766

Please sign in to comment.