Skip to content
Myr

Also use escape to quit

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 c7d2835 · Browse files · 2 files+28 added−7 removed
This commit

State

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

Diff stats

2 files+28 added−7 removed

  • other:+28 added−7 removed

2 files+28 added−7 removed

modified:example/main.go+3 added-1 removed
34 unmodified lines
35 m = 036 }3738 case "q":39 fallthrough40 case "esc":41 return m, tea.Quit4243 }44 }
12 unmodified lines
57 checkbox("See friends", m == 3),58 )5960 return fmt.Sprintf(59 "What to do today?\n\n%s\n\n(press j/k or up/down to select, q to quit)",61 "What to do today?\n\n%s\n\n(press j/k or up/down to select, q or esc to quit)",62 choices,63 )64}65
7 unmodified lines
modified:key.go+25 added-6 removed
7 unmodified lines
89// KeyPressMsg contains information about a keypress10type KeyPressMsg string1112// Control keys13const (14 keyESC = 2715 keyUS = 3116)1718var controlKeyNames = map[int]string{19 keyESC: "esc",20 keyUS: "us",21}2223var keyNames = map[string]string{24 "\x1b[A": "up",25 "\x1b[B": "down",26 "\x1b[C": "right",
10 unmodified lines
37 if err != nil {38 return "", err39 }4030 // Was it a special key, like an arrow key?31 if s, ok := keyNames[string(buf[:n])]; ok {32 return s, nil33 }3435 // Nope, just a regular key41 // Get rune42 c, _ := utf8.DecodeRune(buf[:])43 if c == utf8.RuneError {44 return "", errors.New("no such rune")45 }4647 // Is it a control character?48 if n == 1 && c <= keyUS {49 if s, ok := controlKeyNames[int(c)]; ok {50 return s, nil51 }52 }5354 // Is it a special key, like an arrow key?55 if s, ok := keyNames[string(buf[:n])]; ok {56 return s, nil57 }5859 // Nope, just a regular, ol' rune60 return string(c), nil61}