From e49b5573bc9983027dd972a38e9fec16ab57642f Mon Sep 17 00:00:00 2001 From: Hironao OTSUBO Date: Mon, 11 Jul 2022 23:45:18 +0900 Subject: [PATCH] fix(key): Unbound keys should be disabled * fix condition for #188 This is a fix of https://github.com/charmbracelet/bubbles/pull/188 Unbound keys has nil keys and they are not Enabled --- key/key.go | 2 +- key/key_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 key/key_test.go diff --git a/key/key.go b/key/key.go index 35fb016..fdc7800 100644 --- a/key/key.go +++ b/key/key.go @@ -106,7 +106,7 @@ func (b Binding) Help() Help { // 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 && b.keys != nil } // SetEnabled enables or disables the keybinding. diff --git a/key/key_test.go b/key/key_test.go new file mode 100644 index 0000000..359a4c2 --- /dev/null +++ b/key/key_test.go @@ -0,0 +1,26 @@ +package key + +import ( + "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") + } + + binding.SetEnabled(false) + if binding.Enabled() { + t.Errorf("expected key not to be Enabled") + } + + binding.SetEnabled(true) + binding.Unbind() + if binding.Enabled() { + t.Errorf("expected key not to be Enabled") + } +}