forked from Mirrors/bubbletea
Fill out constants and strings for control keys
This commit is contained in:
parent
84c9ca54bc
commit
91271cacab
|
@ -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
|
||||
|
|
|
@ -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
|
||||
)
|
||||
|
|
|
@ -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=
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
145
key.go
145
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?
|
||||
|
|
Loading…
Reference in New Issue