From ef340a533dc90996d5c95121d188e32ed30b90dd Mon Sep 17 00:00:00 2001 From: KenjiTakahashi Date: Thu, 1 Sep 2016 21:03:18 +0200 Subject: [PATCH] Do binary check after calling previewer Many files valid for previewer, such as archives, are treated as binary and thus were not passed to the previewer. Also sanitizes the previewer output, to some extent. --- ui.go | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/ui.go b/ui.go index 85fb24e..6cfef3a 100644 --- a/ui.go +++ b/ui.go @@ -4,6 +4,7 @@ import ( "bufio" "bytes" "fmt" + "io" "log" "os" "os/exec" @@ -235,9 +236,24 @@ func (win *Win) printd(dir *Dir, marks map[string]bool) { } func (win *Win) printr(reg *os.File) error { + var reader io.ReadSeeker + + if len(gOpts.previewer) != 0 { + cmd := exec.Command(gOpts.previewer, reg.Name(), strconv.Itoa(win.w), strconv.Itoa(win.h)) + + out, err := cmd.Output() + if err != nil { + log.Printf("previewing file: %s", err) + } + + reader = bytes.NewReader(out) + } else { + reader = reg + } + fg, bg := termbox.ColorDefault, termbox.ColorDefault - buf := bufio.NewScanner(reg) + buf := bufio.NewScanner(reader) for i := 0; i < win.h && buf.Scan(); i++ { for _, r := range buf.Text() { @@ -256,19 +272,8 @@ func (win *Win) printr(reg *os.File) error { return fmt.Errorf("printing regular file: %s", buf.Err()) } - if len(gOpts.previewer) != 0 { - cmd := exec.Command(gOpts.previewer, reg.Name(), strconv.Itoa(win.w), strconv.Itoa(win.h)) - - out, err := cmd.Output() - if err != nil { - log.Printf("previewing file: %s", err) - } - - buf = bufio.NewScanner(bytes.NewReader(out)) - } else { - reg.Seek(0, 0) - buf = bufio.NewScanner(reg) - } + reader.Seek(0, 0) + buf = bufio.NewScanner(reader) for i := 0; i < win.h && buf.Scan(); i++ { win.print(2, i, fg, bg, buf.Text())