1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
package gui
import (
"strings"
"github.com/fatih/color"
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/utils"
)
func (gui *Gui) getBindings(v *gocui.View) []*Binding {
var (
bindingsGlobal, bindingsPanel []*Binding
)
bindings := append(gui.GetCustomCommandKeybindings(), gui.GetInitialKeybindings()...)
for _, binding := range bindings {
if GetKeyDisplay(binding.Key) != "" && binding.Description != "" {
switch binding.ViewName {
case "":
bindingsGlobal = append(bindingsGlobal, binding)
case v.Name():
if len(binding.Contexts) == 0 || utils.IncludesString(binding.Contexts, v.Context) {
bindingsPanel = append(bindingsPanel, binding)
}
}
}
}
// append dummy element to have a separator between
// panel and global keybindings
bindingsPanel = append(bindingsPanel, &Binding{})
return append(bindingsPanel, bindingsGlobal...)
}
func (gui *Gui) displayDescription(binding *Binding) string {
commandColor := color.New(color.FgCyan)
menuColor := color.New(color.FgMagenta)
if binding.OpensMenu {
return menuColor.Sprintf("%s...", binding.Description)
}
return commandColor.Sprint(binding.Description)
}
func (gui *Gui) handleCreateOptionsMenu() error {
view := gui.g.CurrentView()
if view == nil {
return nil
}
bindings := gui.getBindings(view)
menuItems := make([]*menuItem, len(bindings))
for i, binding := range bindings {
binding := binding // note to self, never close over loop variables
menuItems[i] = &menuItem{
displayStrings: []string{GetKeyDisplay(binding.Key), gui.displayDescription(binding)},
onPress: func() error {
if binding.Key == nil {
return nil
}
if err := gui.handleMenuClose(); err != nil {
return err
}
return binding.Handler()
},
}
}
return gui.createMenu(strings.Title(gui.Tr.LcMenu), menuItems, createMenuOptions{})
}
|