1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-10 04:07:18 +02:00
lazygit/pkg/gui/controllers/options_menu_action.go

90 lines
2.9 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
local, global, navigation := self.getBindings(ctx)
2023-03-23 13:21:42 +02:00
menuItems := []*types.MenuItem{}
appendBindings := func(bindings []*types.Binding, section *types.MenuSection) {
menuItems = append(menuItems,
lo.Map(bindings, func(binding *types.Binding, _ int) *types.MenuItem {
2023-09-05 22:27:28 +02:00
disabledReason := ""
if binding.GetDisabledReason != nil {
disabledReason = binding.GetDisabledReason()
}
return &types.MenuItem{
OpensMenu: binding.OpensMenu,
Label: binding.Description,
OnPress: func() error {
if binding.Handler == nil {
return nil
}
2023-09-05 22:27:28 +02:00
return self.c.IGuiCommon.CallKeybindingHandler(binding)
},
2023-09-05 22:27:28 +02:00
Key: binding.Key,
Tooltip: binding.Tooltip,
DisabledReason: disabledReason,
Section: section,
2023-03-23 13:21:42 +02:00
}
})...)
}
2023-03-23 13:21:42 +02:00
appendBindings(local, &types.MenuSection{Title: self.c.Tr.KeybindingsMenuSectionLocal, Column: 1})
appendBindings(global, &types.MenuSection{Title: self.c.Tr.KeybindingsMenuSectionGlobal, Column: 1})
appendBindings(navigation, &types.MenuSection{Title: self.c.Tr.KeybindingsMenuSectionNavigation, Column: 1})
2023-03-23 13:21:42 +02:00
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
})
}
// Returns three slices of bindings: local, global, and navigation
func (self *OptionsMenuAction) getBindings(context types.Context) ([]*types.Binding, []*types.Binding, []*types.Binding) {
2023-03-23 13:21:42 +02:00
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
}
}
}
return uniqueBindings(bindingsPanel), uniqueBindings(bindingsGlobal), uniqueBindings(bindingsNavigation)
2022-02-27 07:46:27 +02:00
}
// 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
}