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", "scrolloff",
"tabstop", "tabstop",
"ifs", "ifs",
"previewer",
"shell", "shell",
"showinfo", "showinfo",
"sortby", "sortby",

View File

@ -62,6 +62,8 @@ func (e *SetExpr) eval(app *App, args []string) {
gOpts.tabstop = n gOpts.tabstop = n
case "ifs": case "ifs":
gOpts.ifs = e.val gOpts.ifs = e.val
case "previewer":
gOpts.previewer = e.val
case "shell": case "shell":
gOpts.shell = e.val gOpts.shell = e.val
case "showinfo": case "showinfo":

View File

@ -6,6 +6,7 @@ type Opts struct {
scrolloff int scrolloff int
tabstop int tabstop int
ifs string ifs string
previewer string
shell string shell string
showinfo string showinfo string
sortby string sortby string
@ -21,7 +22,6 @@ func init() {
gOpts.preview = true gOpts.preview = true
gOpts.scrolloff = 0 gOpts.scrolloff = 0
gOpts.tabstop = 8 gOpts.tabstop = 8
gOpts.ifs = ""
gOpts.shell = envShell gOpts.shell = envShell
gOpts.showinfo = "none" gOpts.showinfo = "none"
gOpts.sortby = "name" gOpts.sortby = "name"

15
ui.go
View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"os/exec"
"path" "path"
"sort" "sort"
"strconv" "strconv"
@ -255,9 +256,19 @@ func (win *Win) printr(reg *os.File) error {
return fmt.Errorf("printing regular file: %s", buf.Err()) 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++ { for i := 0; i < win.h && buf.Scan(); i++ {
win.print(2, i, fg, bg, buf.Text()) win.print(2, i, fg, bg, buf.Text())