Skip to content

Commit

Permalink
fix(kitty): only use printables
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Aug 28, 2024
1 parent 5508557 commit 123e465
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
8 changes: 4 additions & 4 deletions kitty.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,11 @@ func parseKittyKeyboard(csi *ansi.CsiSequence) Msg {
}
}

isShifted := key.Mod <= ModShift && key.Code != KeyLeftShift && key.Code != KeyRightShift
if len(key.Text) == 0 && isShifted {
if key.ShiftedCode != 0 {
noneOrShifted := key.Mod <= ModShift && key.Code != KeyLeftShift && key.Code != KeyRightShift
if len(key.Text) == 0 && noneOrShifted {
if key.ShiftedCode != 0 && unicode.IsPrint(key.ShiftedCode) {
key.Text = string(key.ShiftedCode)
} else {
} else if unicode.IsPrint(key.Code) {
key.Text = string(key.Code)
}
}
Expand Down
12 changes: 9 additions & 3 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/base64"
"strings"
"unicode"
"unicode/utf8"

"github.com/charmbracelet/x/ansi"
"github.com/charmbracelet/x/ansi/parser"
Expand Down Expand Up @@ -774,6 +775,7 @@ func parseUtf8(b []byte) (int, Msg) {
code := rune(c)
k := KeyPressMsg{Code: code, Text: string(code)}
if unicode.IsUpper(code) {
// Convert upper case letters to lower case + shift modifier
k.Code = unicode.ToLower(code)
k.ShiftedCode = code
k.Mod |= ModShift
Expand All @@ -782,15 +784,19 @@ func parseUtf8(b []byte) (int, Msg) {
return 1, k
}

code, _ := utf8.DecodeRune(b)
if code == utf8.RuneError {
return 1, UnknownMsg(b[0])
}

cluster, _, _, _ := uniseg.FirstGraphemeCluster(b, -1)
text := string(cluster)
var code rune
for i, r := range text {
for i := range text {
if i > 0 {
// Use [KeyExtended] for multi-rune graphemes
code = KeyExtended
break
}
code = r
}

return len(cluster), KeyPressMsg{Code: code, Text: text}
Expand Down

0 comments on commit 123e465

Please sign in to comment.