From c19d3450a2bce95eba2131a10a8e9949e2f432b1 Mon Sep 17 00:00:00 2001 From: Gokcehan Date: Sun, 28 Aug 2016 15:04:57 +0300 Subject: [PATCH] add an option to filter file content for preview Mentioned in #5. --- comp.go | 1 + eval.go | 2 ++ opts.go | 2 +- ui.go | 15 +++++++++++++-- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/comp.go b/comp.go index 86e387e..7c61c4f 100644 --- a/comp.go +++ b/comp.go @@ -20,6 +20,7 @@ var ( "scrolloff", "tabstop", "ifs", + "previewer", "shell", "showinfo", "sortby", diff --git a/eval.go b/eval.go index cf1bca2..6f98dca 100644 --- a/eval.go +++ b/eval.go @@ -62,6 +62,8 @@ func (e *SetExpr) eval(app *App, args []string) { gOpts.tabstop = n case "ifs": gOpts.ifs = e.val + case "previewer": + gOpts.previewer = e.val case "shell": gOpts.shell = e.val case "showinfo": diff --git a/opts.go b/opts.go index 2207cb6..a0abc56 100644 --- a/opts.go +++ b/opts.go @@ -6,6 +6,7 @@ type Opts struct { scrolloff int tabstop int ifs string + previewer string shell string showinfo string sortby string @@ -21,7 +22,6 @@ func init() { gOpts.preview = true gOpts.scrolloff = 0 gOpts.tabstop = 8 - gOpts.ifs = "" gOpts.shell = envShell gOpts.showinfo = "none" gOpts.sortby = "name" diff --git a/ui.go b/ui.go index 38e91ec..6e4e1fe 100644 --- a/ui.go +++ b/ui.go @@ -6,6 +6,7 @@ import ( "fmt" "log" "os" + "os/exec" "path" "sort" "strconv" @@ -255,9 +256,19 @@ func (win *Win) printr(reg *os.File) error { return fmt.Errorf("printing regular file: %s", buf.Err()) } - reg.Seek(0, 0) + if len(gOpts.previewer) != 0 { + cmd := exec.Command(gOpts.previewer, reg.Name()) - buf = bufio.NewScanner(reg) + 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) + } for i := 0; i < win.h && buf.Scan(); i++ { win.print(2, i, fg, bg, buf.Text())