From dd6a85f480e600df89e33291df502084ef27757b Mon Sep 17 00:00:00 2001 From: Gokcehan Date: Fri, 8 Feb 2019 19:06:37 +0300 Subject: [PATCH] only echo when the output pauses in shell-pipe --- app.go | 45 +++++++++++++++++++++++++++++++++++---------- client.go | 6 ------ 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/app.go b/app.go index cc769d8..d2ac00f 100644 --- a/app.go +++ b/app.go @@ -136,6 +136,12 @@ func (app *app) loop() { clientChan := app.ui.readExpr() serverChan := readExpr() + for _, path := range gConfigPaths { + if _, err := os.Stat(path); !os.IsNotExist(err) { + app.readFile(path) + } + } + if gCommand != "" { p := newParser(strings.NewReader(gCommand)) if e := p.parseExpr(); e != nil { @@ -336,22 +342,41 @@ func (app *app) runShell(s string, args []string, prefix string) { switch prefix { case "%": - go func() { - app.cmd = cmd - app.cmdOutBuf = nil - app.ui.msg = "" - app.ui.cmdPrefix = ">" + app.cmd = cmd + app.cmdOutBuf = nil + app.ui.msg = "" + app.ui.cmdPrefix = ">" - reader := bufio.NewReader(out) + reader := bufio.NewReader(out) + bytes := make(chan byte, 1000) + + go func() { for { b, err := reader.ReadByte() if err == io.EOF { break } - app.cmdOutBuf = append(app.cmdOutBuf, b) - app.ui.exprChan <- &callExpr{"echo", []string{string(app.cmdOutBuf)}, 1} - if b == '\n' || b == '\r' { - app.cmdOutBuf = nil + bytes <- b + } + close(bytes) + }() + + go func() { + loop: + for { + select { + case b, ok := <-bytes: + switch { + case !ok: + break loop + case b == '\n' || b == '\r': + app.ui.exprChan <- &callExpr{"echo", []string{string(app.cmdOutBuf)}, 1} + app.cmdOutBuf = nil + default: + app.cmdOutBuf = append(app.cmdOutBuf, b) + } + default: + app.ui.exprChan <- &callExpr{"echo", []string{string(app.cmdOutBuf)}, 1} } } diff --git a/client.go b/client.go index 28c9489..0686b7d 100644 --- a/client.go +++ b/client.go @@ -33,12 +33,6 @@ func run() { app := newApp() - for _, path := range gConfigPaths { - if _, err := os.Stat(path); !os.IsNotExist(err) { - app.readFile(path) - } - } - if err := app.nav.readMarks(); err != nil { app.ui.printf("reading marks file: %s", err) }