keymap

package
v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 23, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package keymap provides a configurable key-dispatch system for grut.

The keymap loads keybinding definitions from TOML scheme files (embedded or user-supplied), tracks the current input mode (global/panel/input), and dispatches key events to action strings. It does NOT execute actions — the root model's Update() maps action strings to actual behaviour.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WarnConflicts

func WarnConflicts(bindings []Binding)

WarnConflicts detects conflicts in bindings and logs each one as a warning via slog.

Types

type Binding

type Binding struct {
	Key         string  // Key combination, e.g. "ctrl+c", "j", "ctrl+b z"
	Action      string  // Internal action identifier, e.g. "quit", "cursor_down"
	Context     string  // Panel name for context-specific bindings, or "" for all
	Description string  // Human-readable description
	Mode        KeyMode // When this binding is active
}

Binding describes a single key binding with its mode, context, and action.

func LoadScheme

func LoadScheme(name string) ([]Binding, error)

LoadScheme loads key bindings from a built-in scheme name or a filesystem path. Built-in names: "default", "classic", "vim". Any name containing "/" or "\" is treated as a file path.

type Conflict

type Conflict struct {
	Key     string
	Context string
	Actions []string
	Mode    KeyMode
}

Conflict describes a duplicate key+mode+context combination where multiple actions are bound to the same key.

func DetectConflicts

func DetectConflicts(bindings []Binding) []Conflict

DetectConflicts finds duplicate key+mode+context combinations in bindings. A conflict exists when two or more bindings share the same key, mode, and context but map to different actions.

func (Conflict) String

func (c Conflict) String() string

String returns a human-readable description of the conflict.

type KeyMode

type KeyMode int

KeyMode represents the current input mode for key dispatch.

const (
	// ModeGlobal means only global bindings are active.
	ModeGlobal KeyMode = iota
	// ModePanel means global + panel bindings are active.
	ModePanel
	// ModeInput means global + input bindings are active.
	ModeInput
)

func (KeyMode) String

func (m KeyMode) String() string

String returns the string representation of a KeyMode.

type Keymap

type Keymap struct {
	// contains filtered or unexported fields
}

Keymap holds all key bindings and dispatches key events to action strings.

func NewKeymap

func NewKeymap(scheme string) (*Keymap, error)

NewKeymap creates a Keymap by loading the named scheme. The scheme can be "default", "classic", "vim", or a filesystem path.

func NewKeymapFromBindings

func NewKeymapFromBindings(bindings []Binding) *Keymap

NewKeymapFromBindings creates a Keymap from a pre-loaded set of bindings.

func (*Keymap) Bindings

func (km *Keymap) Bindings() []Binding

Bindings returns all loaded bindings.

func (*Keymap) ClearPending

func (km *Keymap) ClearPending()

ClearPending discards any buffered multi-key prefix.

func (*Keymap) CurrentMode

func (km *Keymap) CurrentMode() KeyMode

CurrentMode returns the active key mode.

func (*Keymap) Dispatch

func (km *Keymap) Dispatch(key, context string) (action string, handled bool)

Dispatch finds the action for a key press in the given context. Returns the action string and whether a binding was found.

Dispatch priority:

  1. Global bindings (always checked)
  2. Context-specific panel/input bindings (matching context)
  3. General panel/input bindings (empty context)

func (*Keymap) HasPending

func (km *Keymap) HasPending() bool

HasPending reports whether a multi-key prefix is buffered.

func (*Keymap) SetMode

func (km *Keymap) SetMode(mode KeyMode)

SetMode switches the current input mode. Any pending multi-key prefix is discarded on mode change.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL