Debian Codegraph @

internal/gui/keybindings.go

raw symbols SCIP
1 package gui
2
3 import (
4 "fmt"
5
6 "github.com/jroimartin/gocui"
7 )
8
9 // KeyBinding struct is helpful for not re-writing the same function over and
10 // over again. it hold useful values to generate a controls view
11 type KeyBinding struct {
12 View string
13 Handler func(*gocui.Gui, *gocui.View) error
14 Key interface{}
15 Modifier gocui.Modifier
16 Display string
17 Description string
18 Vital bool
19 }
20
21 // generate the gui's controls a.k.a. keybindings
22 func (gui *Gui) generateKeybindings() error {
23 // Mainviews common keybindings
24 gui.KeyBindings = make([]*KeyBinding, 0)
25 for _, view := range focusViews {
26 focusKeybindings := []*KeyBinding{
27 {
28 View: view.Name,
29 Key: gocui.KeyTab,
30 Modifier: gocui.ModNone,
31 Handler: gui.focusBackToMain,
32 Display: "tab",
33 Description: "Back to Overview",
34 Vital: true,
35 }, {
36 View: view.Name,
37 Key: gocui.KeyArrowRight,
38 Modifier: gocui.ModNone,
39 Handler: gui.nextFocusView,
40 Display: "→",
41 Description: "Next Panel",
42 Vital: false,
43 }, {
44 View: view.Name,
45 Key: 'l',
46 Modifier: gocui.ModNone,
47 Handler: gui.nextFocusView,
48 Display: "l",
49 Description: "Next Panel",
50 Vital: false,
51 }, {
52 View: view.Name,
53 Key: gocui.KeyArrowLeft,
54 Modifier: gocui.ModNone,
55 Handler: gui.previousFocusView,
56 Display: "←",
57 Description: "Prev Panel",
58 Vital: false,
59 }, {
60 View: view.Name,
61 Key: 'h',
62 Modifier: gocui.ModNone,
63 Handler: gui.previousFocusView,
64 Display: "h",
65 Description: "Prev Panel",
66 Vital: false,
67 },
68 }
69 gui.KeyBindings = append(gui.KeyBindings, focusKeybindings...)
70 }
71 for _, view := range sideViews {
72 sideViewKeybindings := []*KeyBinding{
73 {
74 View: view.Name,
75 Key: gocui.KeyArrowDown,
76 Modifier: gocui.ModNone,
77 Handler: gui.sideCursorDown,
78 Display: "↓",
79 Description: "Cursor Down",
80 Vital: false,
81 }, {
82 View: view.Name,
83 Key: gocui.KeyArrowUp,
84 Modifier: gocui.ModNone,
85 Handler: gui.sideCursorUp,
86 Display: "↑",
87 Description: "Cursor Up",
88 Vital: false,
89 }, {
90 View: view.Name,
91 Key: 'j',
92 Modifier: gocui.ModNone,
93 Handler: gui.sideCursorDown,
94 Display: "j",
95 Description: "Cursor Down",
96 Vital: false,
97 }, {
98 View: view.Name,
99 Key: 'k',
100 Modifier: gocui.ModNone,
101 Handler: gui.sideCursorUp,
102 Display: "k",
103 Description: "Cursor Up",
104 Vital: false,
105 }, {
106 View: view.Name,
107 Key: gocui.KeySpace,
108 Modifier: gocui.ModNone,
109 Handler: gui.selectSideItem,
110 Display: "space",
111 Description: "select",
112 Vital: true,
113 },
114 }
115 gui.KeyBindings = append(gui.KeyBindings, sideViewKeybindings...)
116 }
117 for _, view := range authViews {
118 authKeybindings := []*KeyBinding{
119 {
120 View: view.Name,
121 Key: gocui.KeyEsc,
122 Modifier: gocui.ModNone,
123 Handler: gui.closeAuthenticationView,
124 Display: "esc",
125 Description: "Close/Cancel",
126 Vital: true,
127 }, {
128 View: view.Name,
129 Key: gocui.KeyTab,
130 Modifier: gocui.ModNone,
131 Handler: gui.nextAuthView,
132 Display: "tab",
133 Description: "Next Panel",
134 Vital: true,
135 }, {
136 View: view.Name,
137 Key: gocui.KeyEnter,
138 Modifier: gocui.ModNone,
139 Handler: gui.submitAuthenticationView,
140 Display: "enter",
141 Description: "Submit",
142 Vital: true,
143 },
144 }
145 gui.KeyBindings = append(gui.KeyBindings, authKeybindings...)
146 }
147 for _, view := range commitViews {
148 commitKeybindings := []*KeyBinding{
149 {
150 View: view.Name,
151 Key: gocui.KeyEsc,
152 Modifier: gocui.ModNone,
153 Handler: gui.closeCommitMessageView,
154 Display: "esc",
155 Description: "Close/Cancel",
156 Vital: true,
157 }, {
158 View: view.Name,
159 Key: gocui.KeyTab,
160 Modifier: gocui.ModNone,
161 Handler: gui.nextCommitView,
162 Display: "tab",
163 Description: "Next Panel",
164 Vital: true,
165 }, {
166 View: view.Name,
167 Key: gocui.KeyEnter,
168 Modifier: gocui.ModNone,
169 Handler: gui.submitCommitMessageView,
170 Display: "enter",
171 Description: "Submit",
172 Vital: true,
173 },
174 }
175 gui.KeyBindings = append(gui.KeyBindings, commitKeybindings...)
176 }
177 individualKeybindings := []*KeyBinding{
178 // Main view controls
179 {
180 View: mainViewFeature.Name,
181 Key: 'q',
182 Modifier: gocui.ModNone,
183 Handler: gui.quit,
184 Display: "q",
185 Description: "Quit",
186 Vital: true,
187 }, {
188 View: mainViewFeature.Name,
189 Key: 'f',
190 Modifier: gocui.ModNone,
191 Handler: gui.switchToFetchMode,
192 Display: "f",
193 Description: "Fetch mode",
194 Vital: false,
195 }, {
196 View: mainViewFeature.Name,
197 Key: 'p',
198 Modifier: gocui.ModNone,
199 Handler: gui.switchToPullMode,
200 Display: "p",
201 Description: "Pull mode",
202 Vital: false,
203 }, {
204 View: mainViewFeature.Name,
205 Key: 'm',
206 Modifier: gocui.ModNone,
207 Handler: gui.switchToMergeMode,
208 Display: "m",
209 Description: "Merge mode",
210 Vital: false,
211 }, {
212 View: mainViewFeature.Name,
213 Key: 'c',
214 Modifier: gocui.ModNone,
215 Handler: gui.switchToCheckoutMode,
216 Display: "c",
217 Description: "Checkout mode",
218 Vital: false,
219 }, {
220 View: mainViewFeature.Name,
221 Key: gocui.KeyTab,
222 Modifier: gocui.ModNone,
223 Handler: gui.focusToRepository,
224 Display: "tab",
225 Description: "Focus",
226 Vital: true,
227 }, {
228 View: mainViewFeature.Name,
229 Key: 'u',
230 Modifier: gocui.ModNone,
231 Handler: gui.submitCredentials,
232 Display: "u",
233 Description: "Submit Credentials",
234 Vital: false,
235 }, {
236 View: mainViewFeature.Name,
237 Key: gocui.KeyArrowUp,
238 Modifier: gocui.ModNone,
239 Handler: gui.cursorUp,
240 Display: "↑",
241 Description: "Cursor Up",
242 Vital: false,
243 }, {
244 View: mainViewFeature.Name,
245 Key: gocui.KeyPgup,
246 Modifier: gocui.ModNone,
247 Handler: gui.pageUp,
248 Display: "page up",
249 Description: "Page up",
250 Vital: false,
251 }, {
252 View: mainViewFeature.Name,
253 Key: gocui.KeyHome,
254 Modifier: gocui.ModNone,
255 Handler: gui.cursorTop,
256 Display: "home",
257 Description: "Home",
258 Vital: false,
259 }, {
260 View: mainViewFeature.Name,
261 Key: gocui.KeyPgdn,
262 Modifier: gocui.ModNone,
263 Handler: gui.pageDown,
264 Display: "page down",
265 Description: "Page Down",
266 Vital: false,
267 }, {
268 View: mainViewFeature.Name,
269 Key: gocui.KeyEnd,
270 Modifier: gocui.ModNone,
271 Handler: gui.cursorEnd,
272 Display: "end",
273 Description: "End",
274 Vital: false,
275 }, {
276 View: mainViewFeature.Name,
277 Key: gocui.KeyArrowDown,
278 Modifier: gocui.ModNone,
279 Handler: gui.cursorDown,
280 Display: "↓",
281 Description: "Cursor Down",
282 Vital: false,
283 }, {
284 View: mainViewFeature.Name,
285 Key: 'k',
286 Modifier: gocui.ModNone,
287 Handler: gui.cursorUp,
288 Display: "k",
289 Description: "Cursor Up",
290 Vital: false,
291 }, {
292 View: mainViewFeature.Name,
293 Key: 'j',
294 Modifier: gocui.ModNone,
295 Handler: gui.cursorDown,
296 Display: "j",
297 Description: "Cursor Down",
298 Vital: false,
299 }, {
300 View: mainViewFeature.Name,
301 Key: gocui.KeySpace,
302 Modifier: gocui.ModNone,
303 Handler: gui.markRepository,
304 Display: "space",
305 Description: "Select",
306 Vital: true,
307 }, {
308 View: mainViewFeature.Name,
309 Key: gocui.KeyEnter,
310 Modifier: gocui.ModNone,
311 Handler: gui.startQueue,
312 Display: "enter",
313 Description: "Start",
314 Vital: true,
315 }, {
316 View: mainViewFeature.Name,
317 Key: gocui.KeyCtrlSpace,
318 Modifier: gocui.ModNone,
319 Handler: gui.markAllRepositories,
320 Display: "ctrl + space",
321 Description: "Select All",
322 Vital: false,
323 }, {
324 View: mainViewFeature.Name,
325 Key: gocui.KeyBackspace2,
326 Modifier: gocui.ModNone,
327 Handler: gui.unmarkAllRepositories,
328 Display: "backspace",
329 Description: "Deselect All",
330 Vital: false,
331 }, {
332 View: mainViewFeature.Name,
333 Key: 'h',
334 Modifier: gocui.ModNone,
335 Handler: gui.openCheatSheetView,
336 Display: "h",
337 Description: "Help",
338 Vital: true,
339 }, {
340 View: mainViewFeature.Name,
341 Key: 'b',
342 Modifier: gocui.ModNone,
343 Handler: gui.openBranchesView,
344 Display: "b",
345 Description: "branches",
346 Vital: true,
347 }, {
348 View: mainViewFeature.Name,
349 Key: gocui.KeyCtrlB,
350 Modifier: gocui.ModNone,
351 Handler: gui.openBatchBranchView,
352 Display: "ctrl + b",
353 Description: "Batch branch checkout selection",
354 Vital: false,
355 }, {
356 View: mainViewFeature.Name,
357 Key: 'n',
358 Modifier: gocui.ModNone,
359 Handler: gui.sortByName,
360 Display: "n",
361 Description: "Sort repositories by Name",
362 Vital: false,
363 }, {
364 View: mainViewFeature.Name,
365 Key: 'd',
366 Modifier: gocui.ModNone,
367 Handler: gui.sortByMod,
368 Display: "d",
369 Description: "Sort repositories by Modification date",
370 Vital: false,
371 }, {
372 View: "",
373 Key: gocui.KeyCtrlC,
374 Modifier: gocui.ModNone,
375 Handler: gui.quit,
376 Display: "ctrl + c",
377 Description: "Force application to quit",
378 Vital: false,
379 }, {
380 View: remoteViewFeature.Name,
381 Key: 'b',
382 Modifier: gocui.ModNone,
383 Handler: gui.openRemoteBranchesView,
384 Display: "b",
385 Description: "Remote Branches",
386 Vital: true,
387 }, {
388 View: remoteBranchViewFeature.Name,
389 Key: 's',
390 Modifier: gocui.ModNone,
391 Handler: gui.syncRemoteBranch,
392 Display: "s",
393 Description: "Synch with Remote",
394 Vital: true,
395 }, {
396 View: remoteBranchViewFeature.Name,
397 Key: 'q',
398 Modifier: gocui.ModNone,
399 Handler: gui.closeRemoteBranchesView,
400 Display: "q",
401 Description: "close/cancel",
402 Vital: true,
403 }, {
404 View: branchViewFeature.Name,
405 Key: 'u',
406 Modifier: gocui.ModNone,
407 Handler: gui.setUpstreamToBranch,
408 Display: "u",
409 Description: "Set Upstream",
410 Vital: true,
411 }, {
412 View: branchViewFeature.Name,
413 Key: 'q',
414 Modifier: gocui.ModNone,
415 Handler: gui.closeBranchesView,
416 Display: "q",
417 Description: "close/cancel",
418 Vital: true,
419 }, {
420 View: batchBranchViewFeature.Name,
421 Key: 'q',
422 Modifier: gocui.ModNone,
423 Handler: gui.closeBatchBranchesView,
424 Display: "q",
425 Description: "close/cancel",
426 Vital: true,
427 }, {
428 View: batchBranchViewFeature.Name,
429 Key: 'a',
430 Modifier: gocui.ModNone,
431 Handler: gui.openSuggestBranchView,
432 Display: "a",
433 Description: "add new branch",
434 Vital: true,
435 }, {
436 View: suggestBranchViewFeature.Name,
437 Key: gocui.KeyEsc,
438 Modifier: gocui.ModNone,
439 Handler: gui.closeSuggestBranchesView,
440 Display: "esc",
441 Description: "close/cancel",
442 Vital: true,
443 }, {
444 View: suggestBranchViewFeature.Name,
445 Key: gocui.KeyEnter,
446 Modifier: gocui.ModNone,
447 Handler: gui.closeSuggestBranchesViewWithAdd,
448 Display: "enter",
449 Description: "add",
450 Vital: true,
451 },
452 // CommitView
453 {
454 View: commitViewFeature.Name,
455 Key: gocui.KeyArrowDown,
456 Modifier: gocui.ModNone,
457 Handler: gui.commitCursorDown,
458 Display: "↓",
459 Description: "Cursor Down",
460 Vital: false,
461 }, {
462 View: commitViewFeature.Name,
463 Key: gocui.KeyArrowUp,
464 Modifier: gocui.ModNone,
465 Handler: gui.commitCursorUp,
466 Display: "↑",
467 Description: "Cursor Up",
468 Vital: false,
469 }, {
470 View: commitViewFeature.Name,
471 Key: 'j',
472 Modifier: gocui.ModNone,
473 Handler: gui.commitCursorDown,
474 Display: "j",
475 Description: "Cursor Down",
476 Vital: false,
477 }, {
478 View: commitViewFeature.Name,
479 Key: 'k',
480 Modifier: gocui.ModNone,
481 Handler: gui.commitCursorUp,
482 Display: "k",
483 Description: "Cursor Up",
484 Vital: false,
485 }, {
486 View: commitViewFeature.Name,
487 Key: 'd',
488 Modifier: gocui.ModNone,
489 Handler: gui.commitDiff,
490 Display: "d",
491 Description: "Show commit diff",
492 Vital: true,
493 }, {
494 View: commitViewFeature.Name,
495 Key: 's',
496 Modifier: gocui.ModNone,
497 Handler: gui.commitStat,
498 Display: "s",
499 Description: "Show commit stat",
500 Vital: true,
501 }, {
502 View: commitViewFeature.Name,
503 Key: gocui.KeyPgup,
504 Modifier: gocui.ModNone,
505 Handler: gui.commitPageUp,
506 Display: "pg up",
507 Description: "Page up",
508 Vital: false,
509 }, {
510 View: commitViewFeature.Name,
511 Key: gocui.KeyHome,
512 Modifier: gocui.ModNone,
513 Handler: gui.commitCursorTop,
514 Display: "home",
515 Description: "Home",
516 Vital: false,
517 }, {
518 View: commitViewFeature.Name,
519 Key: gocui.KeyPgdn,
520 Modifier: gocui.ModNone,
521 Handler: gui.commitPageDown,
522 Display: "pg down",
523 Description: "Page Down",
524 Vital: false,
525 },
526 // stashview
527 {
528 View: stashViewFeature.Name,
529 Key: gocui.KeyArrowDown,
530 Modifier: gocui.ModNone,
531 Handler: gui.stashCursorDown,
532 Display: "↓",
533 Description: "Cursor Down",
534 Vital: false,
535 }, {
536 View: stashViewFeature.Name,
537 Key: gocui.KeyArrowUp,
538 Modifier: gocui.ModNone,
539 Handler: gui.stashCursorUp,
540 Display: "↑",
541 Description: "Cursor Up",
542 Vital: false,
543 }, {
544 View: stashViewFeature.Name,
545 Key: 'j',
546 Modifier: gocui.ModNone,
547 Handler: gui.stashCursorDown,
548 Display: "j",
549 Description: "Cursor Down",
550 Vital: false,
551 }, {
552 View: stashViewFeature.Name,
553 Key: 'k',
554 Modifier: gocui.ModNone,
555 Handler: gui.stashCursorUp,
556 Display: "k",
557 Description: "Cursor Up",
558 Vital: false,
559 }, {
560 View: stashViewFeature.Name,
561 Key: 'd',
562 Modifier: gocui.ModNone,
563 Handler: gui.stashDiff,
564 Display: "d",
565 Description: "Show stash diff",
566 Vital: true,
567 }, {
568 View: stashViewFeature.Name,
569 Key: 'o',
570 Modifier: gocui.ModNone,
571 Handler: gui.stashPop,
572 Display: "o",
573 Description: "Pop item",
574 Vital: true,
575 },
576 // upstream confirmation
577 {
578 View: confirmationViewFeature.Name,
579 Key: 'q',
580 Modifier: gocui.ModNone,
581 Handler: gui.closeConfirmationView,
582 Display: "q",
583 Description: "Close/Cancel",
584 Vital: true,
585 }, {
586 View: confirmationViewFeature.Name,
587 Key: gocui.KeyEnter,
588 Modifier: gocui.ModNone,
589 Handler: gui.confirmSetUpstreamToBranch,
590 Display: "enter",
591 Description: "Set Upstream",
592 Vital: true,
593 },
594 // Application Controls
595 {
596 View: cheatSheetViewFeature.Name,
597 Key: 'q',
598 Modifier: gocui.ModNone,
599 Handler: gui.closeCheatSheetView,
600 Display: "q",
601 Description: "Close/Cancel",
602 Vital: true,
603 }, {
604 View: cheatSheetViewFeature.Name,
605 Key: gocui.KeyArrowUp,
606 Modifier: gocui.ModNone,
607 Handler: gui.fastCursorUp,
608 Display: "↑",
609 Description: "Cursor Up",
610 Vital: true,
611 }, {
612 View: cheatSheetViewFeature.Name,
613 Key: gocui.KeyArrowDown,
614 Modifier: gocui.ModNone,
615 Handler: gui.fastCursorDown,
616 Display: "↓",
617 Description: "Cursor Down",
618 Vital: true,
619 }, {
620 View: cheatSheetViewFeature.Name,
621 Key: 'k',
622 Modifier: gocui.ModNone,
623 Handler: gui.fastCursorUp,
624 Display: "k",
625 Description: "Cursor Up",
626 Vital: false,
627 }, {
628 View: cheatSheetViewFeature.Name,
629 Key: 'j',
630 Modifier: gocui.ModNone,
631 Handler: gui.fastCursorDown,
632 Display: "j",
633 Description: "Cursor Down",
634 Vital: false,
635 },
636 // Error View
637 {
638 View: errorViewFeature.Name,
639 Key: 'q',
640 Modifier: gocui.ModNone,
641 Handler: gui.closeErrorView,
642 Display: "q",
643 Description: "Close/Cancel",
644 Vital: true,
645 }, {
646 View: errorViewFeature.Name,
647 Key: gocui.KeyArrowUp,
648 Modifier: gocui.ModNone,
649 Handler: gui.fastCursorUp,
650 Display: "↑",
651 Description: "Cursor Up",
652 Vital: true,
653 }, {
654 View: errorViewFeature.Name,
655 Key: gocui.KeyArrowDown,
656 Modifier: gocui.ModNone,
657 Handler: gui.fastCursorDown,
658 Display: "↓",
659 Description: "Cursor Down",
660 Vital: true,
661 }, {
662 View: errorViewFeature.Name,
663 Key: 'k',
664 Modifier: gocui.ModNone,
665 Handler: gui.fastCursorUp,
666 Display: "k",
667 Description: "Cursor Up",
668 Vital: false,
669 }, {
670 View: errorViewFeature.Name,
671 Key: 'j',
672 Modifier: gocui.ModNone,
673 Handler: gui.fastCursorDown,
674 Display: "j",
675 Description: "Cursor Down",
676 Vital: false,
677 },
678 }
679 gui.KeyBindings = append(gui.KeyBindings, individualKeybindings...)
680 return nil
681 }
682
683 func (gui *Gui) keybindings(g *gocui.Gui) error {
684 for _, k := range gui.KeyBindings {
685 if err := g.SetKeybinding(k.View, k.Key, k.Modifier, k.Handler); err != nil {
686 return err
687 }
688 }
689 return nil
690 }
691
692 // the bottom line of the gui is mode indicator and keybindings view. Only the
693 // important controls (marked as vital) are shown
694 func (gui *Gui) updateKeyBindingsView(g *gocui.Gui, viewName string) error {
695 v, err := g.View(keybindingsViewFeature.Name)
696 if err != nil {
697 return err
698 }
699 v.Clear()
700 v.BgColor = gocui.ColorWhite
701 v.FgColor = gocui.ColorBlack
702 v.Frame = false
703 fmt.Fprint(v, ws)
704 modeLabel := ""
705 switch mode := gui.State.Mode.ModeID; mode {
706 case FetchMode:
707 v.BgColor = gocui.ColorBlue
708 modeLabel = fetchSymbol + ws + "FETCH"
709 case PullMode:
710 v.BgColor = gocui.ColorMagenta
711 modeLabel = pullSymbol + ws + "PULL"
712 case MergeMode:
713 v.BgColor = gocui.ColorCyan
714 modeLabel = mergeSymbol + ws + "MERGE"
715 case CheckoutMode:
716 v.BgColor = gocui.ColorGreen
717 modeLabel = checkoutSymbol + ws + "CHECKOUT"
718 default:
719 modeLabel = "No mode selected"
720 }
721
722 fmt.Fprint(v, ws+modeLabel+ws+modeSeperator)
723
724 for _, k := range gui.KeyBindings {
725 if k.View == viewName && k.Vital {
726 binding := keyBindingSeperator + ws + k.Display + ":" + ws + k.Description + ws
727 fmt.Fprint(v, binding)
728 }
729 }
730 return nil
731 }