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

81 lines
2.4 KiB
Go
Raw Normal View History

2023-03-23 13:21:42 +02:00
package controllers
2018-09-18 13:07:25 +02:00
import (
"github.com/jesseduffield/lazygit/pkg/gui/keybindings"
2022-01-28 11:44:36 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/types"
"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
}
2023-03-23 13:21:42 +02:00
bindings := self.getBindings(ctx)
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{
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 {
if keybindings.LabelFromKey(binding.Key) != "" && binding.Description != "" {
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)
} 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
}