mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-08-06 22:33:07 +02:00
Add support for dynamic binding descriptions
This commit is contained in:
@ -25,7 +25,7 @@ func (self *OptionsMenuAction) Call() error {
|
|||||||
}
|
}
|
||||||
return &types.MenuItem{
|
return &types.MenuItem{
|
||||||
OpensMenu: binding.OpensMenu,
|
OpensMenu: binding.OpensMenu,
|
||||||
Label: binding.Description,
|
Label: binding.GetDescription(),
|
||||||
OnPress: func() error {
|
OnPress: func() error {
|
||||||
if binding.Handler == nil {
|
if binding.Handler == nil {
|
||||||
return nil
|
return nil
|
||||||
@ -60,7 +60,7 @@ func (self *OptionsMenuAction) getBindings(context types.Context) ([]*types.Bind
|
|||||||
bindings, _ := self.c.GetInitialKeybindingsWithCustomCommands()
|
bindings, _ := self.c.GetInitialKeybindingsWithCustomCommands()
|
||||||
|
|
||||||
for _, binding := range bindings {
|
for _, binding := range bindings {
|
||||||
if binding.Description != "" {
|
if binding.GetDescription() != "" {
|
||||||
if binding.ViewName == "" || binding.Tag == "global" {
|
if binding.ViewName == "" || binding.Tag == "global" {
|
||||||
bindingsGlobal = append(bindingsGlobal, binding)
|
bindingsGlobal = append(bindingsGlobal, binding)
|
||||||
} else if binding.ViewName == context.GetViewName() {
|
} else if binding.ViewName == context.GetViewName() {
|
||||||
@ -80,6 +80,6 @@ func (self *OptionsMenuAction) getBindings(context types.Context) ([]*types.Bind
|
|||||||
// handler in the keybinding struct.
|
// handler in the keybinding struct.
|
||||||
func uniqueBindings(bindings []*types.Binding) []*types.Binding {
|
func uniqueBindings(bindings []*types.Binding) []*types.Binding {
|
||||||
return lo.UniqBy(bindings, func(binding *types.Binding) string {
|
return lo.UniqBy(bindings, func(binding *types.Binding) string {
|
||||||
return binding.Description
|
return binding.GetDescription()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -16,12 +16,20 @@ type Binding struct {
|
|||||||
Key Key
|
Key Key
|
||||||
Modifier gocui.Modifier
|
Modifier gocui.Modifier
|
||||||
Description string
|
Description string
|
||||||
|
// DescriptionFunc is used instead of Description if non-nil, and is useful for dynamic
|
||||||
|
// descriptions that change depending on context. Important: this must not be an expensive call.
|
||||||
|
// Note that you should still provide a generic, non-dynamic description in the Description field,
|
||||||
|
// as this is used in the cheatsheet.
|
||||||
|
DescriptionFunc func() string
|
||||||
// If defined, this is used in place of Description when showing the keybinding
|
// If defined, this is used in place of Description when showing the keybinding
|
||||||
// in the options view at the bottom left of the screen.
|
// in the options view at the bottom left of the screen.
|
||||||
ShortDescription string
|
ShortDescription string
|
||||||
Alternative string
|
// ShortDescriptionFunc is used instead of ShortDescription if non-nil, and is useful for dynamic
|
||||||
Tag string // e.g. 'navigation'. Used for grouping things in the cheatsheet
|
// descriptions that change depending on context. Important: this must not be an expensive call.
|
||||||
OpensMenu bool
|
ShortDescriptionFunc func() string
|
||||||
|
Alternative string
|
||||||
|
Tag string // e.g. 'navigation'. Used for grouping things in the cheatsheet
|
||||||
|
OpensMenu bool
|
||||||
|
|
||||||
// If true, the keybinding will appear at the bottom of the screen.
|
// If true, the keybinding will appear at the bottom of the screen.
|
||||||
// Even if set to true, the keybinding will not be displayed if it is currently
|
// Even if set to true, the keybinding will not be displayed if it is currently
|
||||||
@ -47,11 +55,21 @@ func (b *Binding) IsDisabled() bool {
|
|||||||
return b.GetDisabledReason != nil && b.GetDisabledReason() != nil
|
return b.GetDisabledReason != nil && b.GetDisabledReason() != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Binding) GetDescription() string {
|
||||||
|
if b.DescriptionFunc != nil {
|
||||||
|
return b.DescriptionFunc()
|
||||||
|
}
|
||||||
|
return b.Description
|
||||||
|
}
|
||||||
|
|
||||||
func (b *Binding) GetShortDescription() string {
|
func (b *Binding) GetShortDescription() string {
|
||||||
|
if b.ShortDescriptionFunc != nil {
|
||||||
|
return b.ShortDescriptionFunc()
|
||||||
|
}
|
||||||
if b.ShortDescription != "" {
|
if b.ShortDescription != "" {
|
||||||
return b.ShortDescription
|
return b.ShortDescription
|
||||||
}
|
}
|
||||||
return b.Description
|
return b.GetDescription()
|
||||||
}
|
}
|
||||||
|
|
||||||
// A guard is a decorator which checks something before executing a handler
|
// A guard is a decorator which checks something before executing a handler
|
||||||
|
Reference in New Issue
Block a user