Add a helper for recognizing printable keys

Two places test for "a character the user typed" by hand, and a third
one is about to be needed. Give the test a name.

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Stefan Haller 2026-08-25 08:52:05 +02:00
parent c840013ca3
commit b2a684bec1
4 changed files with 24 additions and 2 deletions

View file

@ -78,7 +78,7 @@ func SimpleEditor(v *View, key Key) bool {
v.TextArea.GoToEndOfLine()
case key.Equals(NewKeyStrMod("y", ModCtrl)):
v.TextArea.Yank()
case key.Str() != "" && key.Mod() == 0:
case key.IsPrintable():
v.TextArea.TypeCharacter(key.Str())
default:
return false

View file

@ -2097,7 +2097,7 @@ func (g *Gui) matchView(v *View, kb *keybinding) bool {
if v == nil {
return false
}
if v.Editable && kb.key.Str() != "" && kb.key.Mod() == 0 {
if v.Editable && kb.key.IsPrintable() {
return false
}
if kb.viewName != v.name {

View file

@ -61,6 +61,12 @@ func (k Key) IsSet() bool {
return k.keyName != 0
}
// IsPrintable reports whether the key stands for a character that can be typed
// into a text field.
func (k Key) IsPrintable() bool {
return k.keyName == KeyName(tcell.KeyRune) && k.str != "" && k.mod == ModNone
}
func (k Key) Equals(otherKey Key) bool {
return k.keyName == otherKey.keyName && k.str == otherKey.str && k.mod == otherKey.mod
}

16
pkg/gocui/key_test.go Normal file
View file

@ -0,0 +1,16 @@
package gocui
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestKeyIsPrintable(t *testing.T) {
assert.True(t, NewKeyRune('x').IsPrintable())
assert.True(t, NewKeyRune('界').IsPrintable())
assert.True(t, NewKeyRune(' ').IsPrintable())
assert.False(t, NewKeyStrMod("x", ModCtrl).IsPrintable())
assert.False(t, NewKeyName(KeyEnter).IsPrintable())
assert.False(t, Key{}.IsPrintable())
}