2023-03-23 13:21:42 +02:00
|
|
|
package controllers
|
2018-09-18 13:07:25 +02:00
|
|
|
|
|
|
|
import (
|
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"
|
2023-07-20 13:23:46 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
2022-03-19 03:26:30 +02:00
|
|
|
"github.com/samber/lo"
|
2018-09-18 13:07:25 +02:00
|
|
|
)
|
|
|
|
|
2023-03-23 13:21:42 +02:00
|
|
|
type OptionsMenuAction struct {
|
|
|
|
c *ControllerCommon
|
|
|
|
}
|
2018-09-18 13:07:25 +02:00
|
|
|
|
2023-03-23 13:21:42 +02:00
|
|
|
func (self *OptionsMenuAction) Call() error {
|
|
|
|
ctx := self.c.CurrentContext()
|
|
|
|
// Don't show menu while displaying popup.
|
|
|
|
if ctx.GetKind() == types.PERSISTENT_POPUP || ctx.GetKind() == types.TEMPORARY_POPUP {
|
|
|
|
return nil
|
2022-02-24 04:29:48 +02:00
|
|
|
}
|
2023-03-23 13:21:42 +02:00
|
|
|
|
|
|
|
bindings := self.getBindings(ctx)
|
|
|
|
|
2023-07-24 05:06:42 +02:00
|
|
|
menuItems := lo.Map(bindings, func(binding *types.Binding, _ int) *types.MenuItem {
|
2023-03-23 13:21:42 +02:00
|
|
|
return &types.MenuItem{
|
|
|
|
OpensMenu: binding.OpensMenu,
|
|
|
|
Label: binding.Description,
|
|
|
|
OnPress: func() error {
|
|
|
|
if binding.Handler == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return binding.Handler()
|
|
|
|
},
|
|
|
|
Key: binding.Key,
|
|
|
|
Tooltip: binding.Tooltip,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return self.c.Menu(types.CreateMenuOptions{
|
2023-07-20 13:23:46 +02:00
|
|
|
Title: self.c.Tr.Keybindings,
|
|
|
|
Items: menuItems,
|
|
|
|
HideCancel: true,
|
|
|
|
ColumnAlignment: []utils.Alignment{utils.AlignRight, utils.AlignLeft},
|
2023-03-23 13:21:42 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *OptionsMenuAction) getBindings(context types.Context) []*types.Binding {
|
|
|
|
var bindingsGlobal, bindingsPanel, bindingsNavigation []*types.Binding
|
|
|
|
|
|
|
|
bindings, _ := self.c.GetInitialKeybindingsWithCustomCommands()
|
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
|
|
|
}
|