From 91271cacab68724363d5e6791012708a7b72ce20 Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Sun, 26 Jan 2020 16:46:30 -0500 Subject: [PATCH] Fill out constants and strings for control keys --- examples/fullscreen/main.go | 2 +- examples/go.mod | 4 +- examples/go.sum | 2 + examples/http/main.go | 2 +- examples/input/main.go | 6 +- examples/views/main.go | 4 +- key.go | 145 ++++++++++++++++++++++++------------ 7 files changed, 108 insertions(+), 57 deletions(-) diff --git a/examples/fullscreen/main.go b/examples/fullscreen/main.go index 1d53cf3..90d5d05 100644 --- a/examples/fullscreen/main.go +++ b/examples/fullscreen/main.go @@ -34,7 +34,7 @@ func update(message tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) { case tea.KeyMsg: switch msg.String() { - case "break": + case "ctrl+c": fallthrough case "esc": fallthrough diff --git a/examples/go.mod b/examples/go.mod index d5ebaae..cba8229 100644 --- a/examples/go.mod +++ b/examples/go.mod @@ -5,7 +5,7 @@ go 1.13 replace github.com/charmbracelet/tea => ../ require ( - github.com/charmbracelet/tea v0.0.0-20200125213400-f64f86f18301 - github.com/charmbracelet/teaparty v0.0.0-20200118155738-c83a0bee59b9 + github.com/charmbracelet/tea v0.0.0-20200126032228-29799c315162 + github.com/charmbracelet/teaparty v0.0.0-20200126032257-419df0b7a206 github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776 ) diff --git a/examples/go.sum b/examples/go.sum index 987a27e..0ab1bb2 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -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/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/go.mod h1:9wvnDu3YOfxzWM9Cst40msBF1C2UdQgDv962oTxSuMs= github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942 h1:A7GG7zcGjl3jqAqGPmcNjd/D9hzL95SuoOQAaFNdLU0= diff --git a/examples/http/main.go b/examples/http/main.go index 067e25a..73ec44c 100644 --- a/examples/http/main.go +++ b/examples/http/main.go @@ -41,7 +41,7 @@ func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) { switch msg.String() { case "esc": fallthrough - case "break": + case "ctrl+c": fallthrough case "q": return m, tea.Quit diff --git a/examples/input/main.go b/examples/input/main.go index c4e0cd8..d5f50fa 100644 --- a/examples/input/main.go +++ b/examples/input/main.go @@ -53,10 +53,10 @@ func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case tea.KeyMsg: - switch msg.String() { - case "break": + switch msg.Type { + case tea.KeyCtrlC: fallthrough - case "esc": + case tea.KeyEsc: return m, tea.Quit } diff --git a/examples/views/main.go b/examples/views/main.go index 6a3281b..dd80950 100644 --- a/examples/views/main.go +++ b/examples/views/main.go @@ -105,7 +105,7 @@ func updateChoices(msg tea.Msg, m Model) (tea.Model, tea.Cmd) { fallthrough case "esc": fallthrough - case "break": + case "ctrl+c": return m, tea.Quit } @@ -128,7 +128,7 @@ func updateChosen(msg tea.Msg, m Model) (tea.Model, tea.Cmd) { fallthrough case "esc": fallthrough - case "break": + case "ctrl+c": return m, tea.Quit } diff --git a/key.go b/key.go index aad28a4..eeb8eff 100644 --- a/key.go +++ b/key.go @@ -13,7 +13,7 @@ type KeyMsg Key func (k *KeyMsg) String() string { if k.Type == KeyRune { return string(k.Rune) - } else if s, ok := keyNames[k.Type]; ok { + } else if s, ok := keyNames[int(k.Type)]; ok { return s } return "" @@ -33,36 +33,6 @@ type Key struct { // KeyType indicates the key pressed 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 // specific, so we set the values explicitly to avoid any confusion. // @@ -77,7 +47,7 @@ const ( keyENQ = 5 // enquiry keyACK = 6 // acknowledge keyBEL = 7 // bell, \a - keyBS = 8 /// backspace + keyBS = 8 // backspace keyHT = 9 // horizontal tabulation, \t keyLF = 10 // line feed, \n keyVT = 11 // vertical tabulation \v @@ -105,14 +75,101 @@ const ( 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 -var controlKeys = map[int]KeyType{ - keyETX: KeyBreak, - keyLF: KeyEnter, - keyCR: KeyEnter, - keyESC: KeyEscape, - keyUS: KeyUnitSeparator, - keyDEL: KeyBackspace, +var keyNames = map[int]string{ + keyNUL: "ctrl+@", // also ctrl+` + keySOH: "ctrl+a", + keySTX: "ctrl+b", + keyETX: "ctrl+c", + keyEOT: "ctrl+d", + 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 @@ -142,15 +199,7 @@ func ReadKey(r io.Reader) (Key, error) { // Is it a control character? if n == 1 && c <= keyUS || c == keyDEL { - if k, ok := controlKeys[int(c)]; ok { - return Key{Type: k}, nil - } - } - - if n == 1 && c <= keyUS { - if k, ok := controlKeys[int(c)]; ok { - return Key{Type: k}, nil - } + return Key{Type: KeyType(c)}, nil } // Is it a special sequence, like an arrow key?