Support multi-character input (i.e. Chinese and Japanese IME)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Christian Rocha · committed by Christian Rocha · parent eae4d66 · Browse files · 1 file·+49 added−27 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+49 added−27 removed
- other:+49 added−27 removed
Provenance
- Commit
fc37167- Tree
034ed35- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <meowgorithm@users.noreply.github.com>- Parent
eae4d66
1 file changed+49 added−27 removed
modified:key.go+49 added-27 removed
33 unmodified lines34 // case "a':35 // fmt.Println("you pressed a!")36 // }37 // }38+//39+// Note that Key.Runes will always contain at least one character, so you can40+// always safely call Key.Runes[0]. In most cases Key.Runes will only contain41+// one character, though certain input method editors (most notably Chinese42+// IMEs) can input multiple runes at once.43 type KeyMsg Key44 45 // String returns a friendly name for a key.46 //3 unmodified lines50 func (k *KeyMsg) String() (str string) {51 if k.Alt {52 str += "alt+"53 }49- if k.Type == KeyRune {50- str += string(k.Rune)54+ if k.Type == KeyRunes {55+ str += string(k.Runes)56 return str57 } else if s, ok := keyNames[int(k.Type)]; ok {58 str += s59 return str2 unmodified lines62 }63 64 // IsRune returns whether or not the key is a rune.65 func (k *KeyMsg) IsRune() bool {61- return k.Type == KeyRune66+ return k.Type == KeyRunes67 }68 69 // Key contains information about a keypress.70 type Key struct {66- Type KeyType67- Rune rune68- Alt bool71+ Type KeyType72+ Runes []rune73+ Alt bool74 }75 76 // KeyType indicates the key pressed, such as KeyEnter or KeyBreak or72-// KeyCtrlC. All other keys will be type KeyRune. To get the rune value, check77+// KeyCtrlC. All other keys will be type KeyRunes. To get the rune value, check78 // the Rune method on a Key struct, or use the Key.String() method:79 //75-// k := Key{Type: KeyRune, Rune: 'a', Alt: true}76-// if k.Type == KeyRune {80+// k := Key{Type: KeyRunes, Runes: []rune{'a'}, Alt: true}81+// if k.Type == KeyRunes {82 //78-// fmt.Println(k.Rune)83+// fmt.Println(k.Runes)84 // // Output: a85 //86 // fmt.Println(k.String())87 // // Output: alt+a90 unmodified lines178 )179 180 // Other keys.181 const (177- KeyRune = -(iota + 1)182+ KeyRunes = -(iota + 1)183 KeyUp184 KeyDown185 KeyRight186 KeyLeft41 unmodified lines228 keyUS: "ctrl+_",229 keySP: "space",230 keyDEL: "backspace",231 227- KeyRune: "rune",232+ KeyRunes: "runes",233 KeyUp: "up",234 KeyDown: "down",235 KeyRight: "right",236 KeyLeft: "left",46 unmodified lines283 }284 285 // readInput reads keypress and mouse input from a TTY and returns a message286 // containing information about the key or mouse event accordingly.282-func readInput(r io.Reader) (Msg, error) {287+func readInput(input io.Reader) (Msg, error) {288 var buf [256]byte289 290 // Read and block286- numBytes, err := r.Read(buf[:])291+ numBytes, err := input.Read(buf[:])292 if err != nil {293 return nil, err294 }295 3 unmodified lines299 if err == nil {300 return MouseMsg(mouseEvent), nil301 }302 298- hex := fmt.Sprintf("%x", buf[:numBytes])303+ // Is it a special sequence, like an arrow key?304+ if k, ok := sequences[string(buf[:numBytes])]; ok {305+ return KeyMsg(Key{Type: k}), nil306+ }307 308 // Some of these need special handling309+ hex := fmt.Sprintf("%x", buf[:numBytes])310 if k, ok := hexes[hex]; ok {311 return KeyMsg(k), nil312 }313 305- // Get unicode value306- char, _ := utf8.DecodeRune(buf[:])307- if char == utf8.RuneError {308- return nil, errors.New("could not decode rune")314+ var runes []rune315+ b := buf[:numBytes]316+317+ // Translate input into runes. In most cases we'll receive exactly one318+ // rune, but there are cases, particularly when an input method editor is319+ // used, where we can receive multiple runes at once.320+ for i, w := 0, 0; i < len(b); i += w {321+ r, width := utf8.DecodeRune(b[i:])322+ if r == utf8.RuneError {323+ return nil, errors.New("could not decode rune")324+ }325+ runes = append(runes, r)326+ w = width327 }328 329+ if len(runes) == 0 {330+ return nil, errors.New("receied 0 runes from input")331+ } else if len(runes) > 1 {332+ // We received multiple runes, so we know this isn't a control333+ // character, sequence, and so on.334+ return KeyMsg(Key{Type: KeyRunes, Runes: runes}), nil335+ }336+337 // Is it a control character?338+ char := runes[0]339 if numBytes == 1 && char <= keyUS || char == keyDEL {340 return KeyMsg(Key{Type: KeyType(char)}), nil341 }342 316- // Is it a special sequence, like an arrow key?317- if k, ok := sequences[string(buf[:numBytes])]; ok {318- return KeyMsg(Key{Type: k}), nil319- }320-343 // Is the alt key pressed? The buffer will be prefixed with an escape322- // sequence if so344+ // sequence if so.345 if numBytes > 1 && buf[0] == 0x1b {346 // Now remove the initial escape sequence and re-process to get the347 // character.348 c, _ := utf8.DecodeRune(buf[1:])349 if c == utf8.RuneError {350 return nil, errors.New("could not decode rune after removing initial escape")351 }330- return KeyMsg(Key{Alt: true, Type: KeyRune, Rune: c}), nil352+ return KeyMsg(Key{Alt: true, Type: KeyRunes, Runes: runes}), nil353 }354 355 // Just a regular, ol' rune334- return KeyMsg(Key{Type: KeyRune, Rune: char}), nil356+ return KeyMsg(Key{Type: KeyRunes, Runes: runes}), nil357 }