Documentation
¶
Overview ¶
Package keybind provides key binding and command routing helpers.
Index ¶
- func CommandShortcuts(keymaps ...*Keymap) map[string][]Key
- func FormatChord(chord string) string
- func FormatKey(key terminal.KeyEvent) string
- func FormatKeyPress(press KeyPress) string
- func FormatKeySequence(key Key) string
- func FormatKeySequences(keys []Key) string
- func ParseKey(desc string) (terminal.KeyEvent, error)
- func RegisterClipboardCommands(registry *CommandRegistry)
- func RegisterScrollCommands(registry *CommandRegistry)
- func RegisterStandardCommands(registry *CommandRegistry)
- type Binding
- type Chord
- type ChordHandler
- type ChordState
- type Command
- type CommandRegistry
- type Condition
- func All(conditions ...Condition) Condition
- func Any(conditions ...Condition) Condition
- func Not(condition Condition) Condition
- func WhenFocused() Condition
- func WhenFocusedAccessible(fn func(accessibility.Accessible) bool) Condition
- func WhenFocusedClipboardTarget() Condition
- func WhenFocusedNotClipboardTarget() Condition
- func WhenFocusedRole(role accessibility.Role) Condition
- func WhenFocusedWidget(fn func(runtime.Widget) bool) Condition
- type Context
- type Handler
- type HandlerBase
- type Key
- type KeyPress
- type KeyRouter
- type Keymap
- type KeymapStack
- type ModeManager
- type RuntimeHandler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CommandShortcuts ¶
CommandShortcuts builds a map of command IDs to key sequences.
func FormatChord ¶
FormatChord formats a chord description string (e.g. "ctrl+k ctrl+c") into a display string (e.g. "Ctrl+K Ctrl+C").
func FormatKeyPress ¶
FormatKeyPress formats a single key press for display.
func FormatKeySequence ¶
FormatKeySequence formats a key sequence for display.
func FormatKeySequences ¶
FormatKeySequences formats multiple key sequences for display.
func ParseKey ¶
ParseKey parses a key description like "ctrl+k", "shift+enter", "alt+a", "F5" into a terminal.KeyEvent.
func RegisterClipboardCommands ¶
func RegisterClipboardCommands(registry *CommandRegistry)
RegisterClipboardCommands registers clipboard commands for focused widgets.
func RegisterScrollCommands ¶
func RegisterScrollCommands(registry *CommandRegistry)
RegisterScrollCommands registers scroll commands for focused widgets.
func RegisterStandardCommands ¶
func RegisterStandardCommands(registry *CommandRegistry)
RegisterStandardCommands registers common runtime commands.
Types ¶
type ChordHandler ¶
type ChordHandler struct {
// contains filtered or unexported fields
}
ChordHandler manages multi-key chord recognition. It maintains a set of chord bindings and tracks pending key presses, applying a configurable timeout between keys.
func NewChordHandler ¶
func NewChordHandler() *ChordHandler
NewChordHandler creates a ChordHandler with default settings.
func (*ChordHandler) Bind ¶
func (h *ChordHandler) Bind(chord string, action func())
Bind registers a chord sequence to an action. The chord format uses space-separated key descriptions, e.g. "ctrl+k ctrl+c".
func (*ChordHandler) HandleKey ¶
func (h *ChordHandler) HandleKey(key terminal.KeyEvent) ChordState
HandleKey processes a key event. It returns the chord state:
- ChordIdle if the key does not match any chord or prefix
- ChordPending if the key extends a partial chord match
- ChordCompleted if the key completed a chord (action was invoked)
func (*ChordHandler) PendingKeys ¶
func (h *ChordHandler) PendingKeys() string
PendingKeys returns the current pending key sequence as a display string, or an empty string if no chord is in progress.
func (*ChordHandler) Reset ¶
func (h *ChordHandler) Reset()
Reset clears any pending chord sequence.
func (*ChordHandler) SetTimeout ¶
func (h *ChordHandler) SetTimeout(d time.Duration)
SetTimeout sets the maximum time between chord key presses.
func (*ChordHandler) Unbind ¶
func (h *ChordHandler) Unbind(chord string)
Unbind removes a chord binding.
type ChordState ¶
type ChordState int
ChordState indicates the current state of chord recognition.
const ( // ChordIdle means no chord is in progress. ChordIdle ChordState = iota // ChordPending means a partial chord prefix has been recognized. ChordPending // ChordCompleted means a chord was fully matched and its action was invoked. ChordCompleted )
type Command ¶
type Command struct {
ID string
Title string
Description string
Category string
Handler func(ctx Context)
Enabled func(ctx Context) bool
}
Command represents a named action.
type CommandRegistry ¶
type CommandRegistry struct {
// contains filtered or unexported fields
}
CommandRegistry stores registered commands.
func (*CommandRegistry) Execute ¶
func (r *CommandRegistry) Execute(id string, ctx Context) bool
Execute runs a command if registered and enabled.
func (*CommandRegistry) Get ¶
func (r *CommandRegistry) Get(id string) (Command, bool)
Get returns a command by ID.
func (*CommandRegistry) List ¶
func (r *CommandRegistry) List() []Command
List returns registered commands.
func (*CommandRegistry) Register ¶
func (r *CommandRegistry) Register(cmd Command)
Register registers or replaces a command.
func (*CommandRegistry) RegisterAll ¶
func (r *CommandRegistry) RegisterAll(cmds ...Command)
RegisterAll registers multiple commands.
type Condition ¶
Condition gates a binding based on runtime context.
func WhenFocusedAccessible ¶
func WhenFocusedAccessible(fn func(accessibility.Accessible) bool) Condition
WhenFocusedAccessible checks a predicate against the focused accessible widget.
func WhenFocusedClipboardTarget ¶
func WhenFocusedClipboardTarget() Condition
WhenFocusedClipboardTarget matches widgets that support clipboard operations.
func WhenFocusedNotClipboardTarget ¶
func WhenFocusedNotClipboardTarget() Condition
WhenFocusedNotClipboardTarget matches widgets that do not support clipboard operations.
func WhenFocusedRole ¶
func WhenFocusedRole(role accessibility.Role) Condition
WhenFocusedRole matches a specific accessibility role.
type Context ¶
type Context struct {
FocusedWidget accessibility.Accessible
Focused runtime.Widget
Keymap *Keymap
App *runtime.App
}
Context provides data for key binding conditions and handlers.
type HandlerBase ¶
type HandlerBase struct {
Map *Keymap
}
HandlerBase provides a simple keymap implementation.
func (*HandlerBase) Keymap ¶
func (h *HandlerBase) Keymap() *Keymap
Keymap returns the bound keymap.
type Key ¶
type Key struct {
Sequence []KeyPress
}
Key describes a key sequence (single key or chord).
func MustParseKeySequence ¶
MustParseKeySequence parses a key sequence and returns an empty Key on error.
func ParseKeySequence ¶
ParseKeySequence parses a key sequence like "ctrl+s" or "g g".
type KeyPress ¶
KeyPress represents a normalized key press.
func KeyPressFromKeyMsg ¶
KeyPressFromKeyMsg normalizes a runtime key message.
func KeyPressFromTerminal ¶
KeyPressFromTerminal normalizes a terminal key event.
type KeyRouter ¶
type KeyRouter struct {
// contains filtered or unexported fields
}
KeyRouter routes key presses to commands.
func NewKeyRouter ¶
func NewKeyRouter(registry *CommandRegistry, modes *ModeManager, keymaps *KeymapStack) *KeyRouter
NewKeyRouter constructs a router.
type KeymapStack ¶
type KeymapStack struct {
// contains filtered or unexported fields
}
KeymapStack manages a stack of keymaps.
func (*KeymapStack) All ¶
func (s *KeymapStack) All() []*Keymap
All returns a copy of the keymap stack.
func (*KeymapStack) Current ¶
func (s *KeymapStack) Current() *Keymap
Current returns the top keymap.
func (*KeymapStack) Match ¶
func (s *KeymapStack) Match(seq []KeyPress, ctx Context) keyMatch
Match searches the stack top-down for a match.
func (*KeymapStack) Push ¶
func (s *KeymapStack) Push(keymap *Keymap)
Push adds a keymap to the stack.
type ModeManager ¶
type ModeManager struct {
// contains filtered or unexported fields
}
ModeManager manages keymap modes.
func NewModeManager ¶
func NewModeManager() *ModeManager
NewModeManager creates an empty mode manager.
func (*ModeManager) Current ¶
func (m *ModeManager) Current() *Keymap
Current returns the active keymap.
func (*ModeManager) CurrentName ¶
func (m *ModeManager) CurrentName() string
CurrentName returns the active mode name.
func (*ModeManager) Push ¶
func (m *ModeManager) Push(mode string)
Push pushes the current mode onto the stack and switches to mode.
func (*ModeManager) Register ¶
func (m *ModeManager) Register(name string, keymap *Keymap)
Register associates a keymap with a mode name.
func (*ModeManager) Set ¶
func (m *ModeManager) Set(mode string)
Set switches to a mode without modifying the stack.