keys.go

 1package completions
 2
 3import (
 4	"charm.land/bubbles/v2/key"
 5)
 6
 7// KeyMap defines the key bindings for the completions component.
 8type KeyMap struct {
 9	Down,
10	Up,
11	Select,
12	Cancel key.Binding
13	DownInsert,
14	UpInsert key.Binding
15}
16
17// DefaultKeyMap returns the default key bindings for completions.
18func DefaultKeyMap() KeyMap {
19	return KeyMap{
20		Down: key.NewBinding(
21			key.WithKeys("down"),
22			key.WithHelp("down", "move down"),
23		),
24		Up: key.NewBinding(
25			key.WithKeys("up"),
26			key.WithHelp("up", "move up"),
27		),
28		Select: key.NewBinding(
29			key.WithKeys("enter", "tab", "ctrl+y"),
30			key.WithHelp("enter", "select"),
31		),
32		Cancel: key.NewBinding(
33			key.WithKeys("esc", "alt+esc"),
34			key.WithHelp("esc", "cancel"),
35		),
36		DownInsert: key.NewBinding(
37			key.WithKeys("ctrl+n"),
38			key.WithHelp("ctrl+n", "insert next"),
39		),
40		UpInsert: key.NewBinding(
41			key.WithKeys("ctrl+p"),
42			key.WithHelp("ctrl+p", "insert previous"),
43		),
44	}
45}
46
47// KeyBindings returns all key bindings as a slice.
48func (k KeyMap) KeyBindings() []key.Binding {
49	return []key.Binding{
50		k.Down,
51		k.Up,
52		k.Select,
53		k.Cancel,
54	}
55}
56
57// FullHelp returns the full help for the key bindings.
58func (k KeyMap) FullHelp() [][]key.Binding {
59	m := [][]key.Binding{}
60	slice := k.KeyBindings()
61	for i := 0; i < len(slice); i += 4 {
62		end := min(i+4, len(slice))
63		m = append(m, slice[i:end])
64	}
65	return m
66}
67
68// ShortHelp returns the short help for the key bindings.
69func (k KeyMap) ShortHelp() []key.Binding {
70	return []key.Binding{
71		k.Up,
72		k.Down,
73	}
74}