1// Package keymap defines key bindings and keyboard shortcuts for the TUI.
2package keymap
3
4import "github.com/charmbracelet/bubbles/v2/key"
5
6// KeyMap is a map of key bindings for the UI.
7type KeyMap struct {
8 Quit key.Binding
9 Up key.Binding
10 Down key.Binding
11 UpDown key.Binding
12 LeftRight key.Binding
13 Arrows key.Binding
14 GotoTop key.Binding
15 GotoBottom key.Binding
16 Select key.Binding
17 Section key.Binding
18 Back key.Binding
19 PrevPage key.Binding
20 NextPage key.Binding
21 Help key.Binding
22
23 SelectItem key.Binding
24 BackItem key.Binding
25
26 Copy key.Binding
27}
28
29// DefaultKeyMap returns the default key map.
30func DefaultKeyMap() *KeyMap {
31 km := new(KeyMap)
32
33 km.Quit = key.NewBinding(
34 key.WithKeys(
35 "q",
36 "ctrl+c",
37 ),
38 key.WithHelp(
39 "q",
40 "quit",
41 ),
42 )
43
44 km.Up = key.NewBinding(
45 key.WithKeys(
46 "up",
47 "k",
48 ),
49 key.WithHelp(
50 "↑",
51 "up",
52 ),
53 )
54
55 km.Down = key.NewBinding(
56 key.WithKeys(
57 "down",
58 "j",
59 ),
60 key.WithHelp(
61 "↓",
62 "down",
63 ),
64 )
65
66 km.UpDown = key.NewBinding(
67 key.WithKeys(
68 "up",
69 "down",
70 "k",
71 "j",
72 ),
73 key.WithHelp(
74 "↑↓",
75 "navigate",
76 ),
77 )
78
79 km.LeftRight = key.NewBinding(
80 key.WithKeys(
81 "left",
82 "h",
83 "right",
84 "l",
85 ),
86 key.WithHelp(
87 "←→",
88 "navigate",
89 ),
90 )
91
92 km.Arrows = key.NewBinding(
93 key.WithKeys(
94 "up",
95 "right",
96 "down",
97 "left",
98 "k",
99 "j",
100 "h",
101 "l",
102 ),
103 key.WithHelp(
104 "↑←↓→",
105 "navigate",
106 ),
107 )
108
109 km.GotoTop = key.NewBinding(
110 key.WithKeys(
111 "home",
112 "g",
113 ),
114 key.WithHelp(
115 "g/home",
116 "goto top",
117 ),
118 )
119
120 km.GotoBottom = key.NewBinding(
121 key.WithKeys(
122 "end",
123 "G",
124 ),
125 key.WithHelp(
126 "G/end",
127 "goto bottom",
128 ),
129 )
130
131 km.Select = key.NewBinding(
132 key.WithKeys(
133 "enter",
134 ),
135 key.WithHelp(
136 "enter",
137 "select",
138 ),
139 )
140
141 km.Section = key.NewBinding(
142 key.WithKeys(
143 "tab",
144 "shift+tab",
145 ),
146 key.WithHelp(
147 "tab",
148 "section",
149 ),
150 )
151
152 km.Back = key.NewBinding(
153 key.WithKeys(
154 "esc",
155 ),
156 key.WithHelp(
157 "esc",
158 "back",
159 ),
160 )
161
162 km.PrevPage = key.NewBinding(
163 key.WithKeys(
164 "pgup",
165 "b",
166 "u",
167 ),
168 key.WithHelp(
169 "pgup",
170 "prev page",
171 ),
172 )
173
174 km.NextPage = key.NewBinding(
175 key.WithKeys(
176 "pgdown",
177 "f",
178 "d",
179 ),
180 key.WithHelp(
181 "pgdn",
182 "next page",
183 ),
184 )
185
186 km.Help = key.NewBinding(
187 key.WithKeys(
188 "?",
189 ),
190 key.WithHelp(
191 "?",
192 "toggle help",
193 ),
194 )
195
196 km.SelectItem = key.NewBinding(
197 key.WithKeys(
198 "l",
199 "right",
200 ),
201 key.WithHelp(
202 "→/l",
203 "select",
204 ),
205 )
206
207 km.BackItem = key.NewBinding(
208 key.WithKeys(
209 "h",
210 "left",
211 "backspace",
212 ),
213 key.WithHelp(
214 "←/h",
215 "back",
216 ),
217 )
218
219 km.Copy = key.NewBinding(
220 key.WithKeys(
221 "c",
222 "ctrl+c",
223 ),
224 key.WithHelp(
225 "c",
226 "copy text",
227 ),
228 )
229
230 return km
231}