1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/gui/options_menu_panel.go

59 lines
1.5 KiB
Go
Raw Normal View History

2018-09-18 13:07:25 +02:00
package gui
import (
2018-12-08 07:54:54 +02:00
"strings"
2018-09-18 13:07:25 +02:00
"github.com/jesseduffield/gocui"
2019-11-16 03:41:04 +02:00
"github.com/jesseduffield/lazygit/pkg/utils"
2018-09-18 13:07:25 +02:00
)
func (gui *Gui) getBindings(v *gocui.View) []*Binding {
var (
bindingsGlobal, bindingsPanel []*Binding
)
2019-11-16 03:41:04 +02:00
bindings := gui.GetInitialKeybindings()
2018-09-18 13:07:25 +02:00
for _, binding := range bindings {
if GetKeyDisplay(binding.Key) != "" && binding.Description != "" {
2018-09-18 13:07:25 +02:00
switch binding.ViewName {
case "":
bindingsGlobal = append(bindingsGlobal, binding)
case v.Name():
2019-11-16 03:41:04 +02:00
if len(binding.Contexts) == 0 || utils.IncludesString(binding.Contexts, v.Context) {
bindingsPanel = append(bindingsPanel, binding)
}
2018-09-18 13:07:25 +02:00
}
}
}
// append dummy element to have a separator between
// panel and global keybindings
bindingsPanel = append(bindingsPanel, &Binding{})
return append(bindingsPanel, bindingsGlobal...)
}
func (gui *Gui) handleCreateOptionsMenu(g *gocui.Gui, v *gocui.View) error {
bindings := gui.getBindings(v)
2020-02-14 13:32:31 +02:00
menuItems := make([]*menuItem, len(bindings))
for i, binding := range bindings {
innerBinding := binding // note to self, never close over loop variables
menuItems[i] = &menuItem{
displayStrings: []string{GetKeyDisplay(innerBinding.Key), innerBinding.Description},
onPress: func() error {
if innerBinding.Key == nil {
return nil
}
if err := gui.handleMenuClose(g, v); err != nil {
return err
}
return innerBinding.Handler(g, v)
},
2018-09-18 13:07:25 +02:00
}
}
2020-02-14 14:39:02 +02:00
return gui.createMenu(strings.Title(gui.Tr.SLocalize("menu")), menuItems, createMenuOptions{})
2018-09-18 13:07:25 +02:00
}