Skip to content
Myr

Refactor the keybinding stuff + add backspace

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 051a370 · Browse files · 4 files+105 added−38 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+105 added−38 removed

  • other:+105 added−38 removed

4 files+105 added−38 removed

modified:examples/fullscreen/main.go+5 added-5 removed
20 unmodified lines
21 log.Fatal(err)22 }23}2425func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {25func update(message tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {26 m, _ := mdl.(model)2728 switch message := msg.(type) {28 switch msg := message.(type) {2930 case tea.KeyPressMsg:31 switch message {32 case "ctrl+c":30 case tea.KeyMsg:31 switch msg.String() {32 case "break":33 fallthrough34 case "esc":35 fallthrough36 case "q":
23 unmodified lines
modified:examples/views/main.go+8 added-6 removed
1package main23// TODO: This code feels messy. Clean it up.45import (6 "fmt"7 "math"8 "strings"
54 unmodified lines
6364func updateChoices(msg tea.Msg, m Model) (tea.Model, tea.Cmd) {65 switch msg := msg.(type) {6665 case tea.KeyPressMsg:66 switch msg {67 case tea.KeyMsg:68 switch msg.String() {69 case "j":70 fallthrough71 case "down":72 m.Choice += 1
13 unmodified lines
86 case "q":87 fallthrough88 case "esc":89 fallthrough88 case "ctrl+c":90 case "break":91 return m, tea.Quit92 }9394 case tickMsg:
8 unmodified lines
103104func updateChosen(msg tea.Msg, m Model) (tea.Model, tea.Cmd) {105 switch msg := msg.(type) {106105 case tea.KeyPressMsg:106 switch msg {107 case tea.KeyMsg:108 switch msg.String() {109 case "q":110 fallthrough111 case "esc":112 fallthrough111 case "ctrl+c":113 case "break":114 return m, tea.Quit115 }116117 case frameMsg:
93 unmodified lines
modified:key.go+91 added-26 removed
5 unmodified lines
6 "unicode/utf8"7)89// KeyPressMsg contains information about a keypress10type KeyPressMsg string10type KeyMsg Key1112// Control keys12// String returns a friendly name for a key13func (k *KeyMsg) String() string {14 if k.Type == KeyRune {15 return string(k.Rune)16 } else if s, ok := keyNames[k.Type]; ok {17 return s18 }19 return ""20}2122// IsRune returns weather or not the key is a rune23func (k *KeyMsg) IsRune() bool {24 if k.Type == KeyRune {25 return true26 }27 return false28}2930type Key struct {31 Type KeyType32 Rune rune33}3435// KeyType indicates the key pressed36type KeyType int3738// Possible keys39const (14 keyETX = 3 // break, ctrl+c15 keyLF = 9 // line-feed, \n16 keyCR = 13 // carriage return, \r17 keyESC = 2718 keyUS = 31 // unit separator40 KeyBreak KeyType = iota41 KeyEnter42 KeyEscape43 KeyUp44 KeyDown45 KeyRight46 KeyLeft47 KeyUnitSeparator48 KeyBackspace49 KeyRune50)5121var controlNames = map[int]string{22 keyETX: "ctrl+c",23 keyLF: "enter",24 keyCR: "enter",25 keyESC: "esc",26 keyUS: "us",52// Friendly key names53var keyNames = map[KeyType]string{54 KeyBreak: "break",55 KeyEnter: "enter",56 KeyEscape: "esc",57 KeyUp: "up",58 KeyDown: "down",59 KeyRight: "right",60 KeyLeft: "left",61 KeyUnitSeparator: "us",62 KeyBackspace: "backspace",63 KeyRune: "rune",64}6529var sequenceNames = map[string]string{30 "\x1b[A": "up",31 "\x1b[B": "down",32 "\x1b[C": "right",33 "\x1b[D": "left",66// Control keys. I know we could do this with an iota, but the values are very67// specific, so we set the values explicitly to avoid any confusion68const (69 keyETX = 3 // break, ctrl+c70 keyLF = 9 // line-feed, \n71 keyCR = 13 // carriage return, \r72 keyESC = 27 // escape73 keyUS = 31 // unit separator74 keyDEL = 127 // delete. on most systems this is mapped to backspace, I hear75)7677// Mapping for control keys to friendly consts78var controlKeys = map[int]KeyType{79 keyETX: KeyBreak,80 keyLF: KeyEnter,81 keyCR: KeyEnter,82 keyESC: KeyEscape,83 keyUS: KeyUnitSeparator,84 keyDEL: KeyBackspace,85}8687// Mapping for sequences to consts88var sequences = map[string]KeyType{89 "\x1b[A": KeyUp,90 "\x1b[B": KeyDown,91 "\x1b[C": KeyRight,92 "\x1b[D": KeyLeft,93}9495// ReadKey reads keypress input from a TTY and returns a string representation96// of a key38func ReadKey(r io.Reader) (string, error) {97func ReadKey(r io.Reader) (Key, error) {98 var buf [256]byte99100 // Read and block101 n, err := r.Read(buf[:])102 if err != nil {44 return "", err103 return Key{}, err104 }105106 // Get rune107 c, _ := utf8.DecodeRune(buf[:])108 if c == utf8.RuneError {50 return "", errors.New("no such rune")109 return Key{}, errors.New("no such rune")110 }111112 // Is it a control character?113 if n == 1 && c <= keyUS || c == keyDEL {114 if k, ok := controlKeys[n]; ok {115 return Key{Type: k}, nil116 }117 }118119 if n == 1 && c <= keyUS {55 if s, ok := controlNames[int(c)]; ok {56 return s, nil120 if k, ok := controlKeys[int(c)]; ok {121 return Key{Type: k}, nil122 }123 }124125 // Is it a special sequence, like an arrow key?61 if s, ok := sequenceNames[string(buf[:n])]; ok {62 return s, nil126 if k, ok := sequences[string(buf[:n])]; ok {127 return Key{Type: k}, nil128 }129130 // Nope, just a regular, ol' rune66 return string(c), nil131 return Key{Type: KeyRune, Rune: c}, nil132}
modified:tea.go+1 added-1 removed
93 unmodified lines
94 // need that we're enabling it by default.95 go func() {96 for {97 msg, _ := ReadKey(p.rw)98 msgs <- KeyPressMsg(msg)98 msgs <- KeyMsg(msg)99 }100 }()101102 // Initialize subscriptions
125 unmodified lines