Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(key): add support for Help callback #501

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions help/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (m Model) ShortHelpView(bindings []key.Binding) string {
var separator = m.Styles.ShortSeparator.Inline(true).Render(m.ShortSeparator)

for i, kb := range bindings {
if !kb.Enabled() {
if !kb.Enabled() || !kb.HelpAvailable() {
continue
}

Expand Down Expand Up @@ -188,7 +188,7 @@ func (m Model) FullHelpView(groups [][]key.Binding) string {

// Separate keys and descriptions into different slices
for _, kb := range group {
if !kb.Enabled() {
if !kb.Enabled() || !kb.HelpAvailable() {
continue
}
keys = append(keys, kb.Help().Key)
Expand Down
56 changes: 41 additions & 15 deletions key/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import (
// help text.
type Binding struct {
keys []string
help Help
helpFunc func() Help
disabled bool
}

Expand All @@ -71,7 +71,14 @@ func WithKeys(keys ...string) BindingOpt {
// WithHelp initializes a keybinding with the given help text.
func WithHelp(key, desc string) BindingOpt {
return func(b *Binding) {
b.help = Help{Key: key, Desc: desc}
b.SetHelp(key, desc)
}
}

// WithHelpFunc initializes a keybinding with the given Help callback.
func WithHelpFunc(helpFunc func() Help) BindingOpt {
return func(b *Binding) {
b.SetHelpFunc(helpFunc)
}
}

Expand All @@ -94,32 +101,49 @@ func (b Binding) Keys() []string {

// SetHelp sets the help text for the keybinding.
func (b *Binding) SetHelp(key, desc string) {
b.help = Help{Key: key, Desc: desc}
b.SetHelpFunc(func() Help {
return Help{Key: key, Desc: desc}
})
}

// SetHelpFunc sets the help callback for the keybinding.
func (b *Binding) SetHelpFunc(helpFunc func() Help) {
b.helpFunc = helpFunc
}

// Help returns the Help information for the keybinding.
// A zero-valued Help is returned unless HelpAvailable is true.
func (b Binding) Help() Help {
return b.help
if b.HelpAvailable() {
return b.helpFunc()
}
return Help{}
}

// HelpAvailable returns whether or not the keybinding can provide help.
func (b Binding) HelpAvailable() bool {
return b.helpFunc != nil
}

// Enabled returns whether or not the keybinding is enabled. Disabled
// keybindings won't be activated and won't show up in help. Keybindings are
// enabled by default.
// Enabled returns whether or not the keybinding is enabled.
// Disabled keybindings won't be activated and won't show up in help.
// Keybindings are enabled by default.
func (b Binding) Enabled() bool {
return !b.disabled && b.keys != nil
return !b.disabled
}

// SetEnabled enables or disables the keybinding.
func (b *Binding) SetEnabled(v bool) {
b.disabled = !v
}

// Unbind removes the keys and help from this binding, effectively nullifying
// it. This is a step beyond disabling it, since applications can enable
// Unbind removes the keys from and disables this keybinding.
// This is a step beyond disabling it, since applications can enable
// or disable key bindings based on application state.
func (b *Binding) Unbind() {
b.keys = nil
b.help = Help{}
b.helpFunc = nil
b.disabled = true
}

// Help is help information for a given keybinding.
Expand All @@ -128,13 +152,15 @@ type Help struct {
Desc string
}

// Matches checks if the given KeyMsg matches the given bindings.
// Matches checks if the given KeyMsg matches an enabled keybinding.
func Matches(k tea.KeyMsg, b ...Binding) bool {
keys := k.String()
for _, binding := range b {
for _, v := range binding.keys {
if keys == v && binding.Enabled() {
return true
if binding.Enabled() {
for _, v := range binding.keys {
if keys == v {
return true
}
}
}
}
Expand Down
162 changes: 148 additions & 14 deletions key/key_test.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,160 @@
package key

import (
"math/bits"
"testing"
)

func TestBinding_Enabled(t *testing.T) {
binding := NewBinding(
WithKeys("k", "up"),
WithHelp("↑/k", "move up"),
)
if !binding.Enabled() {
t.Errorf("expected key to be Enabled")
type optConfig uint

const (
optWithKeys = optConfig(1) << iota
optWithHelp
optWithDisabled
)
const optNone = optConfig(0)

type bindingTest struct {
desc string
bin Binding
ok bool
}

func (o *optConfig) next() optConfig {
c := optConfig(1) << bits.TrailingZeros(uint(*o))
*o &^= c // clear optConfig at LSB
return c
}

func makeBinding(o ...optConfig) Binding {
opt := []BindingOpt{}
for _, c := range o {
for c > 0 {
switch c.next() {
case optWithKeys:
opt = append(opt, WithKeys("k", "up"))
case optWithHelp:
opt = append(opt, WithHelp("↑/k", "move up"))
case optWithDisabled:
opt = append(opt, WithDisabled())
}
}
}
return NewBinding(opt...)
}

binding.SetEnabled(false)
if binding.Enabled() {
t.Errorf("expected key not to be Enabled")
func TestBinding_Enabled(t *testing.T) {
for _, tc := range []bindingTest{
{
desc: "WithKeys",
bin: makeBinding(optWithKeys),
ok: true,
},
{
desc: "WithKeys, WithHelp",
bin: makeBinding(optWithKeys | optWithHelp),
ok: true,
},
{
desc: "WithHelp",
bin: makeBinding(optWithHelp),
ok: true,
},
{
desc: "",
bin: makeBinding(),
ok: true,
},
{
desc: "WithDisabled, WithKeys",
bin: makeBinding(optWithDisabled | optWithKeys),
ok: false,
},
{
desc: "WithDisabled, WithKeys, WithHelp",
bin: makeBinding(optWithDisabled | optWithKeys | optWithHelp),
ok: false,
},
{
desc: "WithDisabled, WithHelp",
bin: makeBinding(optWithDisabled | optWithHelp),
ok: false,
},
{
desc: "WithDisabled",
bin: makeBinding(optWithDisabled),
ok: false,
},
} {
if tc.bin.Enabled() != tc.ok {
t.Errorf("bin.Enabled(%s) != %t", tc.desc, tc.ok)
}
tc.bin.SetEnabled(!tc.ok)
if tc.bin.Enabled() == tc.ok {
t.Errorf("bin.Enabled(%s) != %t", tc.desc, !tc.ok)
}
tc.bin.SetEnabled(true)
tc.bin.Unbind()
if tc.bin.Enabled() {
t.Errorf("bin.Enabled(%s) != %t", tc.desc, false)
}
}
}

binding.SetEnabled(true)
binding.Unbind()
if binding.Enabled() {
t.Errorf("expected key not to be Enabled")
func TestBinding_HelpAvailable(t *testing.T) {
for _, tc := range []bindingTest{
{
desc: "WithKeys",
bin: makeBinding(optWithKeys),
ok: false,
},
{
desc: "WithKeys, WithHelp",
bin: makeBinding(optWithKeys | optWithHelp),
ok: true,
},
{
desc: "WithHelp",
bin: makeBinding(optWithHelp),
ok: true,
},
{
desc: "",
bin: makeBinding(),
ok: false,
},
{
desc: "WithDisabled, WithKeys",
bin: makeBinding(optWithDisabled | optWithKeys),
ok: false,
},
{
desc: "WithDisabled, WithKeys, WithHelp",
bin: makeBinding(optWithDisabled | optWithKeys | optWithHelp),
ok: true,
},
{
desc: "WithDisabled, WithHelp",
bin: makeBinding(optWithDisabled | optWithHelp),
ok: true,
},
{
desc: "WithDisabled",
bin: makeBinding(optWithDisabled),
ok: false,
},
} {
if tc.bin.HelpAvailable() != tc.ok {
t.Errorf("bin.HelpAvailable(%s) != %t", tc.desc, tc.ok)
}
tc.bin.SetEnabled(false)
if tc.bin.HelpAvailable() != tc.ok {
t.Errorf("bin.HelpAvailable(%s) != %t", tc.desc, tc.ok)
}
tc.bin.SetEnabled(true)
tc.bin.Unbind()
if tc.bin.HelpAvailable() {
t.Errorf("bin.HelpAvailable(%s) != %t", tc.desc, false)
}
}
}