Skip to content

Commit

Permalink
refactor: rename KeySym to KeyType and make KeyRunes the default
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Aug 15, 2024
1 parent f2bdd36 commit eb2eee4
Show file tree
Hide file tree
Showing 11 changed files with 585 additions and 602 deletions.
57 changes: 32 additions & 25 deletions key.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
package tea

// KeySym is a keyboard symbol.
type KeySym int
// KeyType indicates whether the key is a special key or runes. Special
// keys are things like KeyEnter, KeyBackspace, and so on. Runes keys are just
// regular characters like 'a', '你', 'ض', '🦄', and so on.
//
// k := Key{Type: KeyRunes, Runes: []rune{'A'}, Mod: ModShift}
// if k.Type == KeyRunes {
//
// fmt.Println(k.Runes)
// // Output: A
//
// fmt.Println(k.String())
// // Output: shift+a
//
// }
type KeyType int

// Special key symbols.
const (
// KeyRunes indicates that the key represents rune(s), like 'a', 'b', 'c',
// and so on.
KeyRunes KeySym = iota - 1

// KeyNone indicates that this key is not a special key. Combined with
// `len(k.Runes) > 0`, this indicates that the key represents rune(s) with
// modifiers like [ModCtrl], [ModAlt], [ModShift], and so on.
KeyNone
KeyRunes KeyType = iota

// Special names in C0

Check failure on line 25 in key.go

View workflow job for this annotation

GitHub Actions / lint-soft

Comment should end in a period (godot)

Expand Down Expand Up @@ -218,8 +226,8 @@ const (
// one character, though certain input method editors (most notably Chinese
// IMEs) can input multiple runes at once.
type Key struct {
// Sym is a special key, like enter, tab, backspace, and so on.
Sym KeySym
// Type is a special key, like enter, tab, backspace, and so on.
Type KeyType

// Runes contains the actual characters received. This usually has a length
// of 1. Use [Rune()] to get the first key rune received. If the user
Expand Down Expand Up @@ -315,22 +323,22 @@ func (k Key) Rune() rune {
// "shift+ctrl+alt+a".
func (k Key) String() string {
var s string
if k.Mod.HasCtrl() && k.Sym != KeyLeftCtrl && k.Sym != KeyRightCtrl {
if k.Mod.HasCtrl() && k.Type != KeyLeftCtrl && k.Type != KeyRightCtrl {
s += "ctrl+"

Check failure on line 327 in key.go

View workflow job for this annotation

GitHub Actions / lint-soft

string `ctrl+` has 3 occurrences, make it a constant (goconst)
}
if k.Mod.HasAlt() && k.Sym != KeyLeftAlt && k.Sym != KeyRightAlt {
if k.Mod.HasAlt() && k.Type != KeyLeftAlt && k.Type != KeyRightAlt {
s += "alt+"

Check failure on line 330 in key.go

View workflow job for this annotation

GitHub Actions / lint-soft

string `alt+` has 3 occurrences, make it a constant (goconst)
}
if k.Mod.HasShift() && k.Sym != KeyLeftShift && k.Sym != KeyRightShift {
if k.Mod.HasShift() && k.Type != KeyLeftShift && k.Type != KeyRightShift {
s += "shift+"

Check failure on line 333 in key.go

View workflow job for this annotation

GitHub Actions / lint-soft

string `shift+` has 3 occurrences, make it a constant (goconst)
}
if k.Mod.HasMeta() && k.Sym != KeyLeftMeta && k.Sym != KeyRightMeta {
if k.Mod.HasMeta() && k.Type != KeyLeftMeta && k.Type != KeyRightMeta {
s += "meta+"
}
if k.Mod.HasHyper() && k.Sym != KeyLeftHyper && k.Sym != KeyRightHyper {
if k.Mod.HasHyper() && k.Type != KeyLeftHyper && k.Type != KeyRightHyper {
s += "hyper+"
}
if k.Mod.HasSuper() && k.Sym != KeyLeftSuper && k.Sym != KeyRightSuper {
if k.Mod.HasSuper() && k.Type != KeyLeftSuper && k.Type != KeyRightSuper {
s += "super+"
}

Expand All @@ -356,22 +364,21 @@ func (k Key) String() string {
s += runeStr(k.Rune())
}
} else {
s += k.Sym.String()
s += k.Type.String()
}
return s
}

// String implements fmt.Stringer and prints the string representation of a of
// a Symbol key.
func (k KeySym) String() string {
s, ok := keySymString[k]
if !ok {
return "unknown"
// String returns the string representation of the key type.
func (k KeyType) String() string {
if s, ok := keyTypeString[k]; ok {
return s
}
return s
return ""
}

var keySymString = map[KeySym]string{
var keyTypeString = map[KeyType]string{
KeyRunes: "runes",
KeyEnter: "enter",
KeyTab: "tab",
KeyBackspace: "backspace",
Expand Down
16 changes: 0 additions & 16 deletions key_deprecated.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,6 @@ func (k KeyMsg) String() (str string) {
return ""
}

// KeyType indicates the key pressed, such as KeyEnter or KeyBreak or KeyCtrlC.
// All other keys will be type KeyRunes. To get the rune value, check the Rune
// method on a Key struct, or use the Key.String() method:
//
// k := Key{Type: KeyRunes, Runes: []rune{'a'}, Alt: true}
// if k.Type == KeyRunes {
//
// fmt.Println(k.Runes)
// // Output: a
//
// fmt.Println(k.String())
// // Output: alt+a
//
// }
type KeyType = KeySym

// Control key aliases.
const (
KeyNull KeyType = -iota - 10
Expand Down
Loading

0 comments on commit eb2eee4

Please sign in to comment.