From 0395f75ea9aa2e4fea57846cd8004e0d37ac9a80 Mon Sep 17 00:00:00 2001 From: Gokcehan Date: Thu, 15 Sep 2016 20:09:57 +0300 Subject: [PATCH] break previewer output pipe when enough lines are read Mentioned in #5. --- ui.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/ui.go b/ui.go index 56fd346..884bb9b 100644 --- a/ui.go +++ b/ui.go @@ -335,33 +335,38 @@ func (ui *UI) loadFile(nav *Nav) { dir.load(nav.inds[path], nav.poss[path], nav.height, nav.names[path]) ui.dirprev = dir } else if curr.Mode().IsRegular() { - var reader io.Reader + var err error + var out io.ReadCloser if len(gOpts.previewer) != 0 { cmd := exec.Command(gOpts.previewer, path, strconv.Itoa(nav.height)) - out, err := cmd.Output() + out, err = cmd.StdoutPipe() if err != nil { msg := fmt.Sprintf("previewing file: %s", err) ui.message = msg log.Print(msg) } - reader = bytes.NewReader(out) + if err := cmd.Start(); err != nil { + msg := fmt.Sprintf("previewing file: %s", err) + ui.message = msg + log.Print(msg) + } } else { - f, err := os.Open(path) + out, err = os.Open(path) if err != nil { msg := fmt.Sprintf("opening file: %s", err) ui.message = msg log.Print(msg) } - - reader = f } ui.regprev = nil - buf := bufio.NewScanner(reader) + buf := bufio.NewScanner(out) + + defer out.Close() for i := 0; i < nav.height && buf.Scan(); i++ { for _, r := range buf.Text() {