Documentation
¶
Overview ¶
Package keybind provides two things for Go TUI applications:
1. A Key type — a parsed, normalized keyboard shortcut. Parse validates a key string (catching typos like "crtl+c" or "delte"), canonicalizes modifier order and special-key aliases ("escape" → "esc", "return" → "enter"), and produces a value that can be compared with == or used in a switch.
2. A generic JSON keybind-config loader (Load, Save, Validate) that any TUI app can use with its own config struct. It reads a JSON file from a config directory, merges with defaults so unknown fields survive upgrades, writes defaults on first run, and validates intra-area key conflicts.
The package is framework-agnostic and has no external dependencies.
Typical usage:
// Define your app's keybind config once.
type MyKeys struct {
Quit string `json:"quit"`
Reload string `json:"reload"`
}
// Load from ~/.config/myapp/keybinds.json, writing defaults if missing.
cfg, err := keybind.Load(cfgDir, "keybinds.json", MyKeys{
Quit: "ctrl+c",
Reload: "r",
})
// Validate for intra-area conflicts.
conflicts := keybind.Validate(map[string]map[string]string{
"global": {"quit": cfg.Quit, "reload": cfg.Reload},
})
// Parse a key string from the config and compare against incoming events.
k, err := keybind.Parse(cfg.Quit)
if err != nil { /* bad value in config */ }
if incomingEvent == k.String() { /* handle quit */ }
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrEmptyKey is returned when the input string is empty or all whitespace. ErrEmptyKey = fmt.Errorf("keybind: empty key string") // ErrUnknownKey is returned when the base key or a modifier is not recognized. ErrUnknownKey = fmt.Errorf("keybind: unknown key") )
Sentinel errors returned by Parse.
Functions ¶
func Load ¶
Load reads a JSON keybind config file from cfgDir/filename into a value of type T, starting from defaults so that any key absent from the file keeps its default value (merge semantics). If the file does not exist it is created with the JSON representation of defaults.
T must be JSON-marshalable. A typical call looks like:
cfg, err := keybind.Load(cfgDir, "keybinds.json", MyKeys{
Quit: "ctrl+c",
Reload: "r",
})
func Save ¶
Save writes cfg as indented JSON to cfgDir/filename, creating cfgDir if needed. It is the write-side counterpart to Load.
func Validate ¶
Validate checks for intra-area conflicts: two different actions within the same area mapped to the same (non-empty) key. Cross-area duplicates are intentional and not reported — "d" for delete in both an inbox view and an email view is normal.
areas maps area name → (action name → key string). The shape mirrors a struct-per-area keybind config, and is easily built from one:
keybind.Validate(map[string]map[string]string{
"global": {"quit": cfg.Global.Quit, "cancel": cfg.Global.Cancel},
"inbox": {"delete": cfg.Inbox.Delete, "archive": cfg.Inbox.Archive},
})
Each returned string is a human-readable conflict description.
Types ¶
type Key ¶
type Key struct {
Ctrl bool
Alt bool
Shift bool
// Base is the normalized base key name. For printable characters it is the
// literal character (e.g. "c", "1", "/"). For special keys it is one of the
// canonical names listed below.
Base string
}
Key is a parsed, normalized keyboard shortcut.
The zero value is not a valid binding; Parse always returns a non-zero Key on success.
Key is comparable — two Keys are equal (==) when they represent the same shortcut, regardless of how the original strings were written ("esc" and "escape" produce the same Key).
func MustParse ¶
MustParse parses a key string and panics on error. Useful for package-level var initialization and test helpers.
func Parse ¶
Parse parses and normalizes a key string such as "ctrl+c", "shift+tab", "ctrl+shift+a", "f1", "esc", or "k".
Modifier names (case-insensitive): ctrl, alt, meta, opt (alt aliases), shift. Base key names: any single printable character, or a special name from the table below. Modifier order in the input does not matter; Key.String always emits ctrl, alt, shift in that order.
Accepted special key names (and their aliases):
enter (return) esc (escape) tab backspace (bs) space up down left right delete (del) insert home end pgup (pageup) pgdown (pagedown, pgdn) f1 – f12
Returns ErrEmptyKey for an empty string, and ErrUnknownKey for an unrecognized base or modifier.