mirror of
https://github.com/jesseduffield/lazygit.git
synced 2024-12-10 11:10:18 +02:00
e33fe37a99
We've been sometimes using lo and sometimes using my slices package, and we need to pick one for consistency. Lo is more extensive and better maintained so we're going with that. My slices package was a superset of go's own slices package so in some places I've just used the official one (the methods were just wrappers anyway). I've also moved the remaining methods into the utils package.
81 lines
2.4 KiB
Go
81 lines
2.4 KiB
Go
package controllers
|
|
|
|
import (
|
|
"github.com/jesseduffield/lazygit/pkg/gui/keybindings"
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
|
"github.com/samber/lo"
|
|
)
|
|
|
|
type OptionsMenuAction struct {
|
|
c *ControllerCommon
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
bindings := self.getBindings(ctx)
|
|
|
|
menuItems := lo.Map(bindings, func(binding *types.Binding, _ int) *types.MenuItem {
|
|
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},
|
|
})
|
|
}
|
|
|
|
func (self *OptionsMenuAction) getBindings(context types.Context) []*types.Binding {
|
|
var bindingsGlobal, bindingsPanel, bindingsNavigation []*types.Binding
|
|
|
|
bindings, _ := self.c.GetInitialKeybindingsWithCustomCommands()
|
|
|
|
for _, binding := range bindings {
|
|
if keybindings.LabelFromKey(binding.Key) != "" && binding.Description != "" {
|
|
if binding.ViewName == "" {
|
|
bindingsGlobal = append(bindingsGlobal, binding)
|
|
} else if binding.Tag == "navigation" {
|
|
bindingsNavigation = append(bindingsNavigation, binding)
|
|
} else if binding.ViewName == context.GetViewName() {
|
|
bindingsPanel = append(bindingsPanel, binding)
|
|
}
|
|
}
|
|
}
|
|
|
|
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 {
|
|
return lo.UniqBy(bindings, func(binding *types.Binding) string {
|
|
return binding.Description
|
|
})
|
|
}
|