From 7543b2256cb86f721f189be1f612cf68db3578d0 Mon Sep 17 00:00:00 2001 From: Gokcehan Date: Sat, 5 May 2018 16:26:16 +0300 Subject: [PATCH] add support for alt keys Related #67 --- ui.go | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/ui.go b/ui.go index 1b1bf5d..0d17fec 100644 --- a/ui.go +++ b/ui.go @@ -7,10 +7,12 @@ import ( "log" "os" "path/filepath" + "regexp" "sort" "strconv" "strings" "text/tabwriter" + "time" "unicode" "unicode/utf8" @@ -409,11 +411,27 @@ func getWins() []*win { func newUI() *ui { wtot, htot := termbox.Size() - evChan := make(chan termbox.Event) - + evQueue := make(chan termbox.Event) go func() { 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 } +var altKey = regexp.MustCompile(``) + func (ui *ui) pollEvent() termbox.Event { select { case key := <-ui.keyChan: @@ -705,11 +725,15 @@ func (ui *ui) pollEvent() termbox.Event { if len(key) == 1 { ev.Ch, _ = utf8.DecodeRuneInString(key) } else { - switch key { - case "": + switch { + case key == "": ev.Ch = '<' - case "": + case key == "": ev.Ch = '>' + case altKey.MatchString(key): + match := altKey.FindStringSubmatch(key)[1] + ev.Ch, _ = utf8.DecodeRuneInString(match) + ev.Mod = termbox.ModAlt default: if val, ok := gValKey[key]; ok { ev.Key = val @@ -728,7 +752,14 @@ func (ui *ui) pollEvent() termbox.Event { func readCmdEvent(ch chan<- expr, ev termbox.Event) { 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 { val := gKeyVal[ev.Key] 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', '>') case ev.Ch == '>': 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: ui.keyCount = append(ui.keyCount, ev.Ch) default: