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
Provenance
- Commit
208f421- Tree
c48c0df- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
051a370
4 files changed+105 added−38 removed
modified:examples/fullscreen/main.go+5 added-5 removed
20 unmodified lines21 log.Fatal(err)22 }23 }24 25-func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {25+func update(message tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {26 m, _ := mdl.(model)27 28- switch message := msg.(type) {28+ switch msg := message.(type) {29 30- 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 linesmodified:examples/views/main.go+8 added-6 removed
1 package main2 3+// TODO: This code feels messy. Clean it up.4+5 import (6 "fmt"7 "math"8 "strings"54 unmodified lines63 64 func updateChoices(msg tea.Msg, m Model) (tea.Model, tea.Cmd) {65 switch msg := msg.(type) {66 65- case tea.KeyPressMsg:66- switch msg {67+ case tea.KeyMsg:68+ switch msg.String() {69 case "j":70 fallthrough71 case "down":72 m.Choice += 113 unmodified lines86 case "q":87 fallthrough88 case "esc":89 fallthrough88- case "ctrl+c":90+ case "break":91 return m, tea.Quit92 }93 94 case tickMsg:8 unmodified lines103 104 func updateChosen(msg tea.Msg, m Model) (tea.Model, tea.Cmd) {105 switch msg := msg.(type) {106 105- 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 }116 117 case frameMsg:93 unmodified linesmodified:key.go+91 added-26 removed
5 unmodified lines6 "unicode/utf8"7 )8 9 // KeyPressMsg contains information about a keypress10-type KeyPressMsg string10+type KeyMsg Key11 12-// Control keys12+// String returns a friendly name for a key13+func (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+}21+22+// IsRune returns weather or not the key is a rune23+func (k *KeyMsg) IsRune() bool {24+ if k.Type == KeyRune {25+ return true26+ }27+ return false28+}29+30+type Key struct {31+ Type KeyType32+ Rune rune33+}34+35+// KeyType indicates the key pressed36+type KeyType int37+38+// Possible keys39 const (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 )51 21-var controlNames = map[int]string{22- keyETX: "ctrl+c",23- keyLF: "enter",24- keyCR: "enter",25- keyESC: "esc",26- keyUS: "us",52+// Friendly key names53+var 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 }65 29-var 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 confusion68+const (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+)76+77+// Mapping for control keys to friendly consts78+var controlKeys = map[int]KeyType{79+ keyETX: KeyBreak,80+ keyLF: KeyEnter,81+ keyCR: KeyEnter,82+ keyESC: KeyEscape,83+ keyUS: KeyUnitSeparator,84+ keyDEL: KeyBackspace,85 }86 87+// Mapping for sequences to consts88+var sequences = map[string]KeyType{89+ "\x1b[A": KeyUp,90+ "\x1b[B": KeyDown,91+ "\x1b[C": KeyRight,92+ "\x1b[D": KeyLeft,93+}94+95 // ReadKey reads keypress input from a TTY and returns a string representation96 // of a key38-func ReadKey(r io.Reader) (string, error) {97+func ReadKey(r io.Reader) (Key, error) {98 var buf [256]byte99 100 // Read and block101 n, err := r.Read(buf[:])102 if err != nil {44- return "", err103+ return Key{}, err104 }105 106 // 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 }111 112 // 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+ }118+119 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 }124 125 // 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 }129 130 // 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 lines94 // 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 }()101 102 // Initialize subscriptions125 unmodified lines