add an option to filter file content for preview

Mentioned in #5.
This commit is contained in:
Gokcehan 2016-08-28 15:04:57 +03:00
parent e86b8b34bd
commit c19d3450a2
4 changed files with 17 additions and 3 deletions

View File

@ -20,6 +20,7 @@ var (
"scrolloff",
"tabstop",
"ifs",
"previewer",
"shell",
"showinfo",
"sortby",

View File

@ -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":

View File

@ -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"

15
ui.go
View File

@ -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())