add support for alt keys

Related #67
This commit is contained in:
Gokcehan 2018-05-05 16:26:16 +03:00
parent 0855507298
commit 7543b2256c

47
ui.go
View File

@ -7,10 +7,12 @@ import (
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
"regexp"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
"text/tabwriter" "text/tabwriter"
"time"
"unicode" "unicode"
"unicode/utf8" "unicode/utf8"
@ -409,11 +411,27 @@ func getWins() []*win {
func newUI() *ui { func newUI() *ui {
wtot, htot := termbox.Size() wtot, htot := termbox.Size()
evChan := make(chan termbox.Event) evQueue := make(chan termbox.Event)
go func() { go func() {
for { for {
evChan <- termbox.PollEvent() evQueue <- termbox.PollEvent()
}
}()
evChan := make(chan termbox.Event)
go func() {
for {
ev := <-evQueue
if ev.Type == termbox.EventKey && ev.Key == termbox.KeyEsc {
select {
case ev2 := <-evQueue:
ev2.Mod = termbox.ModAlt
evChan <- ev2
continue
case <-time.After(100 * time.Millisecond):
}
}
evChan <- ev
} }
}() }()
@ -697,6 +715,8 @@ func listBinds(binds map[string]expr) *bytes.Buffer {
return b return b
} }
var altKey = regexp.MustCompile(`<a-(.)>`)
func (ui *ui) pollEvent() termbox.Event { func (ui *ui) pollEvent() termbox.Event {
select { select {
case key := <-ui.keyChan: case key := <-ui.keyChan:
@ -705,11 +725,15 @@ func (ui *ui) pollEvent() termbox.Event {
if len(key) == 1 { if len(key) == 1 {
ev.Ch, _ = utf8.DecodeRuneInString(key) ev.Ch, _ = utf8.DecodeRuneInString(key)
} else { } else {
switch key { switch {
case "<lt>": case key == "<lt>":
ev.Ch = '<' ev.Ch = '<'
case "<gt>": case key == "<gt>":
ev.Ch = '>' ev.Ch = '>'
case altKey.MatchString(key):
match := altKey.FindStringSubmatch(key)[1]
ev.Ch, _ = utf8.DecodeRuneInString(match)
ev.Mod = termbox.ModAlt
default: default:
if val, ok := gValKey[key]; ok { if val, ok := gValKey[key]; ok {
ev.Key = val ev.Key = val
@ -728,7 +752,14 @@ func (ui *ui) pollEvent() termbox.Event {
func readCmdEvent(ch chan<- expr, ev termbox.Event) { func readCmdEvent(ch chan<- expr, ev termbox.Event) {
if ev.Ch != 0 { if ev.Ch != 0 {
ch <- &callExpr{"cmd-insert", []string{string(ev.Ch)}, 1} if ev.Mod == termbox.ModAlt {
val := []rune{'<', 'a', '-', ev.Ch, '>'}
if expr, ok := gOpts.cmdkeys[string(val)]; ok {
ch <- expr
}
} else {
ch <- &callExpr{"cmd-insert", []string{string(ev.Ch)}, 1}
}
} else { } else {
val := gKeyVal[ev.Key] val := gKeyVal[ev.Key]
if expr, ok := gOpts.cmdkeys[string(val)]; ok { if expr, ok := gOpts.cmdkeys[string(val)]; ok {
@ -749,6 +780,8 @@ func (ui *ui) readEvent(ch chan<- expr, ev termbox.Event) {
ui.keyAcc = append(ui.keyAcc, '<', 'l', 't', '>') ui.keyAcc = append(ui.keyAcc, '<', 'l', 't', '>')
case ev.Ch == '>': case ev.Ch == '>':
ui.keyAcc = append(ui.keyAcc, '<', 'g', 't', '>') ui.keyAcc = append(ui.keyAcc, '<', 'g', 't', '>')
case ev.Mod == termbox.ModAlt:
ui.keyAcc = append(ui.keyAcc, '<', 'a', '-', ev.Ch, '>')
case unicode.IsDigit(ev.Ch) && len(ui.keyAcc) == 0: case unicode.IsDigit(ev.Ch) && len(ui.keyAcc) == 0:
ui.keyCount = append(ui.keyCount, ev.Ch) ui.keyCount = append(ui.keyCount, ev.Ch)
default: default: