diff --git a/app.go b/app.go index 92633198..cd921a85 100644 --- a/app.go +++ b/app.go @@ -3,6 +3,7 @@ package main import ( "fmt" "log" + "net" "os" "os/exec" "strings" @@ -18,6 +19,7 @@ func newApp() *App { ui := newUI() nav := newNav(ui.wins[0].h) quit := make(chan bool) + return &App{ ui: ui, nav: nav, @@ -50,6 +52,16 @@ func waitKey() error { } func (app *App) handleInp() { + clientChan := app.ui.readExpr(app) + + c, err := net.Dial("unix", gSocketPath) + if err != nil { + msg := fmt.Sprintf("connecting server: %s", err) + app.ui.message = msg + log.Printf(msg) + } + serverChan := readExpr(c) + for { select { case <-app.quit: @@ -71,15 +83,14 @@ func (app *App) handleInp() { } return - default: - e, c := app.ui.getExpr(app.nav) - if e == nil { - continue - } - for i := 0; i < c; i++ { - e.eval(app, nil) + case e := <-clientChan: + for i := 0; i < e.count; i++ { + e.expr.eval(app, nil) } app.ui.draw(app.nav) + case e := <-serverChan: + e.eval(app, nil) + app.ui.draw(app.nav) } } } diff --git a/client.go b/client.go index d647dc0b..d161e014 100644 --- a/client.go +++ b/client.go @@ -6,6 +6,7 @@ import ( "log" "net" "os" + "strings" "github.com/nsf/termbox-go" ) @@ -56,6 +57,27 @@ func client() { app.handleInp() } +func readExpr(c net.Conn) chan Expr { + ch := make(chan Expr) + + go func() { + fmt.Fprintln(c, "conn") + + s := bufio.NewScanner(c) + for s.Scan() { + log.Printf("reading expression: %s", s.Text()) + p := newParser(strings.NewReader(s.Text())) + if p.parse() { + ch <- p.expr + } + } + + c.Close() + }() + + return ch +} + func saveFiles(list []string, keep bool) error { c, err := net.Dial("unix", gSocketPath) if err != nil { diff --git a/server.go b/server.go index e5f9c6b6..abff940b 100644 --- a/server.go +++ b/server.go @@ -11,6 +11,7 @@ import ( var ( gKeepFile bool gFileList []string + gConnList []net.Conn ) func serve() { @@ -38,23 +39,35 @@ func listen(l net.Listener) { if err != nil { log.Printf("accepting connection: %s", err) } - defer c.Close() + go handleConn(c) + } +} - s := bufio.NewScanner(c) +func handleConn(c net.Conn) { + s := bufio.NewScanner(c) - for s.Scan() { - switch s.Text() { - case "save": - saveFilesServer(s) - log.Printf("listen: save, list: %v, keep: %t", gFileList, gKeepFile) - case "load": - loadFilesServer(c) - log.Printf("listen: load, keep: %t", gKeepFile) - default: - log.Print("listen: unexpected command") + for s.Scan() { + switch s.Text() { + case "save": + saveFilesServer(s) + log.Printf("listen: save, list: %v, keep: %t", gFileList, gKeepFile) + case "load": + loadFilesServer(c) + log.Printf("listen: load, keep: %t", gKeepFile) + case "conn": + gConnList = append(gConnList, c) + log.Print("listen: conn") + case "ping": + for _, c := range gConnList { + fmt.Fprintln(c, "echo ping from server") } + log.Print("listen: ping") + default: + log.Print("listen: unexpected command") } } + + c.Close() } func saveFilesServer(s *bufio.Scanner) { diff --git a/ui.go b/ui.go index 41def4e9..374a1a4a 100644 --- a/ui.go +++ b/ui.go @@ -570,84 +570,131 @@ func (ui *UI) pollEvent() termbox.Event { return termbox.PollEvent() } -func (ui *UI) getExpr(nav *Nav) (expr Expr, count int) { - expr = &CallExpr{"renew", nil} - count = 1 +type MultiExpr struct { + expr Expr + count int +} + +func (ui *UI) readExpr(app *App) chan MultiExpr { + ch := make(chan MultiExpr) + + renew := &CallExpr{"renew", nil} + count := 1 var acc []rune var cnt []rune - for { - switch ev := 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', '>') - // Interpret digits as command count but only do this for - // digits preceding any non-digit characters - // (e.g. "42y2k" as 42 times "y2k"). - case unicode.IsDigit(ev.Ch) && len(acc) == 0: - cnt = append(cnt, ev.Ch) - default: - acc = append(acc, ev.Ch) + go func() { + for { + switch ev := 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', '>') + // Interpret digits as command count but only do this for + // digits preceding any non-digit characters + // (e.g. "42y2k" as 42 times "y2k"). + 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) == "" { + ch <- MultiExpr{renew, 1} + acc = nil + cnt = nil + } + acc = append(acc, val...) } - } else { - val := gKeyVal[ev.Key] - if string(val) == "" { + + binds, ok := findBinds(gOpts.keys, string(acc)) + + switch len(binds) { + case 0: + ui.message = fmt.Sprintf("unknown mapping: %s", string(acc)) + ch <- MultiExpr{renew, 1} acc = nil - return - } - acc = append(acc, val...) - } - - binds, ok := findBinds(gOpts.keys, string(acc)) - - switch len(binds) { - case 0: - ui.message = fmt.Sprintf("unknown mapping: %s", string(acc)) - acc = nil - return - case 1: - if ok { - if len(cnt) > 0 { - c, err := strconv.Atoi(string(cnt)) - if err != nil { - log.Printf("converting command count: %s", err) + 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 } - count = c + expr := gOpts.keys[string(acc)] + switch expr.(type) { + case *CallExpr: + if expr.(*CallExpr).name == "read" || + expr.(*CallExpr).name == "read-shell" || + expr.(*CallExpr).name == "read-shell-wait" || + expr.(*CallExpr).name == "read-shell-async" { + expr.eval(app, nil) + ui.draw(app.nav) + } else { + ch <- MultiExpr{expr, count} + } + default: + ch <- MultiExpr{expr, count} + } + acc = nil + cnt = nil + } + if len(acc) > 0 { + 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: + if expr.(*CallExpr).name == "read" || + expr.(*CallExpr).name == "read-shell" || + expr.(*CallExpr).name == "read-shell-wait" || + expr.(*CallExpr).name == "read-shell-async" { + expr.eval(app, nil) + ui.draw(app.nav) + } else { + ch <- MultiExpr{expr, count} + } + default: + ch <- MultiExpr{expr, count} + } + acc = nil + cnt = nil + } + if len(acc) > 0 { + ui.listBinds(binds) } - return gOpts.keys[string(acc)], count - } - ui.draw(nav) - if len(acc) > 0 { - ui.listBinds(binds) } + case termbox.EventResize: + ch <- MultiExpr{renew, 1} 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 - } - return gOpts.keys[string(acc)], count - } - ui.draw(nav) - if len(acc) > 0 { - ui.listBinds(binds) - } + // TODO: handle other events } - case termbox.EventResize: - return - default: - // TODO: handle other events } - } + }() + + return ch } func (ui *UI) prompt(nav *Nav, pref string) string {