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

54 lines
1.2 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/go-errors/errors"
2018-09-18 13:07:25 +02:00
"github.com/jesseduffield/gocui"
)
func (gui *Gui) getBindings(v *gocui.View) []*Binding {
var (
bindingsGlobal, bindingsPanel []*Binding
)
bindings := gui.GetCurrentKeybindings()
2018-09-18 13:07:25 +02:00
for _, binding := range bindings {
if binding.GetKey() != "" && binding.Description != "" {
switch binding.ViewName {
case "":
bindingsGlobal = append(bindingsGlobal, binding)
case v.Name():
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) handleCreateOptionsMenu(g *gocui.Gui, v *gocui.View) error {
bindings := gui.getBindings(v)
2018-09-19 11:15:29 +02:00
handleMenuPress := func(index int) error {
2018-09-18 13:07:25 +02:00
if bindings[index].Key == nil {
return nil
}
2018-09-19 11:15:29 +02:00
if index >= len(bindings) {
2018-09-18 13:07:25 +02:00
return errors.New("Index is greater than size of bindings")
}
err := gui.handleMenuClose(g, v)
if err != nil {
return err
}
return bindings[index].Handler(g, v)
}
2018-12-08 07:54:54 +02:00
return gui.createMenu(strings.Title(gui.Tr.SLocalize("menu")), bindings, handleMenuPress)
2018-09-18 13:07:25 +02:00
}