1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-04-25 12:24:47 +02:00

Add Enabled func to Binding

This commit is contained in:
Stefan Haller 2023-09-05 22:27:28 +02:00
parent f2f50ccf75
commit e592d81b60
5 changed files with 36 additions and 5 deletions

View File

@ -25,6 +25,10 @@ func (self *OptionsMenuAction) Call() error {
appendBindings := func(bindings []*types.Binding, section *types.MenuSection) { appendBindings := func(bindings []*types.Binding, section *types.MenuSection) {
menuItems = append(menuItems, menuItems = append(menuItems,
lo.Map(bindings, func(binding *types.Binding, _ int) *types.MenuItem { lo.Map(bindings, func(binding *types.Binding, _ int) *types.MenuItem {
disabledReason := ""
if binding.GetDisabledReason != nil {
disabledReason = binding.GetDisabledReason()
}
return &types.MenuItem{ return &types.MenuItem{
OpensMenu: binding.OpensMenu, OpensMenu: binding.OpensMenu,
Label: binding.Description, Label: binding.Description,
@ -33,10 +37,11 @@ func (self *OptionsMenuAction) Call() error {
return nil return nil
} }
return binding.Handler() return self.c.IGuiCommon.CallKeybindingHandler(binding)
}, },
Key: binding.Key, Key: binding.Key,
Tooltip: binding.Tooltip, Tooltip: binding.Tooltip,
DisabledReason: disabledReason,
Section: section, Section: section,
} }
})...) })...)

View File

@ -171,6 +171,10 @@ func (self *guiCommon) KeybindingsOpts() types.KeybindingsOpts {
return self.gui.keybindingOpts() return self.gui.keybindingOpts()
} }
func (self *guiCommon) CallKeybindingHandler(binding *types.Binding) error {
return self.gui.callKeybindingHandler(binding)
}
func (self *guiCommon) IsAnyModeActive() bool { func (self *guiCommon) IsAnyModeActive() bool {
return self.gui.helpers.Mode.IsAnyModeActive() return self.gui.helpers.Mode.IsAnyModeActive()
} }

View File

@ -375,7 +375,10 @@ func (gui *Gui) wrappedHandler(f func() error) func(g *gocui.Gui, v *gocui.View)
} }
func (gui *Gui) SetKeybinding(binding *types.Binding) error { func (gui *Gui) SetKeybinding(binding *types.Binding) error {
handler := binding.Handler handler := func() error {
return gui.callKeybindingHandler(binding)
}
// TODO: move all mouse-ey stuff into new mouse approach // TODO: move all mouse-ey stuff into new mouse approach
if gocui.IsMouseKey(binding.Key) { if gocui.IsMouseKey(binding.Key) {
handler = func() error { handler = func() error {
@ -406,3 +409,14 @@ func (gui *Gui) SetMouseKeybinding(binding *gocui.ViewMouseBinding) error {
return gui.g.SetViewClickBinding(binding) return gui.g.SetViewClickBinding(binding)
} }
func (gui *Gui) callKeybindingHandler(binding *types.Binding) error {
disabledReason := ""
if binding.GetDisabledReason != nil {
disabledReason = binding.GetDisabledReason()
}
if disabledReason != "" {
return gui.c.ErrorMsg(disabledReason)
}
return binding.Handler()
}

View File

@ -104,6 +104,7 @@ type IGuiCommon interface {
State() IStateAccessor State() IStateAccessor
KeybindingsOpts() KeybindingsOpts KeybindingsOpts() KeybindingsOpts
CallKeybindingHandler(binding *Binding) error
// hopefully we can remove this once we've moved all our keybinding stuff out of the gui god struct. // hopefully we can remove this once we've moved all our keybinding stuff out of the gui god struct.
GetInitialKeybindingsWithCustomCommands() ([]*Binding, []*gocui.ViewMouseBinding) GetInitialKeybindingsWithCustomCommands() ([]*Binding, []*gocui.ViewMouseBinding)

View File

@ -25,6 +25,13 @@ type Binding struct {
// to be displayed if the keybinding is highlighted from within a menu // to be displayed if the keybinding is highlighted from within a menu
Tooltip string Tooltip string
// Function to decide whether the command is enabled, and why. If this
// returns an empty string, it is; if it returns a non-empty string, it is
// disabled and we show the given text in an error message when trying to
// invoke it. When left nil, the command is always enabled. Note that this
// function must not do expensive calls.
GetDisabledReason func() string
} }
// A guard is a decorator which checks something before executing a handler // A guard is a decorator which checks something before executing a handler