Documentation ¶
Overview ¶
Package key provides some types and functions for generating user-definable keymappings useful in Bubble Tea components. There are a few different ways you can define a keymapping with this package. Here's one example:
type KeyMap struct { Up key.Binding Down key.Binding } var DefaultKeyMap = KeyMap{ Up: key.NewBinding( key.WithKeys("k", "up"), // actual keybindings key.WithHelp("↑/k", "move up"), // corresponding help text ), Down: key.NewBinding( key.WithKeys("j", "down"), key.WithHelp("↓/j", "move down"), ), } func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case tea.KeyMsg: switch { case key.Matches(msg, DefaultKeyMap.Up): // The user pressed up case key.Matches(msg, DefaultKeyMap.Down): // The user pressed down } } // ... }
The help information, which is not used in the example above, can be used to render help text for keystrokes in your views.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Binding ¶
type Binding struct {
// contains filtered or unexported fields
}
Binding describes a set of keybindings and, optionally, their associated help text.
func NewBinding ¶
func NewBinding(opts ...BindingOpt) Binding
NewBinding returns a new keybinding from a set of BindingOpt options.
func (Binding) Enabled ¶
Enabled returns whether or not the keybinding is enabled. Disabled keybindings won't be activated and won't show up in help. Keybindings are enabled by default.
func (*Binding) SetEnabled ¶
SetEnabled enables or disables the keybinding.
type BindingOpt ¶
type BindingOpt func(*Binding)
BindingOpt is an initialization option for a keybinding. It's used as an argument to NewBinding.
func WithHelp ¶
func WithHelp(key, desc string) BindingOpt
WithHelp initializes a keybinding with the given help text.
func WithKeys ¶
func WithKeys(keys ...string) BindingOpt
WithKeys initializes a keybinding with the given keystrokes.