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

97 lines
2.8 KiB
Go
Raw Normal View History

2018-09-18 13:07:25 +02:00
package gui
import (
"log"
2018-12-08 07:54:54 +02:00
"strings"
2018-09-18 13:07:25 +02:00
2022-02-05 08:04:10 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
"github.com/jesseduffield/lazygit/pkg/gui/style"
2022-01-28 11:44:36 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/types"
2019-11-16 03:41:04 +02:00
"github.com/jesseduffield/lazygit/pkg/utils"
2018-09-18 13:07:25 +02:00
)
2022-02-05 05:42:56 +02:00
func (gui *Gui) getBindings(context types.Context) []*types.Binding {
2022-03-19 00:38:49 +02:00
var bindingsGlobal, bindingsPanel, bindingsNavigation []*types.Binding
2018-09-18 13:07:25 +02:00
2022-02-05 05:42:56 +02:00
bindings, _ := gui.GetInitialKeybindings()
customBindings, err := gui.CustomCommandsClient.GetCustomCommandKeybindings()
if err != nil {
log.Fatal(err)
}
bindings = append(customBindings, bindings...)
2018-09-18 13:07:25 +02:00
for _, binding := range bindings {
if GetKeyDisplay(binding.Key) != "" && binding.Description != "" {
2022-02-27 07:46:27 +02:00
if len(binding.Contexts) == 0 && binding.ViewName == "" {
2018-09-18 13:07:25 +02:00
bindingsGlobal = append(bindingsGlobal, binding)
2022-02-27 07:46:27 +02:00
} else if binding.Tag == "navigation" {
bindingsNavigation = append(bindingsNavigation, binding)
2022-02-05 05:42:56 +02:00
} else if utils.IncludesString(binding.Contexts, string(context.GetKey())) {
bindingsPanel = append(bindingsPanel, binding)
2018-09-18 13:07:25 +02:00
}
}
}
2022-02-27 07:46:27 +02:00
resultBindings := []*types.Binding{}
resultBindings = append(resultBindings, uniqueBindings(bindingsPanel)...)
// adding a separator between the panel-specific bindings and the other bindings
resultBindings = append(resultBindings, &types.Binding{})
resultBindings = append(resultBindings, uniqueBindings(bindingsGlobal)...)
resultBindings = append(resultBindings, uniqueBindings(bindingsNavigation)...)
return resultBindings
}
// We shouldn't really need to do this. We should define alternative keys for the same
// handler in the keybinding struct.
func uniqueBindings(bindings []*types.Binding) []*types.Binding {
keys := make(map[string]bool)
result := make([]*types.Binding, 0)
for _, binding := range bindings {
if _, ok := keys[binding.Description]; !ok {
keys[binding.Description] = true
result = append(result, binding)
}
}
return result
2018-09-18 13:07:25 +02:00
}
2022-01-28 11:44:36 +02:00
func (gui *Gui) displayDescription(binding *types.Binding) string {
if binding.OpensMenu {
2022-02-05 08:04:10 +02:00
return presentation.OpensMenuStyle(binding.Description)
}
return style.FgCyan.Sprint(binding.Description)
}
func (gui *Gui) handleCreateOptionsMenu() error {
2022-02-05 05:42:56 +02:00
context := gui.currentContext()
bindings := gui.getBindings(context)
2018-09-18 13:07:25 +02:00
2022-01-29 10:09:20 +02:00
menuItems := make([]*types.MenuItem, len(bindings))
2020-02-14 13:32:31 +02:00
for i, binding := range bindings {
2020-09-26 07:23:28 +02:00
binding := binding // note to self, never close over loop variables
2022-01-29 10:09:20 +02:00
menuItems[i] = &types.MenuItem{
2022-01-28 11:44:36 +02:00
DisplayStrings: []string{GetKeyDisplay(binding.Key), gui.displayDescription(binding)},
OnPress: func() error {
2020-09-26 07:23:28 +02:00
if binding.Key == nil {
2020-02-14 13:32:31 +02:00
return nil
}
if err := gui.handleMenuClose(); err != nil {
2020-02-14 13:32:31 +02:00
return err
}
return binding.Handler()
2020-02-14 13:32:31 +02:00
},
2018-09-18 13:07:25 +02:00
}
}
2022-01-29 10:09:20 +02:00
return gui.c.Menu(types.CreateMenuOptions{
Title: strings.Title(gui.c.Tr.LcMenu),
2022-01-28 11:44:36 +02:00
Items: menuItems,
HideCancel: true,
})
2018-09-18 13:07:25 +02:00
}