keymap

package
v0.0.0-...-64e189b Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2026 License: MIT Imports: 4 Imported by: 0

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

func Matches(msg any, b Binding) bool

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

func NewBinding(desc string, keys ...string) Binding

NewBinding returns a Binding for desc triggered by keys.

func (Binding) Enabled

func (b Binding) Enabled() bool

Enabled reports whether b is enabled.

func (*Binding) SetEnabled

func (b *Binding) SetEnabled(on bool)

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.

func (Conflict) String

func (c Conflict) String() string

String describes the conflict in one line, for logs and error text.

type Hint

type Hint struct {
	Key  string
	Desc string
}

Hint is a binding flattened for display: its keys joined with "/" and its description.

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

func (r *Registry) Add(b Binding) ([]Conflict, error)

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.

func (*Registry) Bindings

func (r *Registry) Bindings(scope string) []Binding

Bindings returns the enabled bindings in scope, in registration order.

func (*Registry) Conflicts

func (r *Registry) Conflicts() []Conflict

Conflicts returns every conflict reported by Add so far.

func (*Registry) Hints

func (r *Registry) Hints(scope string) []Hint

Hints returns the bindings in scope as display hints, in registration order. Bindings with no keys or no description are skipped.

func (*Registry) SetEnabled

func (r *Registry) SetEnabled(scope, key string, on bool) int

SetEnabled enables or disables every registered binding in scope that holds key, and returns how many it changed. Disabled bindings are omitted from Bindings and Hints but still count for conflict detection.

Jump to

Keyboard shortcuts

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