Documentation
¶
Overview ¶
Package keymap is a registry of key bindings that an app fills once and that help widgets (helpscreen, widgets.KeyHints) read, so the help text cannot drift from a second hand-written list. Keys are plain strings such as "ctrl+c" or "enter", matched against a key event by Matches.
Widget convention ¶
A stateful widget that reacts to keys declares a KeyMap struct with one Binding field per action, a DefaultKeyMap constructor returning today's keys, a KeyMap field on its Model, and a Bindings method returning the bindings it currently honours (so help can list them). The widget's Update tests keys with Matches(msg, m.KeyMap.Down), never with a literal, so a caller rebinding a field changes the behaviour and the help together.
Example ¶
A Registry collects bindings so a help screen can list them and so two actions bound to the same key in one scope are reported.
package main
import (
"fmt"
"github.com/ows4444/tui/keymap"
)
func main() {
var r keymap.Registry
r.Add(keymap.NewBinding("quit", "q"))
conflicts, _ := r.Add(keymap.NewBinding("close", "q"))
fmt.Println(len(conflicts))
for _, h := range r.Hints("") {
fmt.Println(h.Key, h.Desc)
}
}
Output: 1 q quit q close
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Matches ¶
Matches reports whether msg is a key press named by one of b's keys. Key names are those of input.Key.String ("j", "ctrl+c", "enter", "up"). Any other Msg (tui.KeyReleaseMsg included), a Key whose Action is KeyRelease, or a Binding with no keys, reports false. A repeat counts as a press.
Types ¶
type Binding ¶
type Binding struct {
// Keys are the key names that trigger the action, e.g. "q", "ctrl+c".
Keys []string
// Desc is the human description, e.g. "quit".
Desc string
// Scope groups bindings that are active together ("" is the default
// scope). Only bindings in the same scope can conflict.
Scope string
// contains filtered or unexported fields
}
Binding is one action and the keys that trigger it.
func NewBinding ¶
NewBinding returns a Binding for desc triggered by keys.
func (*Binding) SetEnabled ¶
SetEnabled enables or disables b. A disabled binding never matches in Matches and is left out of Registry.Bindings and Hints (so help screens do not list it). The zero Binding is enabled.
type Conflict ¶
type Conflict struct {
Scope string
Key string
// Existing is the binding that already held Key; New is the one added
// after it. New is still registered; the conflict is only reported.
Existing Binding
New Binding
}
Conflict reports a key already bound in a scope.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry holds bindings in registration order. The zero Registry is empty and ready to use; use it through a pointer.
func (*Registry) Add ¶
Add registers b and returns any conflicts it creates with earlier bindings in the same scope, and a non-nil error when there are some. The binding is registered either way; Conflicts lists every conflict seen so far.