1package quit
 2
 3import (
 4	"github.com/charmbracelet/bubbles/v2/key"
 5)
 6
 7// KeyMap defines the keyboard bindings for the quit dialog.
 8type KeyMap struct {
 9	LeftRight,
10	EnterSpace,
11	Yes,
12	No,
13	Tab,
14	Close key.Binding
15}
16
17func DefaultKeymap() KeyMap {
18	return KeyMap{
19		LeftRight: key.NewBinding(
20			key.WithKeys("left", "right"),
21			key.WithHelp("←/→", "switch options"),
22		),
23		EnterSpace: key.NewBinding(
24			key.WithKeys("enter", " "),
25			key.WithHelp("enter/space", "confirm"),
26		),
27		Yes: key.NewBinding(
28			key.WithKeys("y", "Y", "ctrl+c"),
29			key.WithHelp("y/Y/ctrl+c", "yes"),
30		),
31		No: key.NewBinding(
32			key.WithKeys("n", "N"),
33			key.WithHelp("n/N", "no"),
34		),
35		Tab: key.NewBinding(
36			key.WithKeys("tab"),
37			key.WithHelp("tab", "switch options"),
38		),
39		Close: key.NewBinding(
40			key.WithKeys("esc", "alt+esc"),
41			key.WithHelp("esc", "cancel"),
42		),
43	}
44}
45
46// KeyBindings implements layout.KeyMapProvider
47func (k KeyMap) KeyBindings() []key.Binding {
48	return []key.Binding{
49		k.LeftRight,
50		k.EnterSpace,
51		k.Yes,
52		k.No,
53		k.Tab,
54		k.Close,
55	}
56}
57
58// FullHelp implements help.KeyMap.
59func (k KeyMap) FullHelp() [][]key.Binding {
60	m := [][]key.Binding{}
61	slice := k.KeyBindings()
62	for i := 0; i < len(slice); i += 4 {
63		end := min(i+4, len(slice))
64		m = append(m, slice[i:end])
65	}
66	return m
67}
68
69// ShortHelp implements help.KeyMap.
70func (k KeyMap) ShortHelp() []key.Binding {
71	return []key.Binding{
72		k.LeftRight,
73		k.EnterSpace,
74	}
75}