From 8229d972b9d1c8523ed8f209b61ac49169db4e16 Mon Sep 17 00:00:00 2001 From: Gokcehan Date: Thu, 8 Nov 2018 20:00:54 +0300 Subject: [PATCH] only draw ui when the output pauses in shell-pipe --- app.go | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/app.go b/app.go index 81bd65e4..0ad6a8b9 100644 --- a/app.go +++ b/app.go @@ -335,15 +335,34 @@ func (app *app) runShell(s string, args []string, prefix string) { app.ui.cmdPrefix = ">" reader := bufio.NewReader(out) - for { - b, err := reader.ReadByte() - if err == io.EOF { - break + bytes := make(chan byte, 1000) + + go func() { + for { + b, err := reader.ReadByte() + if err == io.EOF { + break + } + bytes <- b } - app.cmdOutBuf = append(app.cmdOutBuf, b) - app.ui.exprChan <- &callExpr{"echo", []string{string(app.cmdOutBuf)}, 1} - if b == '\n' || b == '\r' { - app.cmdOutBuf = nil + close(bytes) + }() + + 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} } }