Fill out constants and strings for control keys

This commit is contained in:
Christian Rocha 2020-01-26 16:46:30 -05:00
parent 84c9ca54bc
commit 91271cacab
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018
7 changed files with 108 additions and 57 deletions

View File

@ -34,7 +34,7 @@ func update(message tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {
case tea.KeyMsg: case tea.KeyMsg:
switch msg.String() { switch msg.String() {
case "break": case "ctrl+c":
fallthrough fallthrough
case "esc": case "esc":
fallthrough fallthrough

View File

@ -5,7 +5,7 @@ go 1.13
replace github.com/charmbracelet/tea => ../ replace github.com/charmbracelet/tea => ../
require ( require (
github.com/charmbracelet/tea v0.0.0-20200125213400-f64f86f18301 github.com/charmbracelet/tea v0.0.0-20200126032228-29799c315162
github.com/charmbracelet/teaparty v0.0.0-20200118155738-c83a0bee59b9 github.com/charmbracelet/teaparty v0.0.0-20200126032257-419df0b7a206
github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776 github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776
) )

View File

@ -1,5 +1,7 @@
github.com/charmbracelet/teaparty v0.0.0-20200118155738-c83a0bee59b9 h1:YQvJgppGVexnzIJ+KJlK9lBYA3+zXfdqZO/5Ngedtb0= github.com/charmbracelet/teaparty v0.0.0-20200118155738-c83a0bee59b9 h1:YQvJgppGVexnzIJ+KJlK9lBYA3+zXfdqZO/5Ngedtb0=
github.com/charmbracelet/teaparty v0.0.0-20200118155738-c83a0bee59b9/go.mod h1:z8JWtuxM0oA+dZfi7BkgBW2YGbyOTbWAixFs46W3SK4= github.com/charmbracelet/teaparty v0.0.0-20200118155738-c83a0bee59b9/go.mod h1:z8JWtuxM0oA+dZfi7BkgBW2YGbyOTbWAixFs46W3SK4=
github.com/charmbracelet/teaparty v0.0.0-20200126032257-419df0b7a206 h1:sjDz9mRGgyWvFH1T2FWKjlUecL6jjLUoKszsYBX+H0E=
github.com/charmbracelet/teaparty v0.0.0-20200126032257-419df0b7a206/go.mod h1:pmTejx7xSMAHuh8aY5r1cwNzNiv0akVFNPeiae+e6RQ=
github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776 h1:VRIbnDWRmAh5yBdz+J6yFMF5vso1It6vn+WmM/5l7MA= github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776 h1:VRIbnDWRmAh5yBdz+J6yFMF5vso1It6vn+WmM/5l7MA=
github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776/go.mod h1:9wvnDu3YOfxzWM9Cst40msBF1C2UdQgDv962oTxSuMs= github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776/go.mod h1:9wvnDu3YOfxzWM9Cst40msBF1C2UdQgDv962oTxSuMs=
github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942 h1:A7GG7zcGjl3jqAqGPmcNjd/D9hzL95SuoOQAaFNdLU0= github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942 h1:A7GG7zcGjl3jqAqGPmcNjd/D9hzL95SuoOQAaFNdLU0=

View File

@ -41,7 +41,7 @@ func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {
switch msg.String() { switch msg.String() {
case "esc": case "esc":
fallthrough fallthrough
case "break": case "ctrl+c":
fallthrough fallthrough
case "q": case "q":
return m, tea.Quit return m, tea.Quit

View File

@ -53,10 +53,10 @@ func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {
switch msg := msg.(type) { switch msg := msg.(type) {
case tea.KeyMsg: case tea.KeyMsg:
switch msg.String() { switch msg.Type {
case "break": case tea.KeyCtrlC:
fallthrough fallthrough
case "esc": case tea.KeyEsc:
return m, tea.Quit return m, tea.Quit
} }

View File

@ -105,7 +105,7 @@ func updateChoices(msg tea.Msg, m Model) (tea.Model, tea.Cmd) {
fallthrough fallthrough
case "esc": case "esc":
fallthrough fallthrough
case "break": case "ctrl+c":
return m, tea.Quit return m, tea.Quit
} }
@ -128,7 +128,7 @@ func updateChosen(msg tea.Msg, m Model) (tea.Model, tea.Cmd) {
fallthrough fallthrough
case "esc": case "esc":
fallthrough fallthrough
case "break": case "ctrl+c":
return m, tea.Quit return m, tea.Quit
} }

145
key.go
View File

@ -13,7 +13,7 @@ type KeyMsg Key
func (k *KeyMsg) String() string { func (k *KeyMsg) String() string {
if k.Type == KeyRune { if k.Type == KeyRune {
return string(k.Rune) return string(k.Rune)
} else if s, ok := keyNames[k.Type]; ok { } else if s, ok := keyNames[int(k.Type)]; ok {
return s return s
} }
return "" return ""
@ -33,36 +33,6 @@ type Key struct {
// KeyType indicates the key pressed // KeyType indicates the key pressed
type KeyType int type KeyType int
// Possible keys
const (
KeyBreak KeyType = iota
KeyTab
KeyEnter
KeyEscape
KeyUp
KeyDown
KeyRight
KeyLeft
KeyUnitSeparator
KeyBackspace
KeyRune = -1
)
// Friendly key names
var keyNames = map[KeyType]string{
KeyBreak: "break",
KeyTab: "tab",
KeyEnter: "enter",
KeyEscape: "esc",
KeyUp: "up",
KeyDown: "down",
KeyRight: "right",
KeyLeft: "left",
KeyUnitSeparator: "us",
KeyBackspace: "backspace",
KeyRune: "rune",
}
// Control keys. I know we could do this with an iota, but the values are very // Control keys. I know we could do this with an iota, but the values are very
// specific, so we set the values explicitly to avoid any confusion. // specific, so we set the values explicitly to avoid any confusion.
// //
@ -77,7 +47,7 @@ const (
keyENQ = 5 // enquiry keyENQ = 5 // enquiry
keyACK = 6 // acknowledge keyACK = 6 // acknowledge
keyBEL = 7 // bell, \a keyBEL = 7 // bell, \a
keyBS = 8 /// backspace keyBS = 8 // backspace
keyHT = 9 // horizontal tabulation, \t keyHT = 9 // horizontal tabulation, \t
keyLF = 10 // line feed, \n keyLF = 10 // line feed, \n
keyVT = 11 // vertical tabulation \v keyVT = 11 // vertical tabulation \v
@ -105,14 +75,101 @@ const (
keyDEL = 127 // delete. on most systems this is mapped to backspace, I hear keyDEL = 127 // delete. on most systems this is mapped to backspace, I hear
) )
// Aliases
const (
KeyBreak = keyETX
KeyEnter = keyCR
KeyBackspace = keyBS
KeySpace = keySP
KeyEsc = keyESC
KeyEscape = keyESC
KeyDelete = keyDEL
KeyCtrlAt = keyNUL // ctrl+@
KeyCtrlA = keySOH
KeyCtrlB = keySTX
KeyCtrlC = keyETX
KeyCtrlD = keyEOT
KeyCtrlE = keyENQ
KeyCtrlF = keyACK
KeyCtrlG = keyBEL
KeyCtrlH = keyBS
KeyCtrlI = keyHT
KeyCtrlJ = keyLF
KeyCtrlK = keyVT
KeyCtrlL = keyFF
KeyCtrlM = keyCR
KeyCtrlN = keySO
KeyCtrlO = keySI
KeyCtrlP = keyDLE
KeyCtrlQ = keyDC1
KeyCtrlR = keyDC2
KeyCtrlS = keyDC3
KeyCtrlT = keyDC4
KeyCtrlU = keyNAK
KeyCtrlV = keyETB
KeyCtrlW = keyETB
KeyCtrlX = keyCAN
KeyCtrlY = keyEM
KeyCtrlZ = keySUB
KeyCtrlOpenBracket = keyESC // ctrl+[
KeyCtrlBackslash = keyFS // ctrl+\
KeyCtrlCloseBracket = keyGS // ctrl+]
KeyCtrlCaret = keyRS // ctrl+^
KeyCtrlUnderscore = keyUS // ctrl+_
KeyCtrlQuestionMark = keyDEL // ctrl+?
)
const (
KeyRune = -(iota + 1)
KeyUp
KeyDown
KeyRight
KeyLeft
)
// Mapping for control keys to friendly consts // Mapping for control keys to friendly consts
var controlKeys = map[int]KeyType{ var keyNames = map[int]string{
keyETX: KeyBreak, keyNUL: "ctrl+@", // also ctrl+`
keyLF: KeyEnter, keySOH: "ctrl+a",
keyCR: KeyEnter, keySTX: "ctrl+b",
keyESC: KeyEscape, keyETX: "ctrl+c",
keyUS: KeyUnitSeparator, keyEOT: "ctrl+d",
keyDEL: KeyBackspace, keyENQ: "ctrl+e",
keyACK: "ctrl+f",
keyBEL: "ctrl+g",
keyBS: "backspace", // also ctrl+h
keyHT: "tab", // also ctrl+i
keyLF: "ctrl+j",
keyVT: "ctrl+k",
keyFF: "ctrl+l",
keyCR: "enter",
keySO: "ctrl+n",
keySI: "ctrl+o",
keyDLE: "ctrl+p",
keyDC1: "ctrl+q",
keyDC2: "ctrl+r",
keyDC3: "ctrl+s",
keyDC4: "ctrl+t",
keyNAK: "ctrl+u",
keySYN: "ctrl+v",
keyETB: "ctrl+w",
keyCAN: "ctrl+x",
keyEM: "ctrl+y",
keySUB: "ctrl+z",
keyESC: "esc",
keyFS: "ctrl+\\",
keyGS: "ctrl+]",
keyRS: "ctrl+^",
keyUS: "ctrl+_",
keySP: "space",
keyDEL: "delete",
KeyRune: "rune",
KeyUp: "up",
KeyDown: "down",
KeyRight: "right",
KeyLeft: "left",
} }
// Mapping for sequences to consts // Mapping for sequences to consts
@ -142,15 +199,7 @@ func ReadKey(r io.Reader) (Key, error) {
// Is it a control character? // Is it a control character?
if n == 1 && c <= keyUS || c == keyDEL { if n == 1 && c <= keyUS || c == keyDEL {
if k, ok := controlKeys[int(c)]; ok { return Key{Type: KeyType(c)}, nil
return Key{Type: k}, nil
}
}
if n == 1 && c <= keyUS {
if k, ok := controlKeys[int(c)]; ok {
return Key{Type: k}, nil
}
} }
// Is it a special sequence, like an arrow key? // Is it a special sequence, like an arrow key?