1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-26 05:37:18 +02:00
lazygit/pkg/gui/options_menu_panel.go

87 lines
2.5 KiB
Go
Raw Normal View History

2018-09-18 21:07:25 +10:00
package gui
import (
"log"
2018-12-08 16:54:54 +11:00
"strings"
2018-09-18 21:07:25 +10:00
2022-03-19 19:12:58 +11:00
"github.com/jesseduffield/generics/slices"
"github.com/jesseduffield/lazygit/pkg/gui/keybindings"
2022-02-05 17:04:10 +11:00
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
2022-01-28 20:44:36 +11:00
"github.com/jesseduffield/lazygit/pkg/gui/types"
2022-03-19 12:26:30 +11:00
"github.com/samber/lo"
2018-09-18 21:07:25 +10:00
)
2022-02-05 14:42:56 +11:00
func (gui *Gui) getBindings(context types.Context) []*types.Binding {
2022-03-19 09:38:49 +11:00
var bindingsGlobal, bindingsPanel, bindingsNavigation []*types.Binding
2018-09-18 21:07:25 +10:00
2022-02-05 14:42:56 +11:00
bindings, _ := gui.GetInitialKeybindings()
customBindings, err := gui.CustomCommandsClient.GetCustomCommandKeybindings()
if err != nil {
log.Fatal(err)
}
bindings = append(customBindings, bindings...)
2018-09-18 21:07:25 +10:00
for _, binding := range bindings {
if keybindings.GetKeyDisplay(binding.Key) != "" && binding.Description != "" {
2022-02-27 16:46:27 +11:00
if len(binding.Contexts) == 0 && binding.ViewName == "" {
2018-09-18 21:07:25 +10:00
bindingsGlobal = append(bindingsGlobal, binding)
2022-02-27 16:46:27 +11:00
} else if binding.Tag == "navigation" {
bindingsNavigation = append(bindingsNavigation, binding)
2022-03-19 12:26:30 +11:00
} else if lo.Contains(binding.Contexts, string(context.GetKey())) {
2022-02-05 14:42:56 +11:00
bindingsPanel = append(bindingsPanel, binding)
2018-09-18 21:07:25 +10:00
}
}
}
2022-02-27 16:46:27 +11: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 {
2022-03-19 12:26:30 +11:00
return lo.UniqBy(bindings, func(binding *types.Binding) string {
return binding.Description
})
2018-09-18 21:07:25 +10:00
}
2022-01-28 20:44:36 +11:00
func (gui *Gui) displayDescription(binding *types.Binding) string {
if binding.OpensMenu {
2022-02-05 17:04:10 +11:00
return presentation.OpensMenuStyle(binding.Description)
}
2022-03-27 17:58:30 +11:00
return binding.Description
}
func (gui *Gui) handleCreateOptionsMenu() error {
2022-02-05 14:42:56 +11:00
context := gui.currentContext()
bindings := gui.getBindings(context)
2018-09-18 21:07:25 +10:00
2022-03-19 19:12:58 +11:00
menuItems := slices.Map(bindings, func(binding *types.Binding) *types.MenuItem {
return &types.MenuItem{
Label: gui.displayDescription(binding),
2022-01-28 20:44:36 +11:00
OnPress: func() error {
2020-09-26 15:23:28 +10:00
if binding.Key == nil {
2020-02-14 22:32:31 +11:00
return nil
}
2022-03-26 15:34:15 +11:00
return binding.Handler()
2020-02-14 22:32:31 +11:00
},
Key: binding.Key,
Tooltip: binding.Tooltip,
2018-09-18 21:07:25 +10:00
}
2022-03-19 19:12:58 +11:00
})
2018-09-18 21:07:25 +10:00
2022-01-29 19:09:20 +11:00
return gui.c.Menu(types.CreateMenuOptions{
Title: strings.Title(gui.c.Tr.LcMenu),
2022-01-28 20:44:36 +11:00
Items: menuItems,
HideCancel: true,
})
2018-09-18 21:07:25 +10:00
}