Skip to content

Commit 75afa09

Browse files
committed
Add support for dynamic binding descriptions
1 parent 9eb1e3a commit 75afa09

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

pkg/gui/controllers/options_menu_action.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (self *OptionsMenuAction) Call() error {
2525
}
2626
return &types.MenuItem{
2727
OpensMenu: binding.OpensMenu,
28-
Label: binding.Description,
28+
Label: binding.GetDescription(),
2929
OnPress: func() error {
3030
if binding.Handler == nil {
3131
return nil
@@ -60,7 +60,7 @@ func (self *OptionsMenuAction) getBindings(context types.Context) ([]*types.Bind
6060
bindings, _ := self.c.GetInitialKeybindingsWithCustomCommands()
6161

6262
for _, binding := range bindings {
63-
if binding.Description != "" {
63+
if binding.GetDescription() != "" {
6464
if binding.ViewName == "" || binding.Tag == "global" {
6565
bindingsGlobal = append(bindingsGlobal, binding)
6666
} else if binding.ViewName == context.GetViewName() {
@@ -80,6 +80,6 @@ func (self *OptionsMenuAction) getBindings(context types.Context) ([]*types.Bind
8080
// handler in the keybinding struct.
8181
func uniqueBindings(bindings []*types.Binding) []*types.Binding {
8282
return lo.UniqBy(bindings, func(binding *types.Binding) string {
83-
return binding.Description
83+
return binding.GetDescription()
8484
})
8585
}

pkg/gui/types/keybindings.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,20 @@ type Binding struct {
1616
Key Key
1717
Modifier gocui.Modifier
1818
Description string
19+
// DescriptionFunc is used instead of Description if non-nil, and is useful for dynamic
20+
// descriptions that change depending on context. Important: this must not be an expensive call.
21+
// Note that you should still provide a generic, non-dynamic description in the Description field,
22+
// as this is used in the cheatsheet.
23+
DescriptionFunc func() string
1924
// If defined, this is used in place of Description when showing the keybinding
2025
// in the options view at the bottom left of the screen.
2126
ShortDescription string
22-
Alternative string
23-
Tag string // e.g. 'navigation'. Used for grouping things in the cheatsheet
24-
OpensMenu bool
27+
// ShortDescriptionFunc is used instead of ShortDescription if non-nil, and is useful for dynamic
28+
// descriptions that change depending on context. Important: this must not be an expensive call.
29+
ShortDescriptionFunc func() string
30+
Alternative string
31+
Tag string // e.g. 'navigation'. Used for grouping things in the cheatsheet
32+
OpensMenu bool
2533

2634
// If true, the keybinding will appear at the bottom of the screen.
2735
// Even if set to true, the keybinding will not be displayed if it is currently
@@ -47,11 +55,21 @@ func (b *Binding) IsDisabled() bool {
4755
return b.GetDisabledReason != nil && b.GetDisabledReason() != nil
4856
}
4957

58+
func (b *Binding) GetDescription() string {
59+
if b.DescriptionFunc != nil {
60+
return b.DescriptionFunc()
61+
}
62+
return b.Description
63+
}
64+
5065
func (b *Binding) GetShortDescription() string {
66+
if b.ShortDescriptionFunc != nil {
67+
return b.ShortDescriptionFunc()
68+
}
5169
if b.ShortDescription != "" {
5270
return b.ShortDescription
5371
}
54-
return b.Description
72+
return b.GetDescription()
5573
}
5674

5775
// A guard is a decorator which checks something before executing a handler

0 commit comments

Comments
 (0)