implement asynchronous read commands
This commit changes previous reading command implementation to an asynchronous implementation. By the nature of this change, this commit touches many places in the ui and evaluator. Aim is to fix the following problems: - There is no race condition anymore when reading commands and other commands update the ui at the same time. - Autocompletion and keymenu is now drawn in the main draw event. This should fix some ui glitches when a new menu is smaller than the previous one. - Window resize event when reading a command is now properly handled. - Readline actions are now regular commands. This should make it possible to change the default keybindings for these actions in the future. Mentioned in #36.
This commit is contained in:
1 parent
f66a4a4a2e
commit
5f87cb2542
3 files changed
+343
-341
No files matched your search
@@ -8,9 +8,6 @@ import (
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"github.com/nsf/termbox-go"
|
||||
)
|
||||
|
||||
type App struct {
|
||||
@@ -55,144 +52,11 @@ func waitKey() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// This function is used to read expressions on the client side. Prompting
|
||||
// commands (e.g. "read") are recognized and evaluated while being read here.
|
||||
// Digits are interpreted as command counts but this is only done for digits
|
||||
// preceding any non-digit characters (e.g. "42y2k" as 42 times "y2k").
|
||||
func (app *App) readExpr() chan MultiExpr {
|
||||
ch := make(chan MultiExpr)
|
||||
|
||||
renew := &CallExpr{"renew", nil}
|
||||
count := 1
|
||||
|
||||
var acc []rune
|
||||
var cnt []rune
|
||||
|
||||
go func() {
|
||||
for {
|
||||
switch ev := app.ui.pollEvent(); ev.Type {
|
||||
case termbox.EventKey:
|
||||
if ev.Ch != 0 {
|
||||
switch {
|
||||
case ev.Ch == '<':
|
||||
acc = append(acc, '<', 'l', 't', '>')
|
||||
case ev.Ch == '>':
|
||||
acc = append(acc, '<', 'g', 't', '>')
|
||||
case unicode.IsDigit(ev.Ch) && len(acc) == 0:
|
||||
cnt = append(cnt, ev.Ch)
|
||||
default:
|
||||
acc = append(acc, ev.Ch)
|
||||
}
|
||||
} else {
|
||||
val := gKeyVal[ev.Key]
|
||||
if string(val) == "<esc>" {
|
||||
ch <- MultiExpr{renew, 1}
|
||||
acc = nil
|
||||
cnt = nil
|
||||
}
|
||||
acc = append(acc, val...)
|
||||
}
|
||||
|
||||
binds, ok := findBinds(gOpts.keys, string(acc))
|
||||
|
||||
switch len(binds) {
|
||||
case 0:
|
||||
app.ui.message = fmt.Sprintf("unknown mapping: %s", string(acc))
|
||||
ch <- MultiExpr{renew, 1}
|
||||
acc = nil
|
||||
cnt = nil
|
||||
case 1:
|
||||
if ok {
|
||||
if len(cnt) > 0 {
|
||||
c, err := strconv.Atoi(string(cnt))
|
||||
if err != nil {
|
||||
log.Printf("converting command count: %s", err)
|
||||
}
|
||||
count = c
|
||||
} else {
|
||||
count = 1
|
||||
}
|
||||
expr := gOpts.keys[string(acc)]
|
||||
switch expr.(type) {
|
||||
case *CallExpr:
|
||||
switch expr.(*CallExpr).name {
|
||||
case "read",
|
||||
"read-shell",
|
||||
"read-shell-wait",
|
||||
"read-shell-async",
|
||||
"search",
|
||||
"search-back",
|
||||
"push":
|
||||
expr.eval(app, nil)
|
||||
app.ui.loadFile(app.nav)
|
||||
app.ui.draw(app.nav)
|
||||
default:
|
||||
ch <- MultiExpr{expr, count}
|
||||
}
|
||||
default:
|
||||
ch <- MultiExpr{expr, count}
|
||||
}
|
||||
acc = nil
|
||||
cnt = nil
|
||||
}
|
||||
if len(acc) > 0 {
|
||||
app.ui.listBinds(binds)
|
||||
}
|
||||
default:
|
||||
if ok {
|
||||
// TODO: use a delay
|
||||
if len(cnt) > 0 {
|
||||
c, err := strconv.Atoi(string(cnt))
|
||||
if err != nil {
|
||||
log.Printf("converting command count: %s", err)
|
||||
}
|
||||
count = c
|
||||
} else {
|
||||
count = 1
|
||||
}
|
||||
expr := gOpts.keys[string(acc)]
|
||||
switch expr.(type) {
|
||||
case *CallExpr:
|
||||
switch expr.(*CallExpr).name {
|
||||
case "read",
|
||||
"read-shell",
|
||||
"read-shell-wait",
|
||||
"read-shell-async",
|
||||
"search",
|
||||
"search-back",
|
||||
"push":
|
||||
expr.eval(app, nil)
|
||||
app.ui.loadFile(app.nav)
|
||||
app.ui.draw(app.nav)
|
||||
default:
|
||||
ch <- MultiExpr{expr, count}
|
||||
}
|
||||
default:
|
||||
ch <- MultiExpr{expr, count}
|
||||
}
|
||||
acc = nil
|
||||
cnt = nil
|
||||
}
|
||||
if len(acc) > 0 {
|
||||
app.ui.listBinds(binds)
|
||||
}
|
||||
}
|
||||
case termbox.EventResize:
|
||||
ch <- MultiExpr{renew, 1}
|
||||
default:
|
||||
// TODO: handle other events
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return ch
|
||||
}
|
||||
|
||||
// This is the main event loop of the application. There are two channels to
|
||||
// read expressions from client and server. Reading and evaluation are done on
|
||||
// different goroutines except for prompting commands (e.g. "read").
|
||||
// separate goroutines.
|
||||
func (app *App) handleInp() {
|
||||
clientChan := app.readExpr()
|
||||
clientChan := app.ui.readExpr()
|
||||
|
||||
var serverChan chan Expr
|
||||
|
||||
|
||||
Reference in new issue
Block a user