clean command resources after preview script call

Mentioned in #5.
This commit is contained in:
Gokcehan 2016-09-16 13:27:38 +03:00
parent 878d28237a
commit f82073603f

17
ui.go
View File

@ -335,13 +335,12 @@ func (ui *UI) loadFile(nav *Nav) {
dir.load(nav.inds[path], nav.poss[path], nav.height, nav.names[path]) dir.load(nav.inds[path], nav.poss[path], nav.height, nav.names[path])
ui.dirprev = dir ui.dirprev = dir
} else if curr.Mode().IsRegular() { } else if curr.Mode().IsRegular() {
var err error var reader io.Reader
var out io.ReadCloser
if len(gOpts.previewer) != 0 { if len(gOpts.previewer) != 0 {
cmd := exec.Command(gOpts.previewer, path, strconv.Itoa(nav.height)) cmd := exec.Command(gOpts.previewer, path, strconv.Itoa(nav.height))
out, err = cmd.StdoutPipe() out, err := cmd.StdoutPipe()
if err != nil { if err != nil {
msg := fmt.Sprintf("previewing file: %s", err) msg := fmt.Sprintf("previewing file: %s", err)
ui.message = msg ui.message = msg
@ -353,20 +352,24 @@ func (ui *UI) loadFile(nav *Nav) {
ui.message = msg ui.message = msg
log.Print(msg) log.Print(msg)
} }
defer cmd.Wait()
reader = out
} else { } else {
out, err = os.Open(path) f, err := os.Open(path)
if err != nil { if err != nil {
msg := fmt.Sprintf("opening file: %s", err) msg := fmt.Sprintf("opening file: %s", err)
ui.message = msg ui.message = msg
log.Print(msg) log.Print(msg)
} }
defer f.Close()
reader = f
} }
ui.regprev = nil ui.regprev = nil
buf := bufio.NewScanner(out) buf := bufio.NewScanner(reader)
defer out.Close()
for i := 0; i < nav.height && buf.Scan(); i++ { for i := 0; i < nav.height && buf.Scan(); i++ {
for _, r := range buf.Text() { for _, r := range buf.Text() {