1package commands
  2
  3import (
  4	"github.com/charmbracelet/bubbles/v2/key"
  5)
  6
  7type CommandsDialogKeyMap struct {
  8	Select,
  9	Next,
 10	Previous,
 11	Tab,
 12	Close key.Binding
 13}
 14
 15func DefaultCommandsDialogKeyMap() CommandsDialogKeyMap {
 16	return CommandsDialogKeyMap{
 17		Select: key.NewBinding(
 18			key.WithKeys("enter", "ctrl+y"),
 19			key.WithHelp("enter", "confirm"),
 20		),
 21		Next: key.NewBinding(
 22			key.WithKeys("down", "ctrl+n"),
 23			key.WithHelp("↓", "next item"),
 24		),
 25		Previous: key.NewBinding(
 26			key.WithKeys("up", "ctrl+p"),
 27			key.WithHelp("↑", "previous item"),
 28		),
 29		Tab: key.NewBinding(
 30			key.WithKeys("tab"),
 31			key.WithHelp("tab", "switch selection"),
 32		),
 33		Close: key.NewBinding(
 34			key.WithKeys("esc", "alt+esc"),
 35			key.WithHelp("esc", "cancel"),
 36		),
 37	}
 38}
 39
 40// KeyBindings implements layout.KeyMapProvider
 41func (k CommandsDialogKeyMap) KeyBindings() []key.Binding {
 42	return []key.Binding{
 43		k.Select,
 44		k.Next,
 45		k.Previous,
 46		k.Tab,
 47		k.Close,
 48	}
 49}
 50
 51// FullHelp implements help.KeyMap.
 52func (k CommandsDialogKeyMap) FullHelp() [][]key.Binding {
 53	m := [][]key.Binding{}
 54	slice := k.KeyBindings()
 55	for i := 0; i < len(slice); i += 4 {
 56		end := min(i+4, len(slice))
 57		m = append(m, slice[i:end])
 58	}
 59	return m
 60}
 61
 62// ShortHelp implements help.KeyMap.
 63func (k CommandsDialogKeyMap) ShortHelp() []key.Binding {
 64	return []key.Binding{
 65		k.Tab,
 66		key.NewBinding(
 67			key.WithKeys("down", "up"),
 68			key.WithHelp("↑↓", "choose"),
 69		),
 70		k.Select,
 71		k.Close,
 72	}
 73}
 74
 75type ArgumentsDialogKeyMap struct {
 76	Confirm  key.Binding
 77	Next     key.Binding
 78	Previous key.Binding
 79	Cancel   key.Binding
 80}
 81
 82func DefaultArgumentsDialogKeyMap() ArgumentsDialogKeyMap {
 83	return ArgumentsDialogKeyMap{
 84		Confirm: key.NewBinding(
 85			key.WithKeys("enter"),
 86			key.WithHelp("enter", "confirm"),
 87		),
 88
 89		Next: key.NewBinding(
 90			key.WithKeys("tab", "down"),
 91			key.WithHelp("tab/↓", "next"),
 92		),
 93		Previous: key.NewBinding(
 94			key.WithKeys("shift+tab", "up"),
 95			key.WithHelp("shift+tab/↑", "previous"),
 96		),
 97		Cancel: key.NewBinding(
 98			key.WithKeys("esc"),
 99			key.WithHelp("esc", "cancel"),
100		),
101	}
102}
103
104// KeyBindings implements layout.KeyMapProvider
105func (k ArgumentsDialogKeyMap) KeyBindings() []key.Binding {
106	return []key.Binding{
107		k.Confirm,
108		k.Next,
109		k.Previous,
110	}
111}
112
113// FullHelp implements help.KeyMap.
114func (k ArgumentsDialogKeyMap) FullHelp() [][]key.Binding {
115	m := [][]key.Binding{}
116	slice := k.KeyBindings()
117	for i := 0; i < len(slice); i += 4 {
118		end := min(i+4, len(slice))
119		m = append(m, slice[i:end])
120	}
121	return m
122}
123
124// ShortHelp implements help.KeyMap.
125func (k ArgumentsDialogKeyMap) ShortHelp() []key.Binding {
126	return []key.Binding{
127		k.Confirm,
128		k.Next,
129		k.Previous,
130	}
131}