2018-09-18 13:07:25 +02:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
2022-02-24 04:29:48 +02:00
|
|
|
"log"
|
2018-09-18 13:07:25 +02:00
|
|
|
|
2022-03-19 10:12:58 +02:00
|
|
|
"github.com/jesseduffield/generics/slices"
|
2022-03-27 08:15:17 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/keybindings"
|
2022-01-28 11:44:36 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
2022-03-19 03:26:30 +02:00
|
|
|
"github.com/samber/lo"
|
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()
|
2022-02-24 04:29:48 +02:00
|
|
|
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 {
|
2022-08-06 10:50:52 +02:00
|
|
|
if keybindings.LabelFromKey(binding.Key) != "" && binding.Description != "" {
|
2022-06-13 03:01:26 +02:00
|
|
|
if 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-06-13 03:01:26 +02:00
|
|
|
} else if binding.ViewName == context.GetViewName() {
|
2022-02-05 05:42:56 +02:00
|
|
|
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 {
|
2022-03-19 03:26:30 +02:00
|
|
|
return lo.UniqBy(bindings, func(binding *types.Binding) string {
|
|
|
|
return binding.Description
|
|
|
|
})
|
2018-09-18 13:07:25 +02:00
|
|
|
}
|
|
|
|
|
2021-04-02 10:20:40 +02:00
|
|
|
func (gui *Gui) handleCreateOptionsMenu() error {
|
2023-02-05 14:01:15 +02:00
|
|
|
ctx := gui.currentContext()
|
|
|
|
// Don't show menu while displaying popup.
|
|
|
|
if ctx.GetKind() == types.PERSISTENT_POPUP || ctx.GetKind() == types.TEMPORARY_POPUP {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
bindings := gui.getBindings(ctx)
|
2018-09-18 13:07:25 +02:00
|
|
|
|
2022-03-19 10:12:58 +02:00
|
|
|
menuItems := slices.Map(bindings, func(binding *types.Binding) *types.MenuItem {
|
|
|
|
return &types.MenuItem{
|
2022-07-30 12:27:51 +02:00
|
|
|
OpensMenu: binding.OpensMenu,
|
|
|
|
Label: binding.Description,
|
2022-01-28 11:44:36 +02:00
|
|
|
OnPress: func() error {
|
2022-10-15 15:17:00 +02:00
|
|
|
if binding.Handler == nil {
|
2020-02-14 13:32:31 +02:00
|
|
|
return nil
|
|
|
|
}
|
2022-03-26 06:34:15 +02:00
|
|
|
|
2021-04-02 10:20:40 +02:00
|
|
|
return binding.Handler()
|
2020-02-14 13:32:31 +02:00
|
|
|
},
|
2022-05-08 04:46:48 +02:00
|
|
|
Key: binding.Key,
|
|
|
|
Tooltip: binding.Tooltip,
|
2018-09-18 13:07:25 +02:00
|
|
|
}
|
2022-03-19 10:12:58 +02:00
|
|
|
})
|
2018-09-18 13:07:25 +02:00
|
|
|
|
2022-01-29 10:09:20 +02:00
|
|
|
return gui.c.Menu(types.CreateMenuOptions{
|
2022-05-18 13:33:35 +02:00
|
|
|
Title: gui.c.Tr.MenuTitle,
|
2022-01-28 11:44:36 +02:00
|
|
|
Items: menuItems,
|
|
|
|
HideCancel: true,
|
|
|
|
})
|
2018-09-18 13:07:25 +02:00
|
|
|
}
|