diff --git a/client.go b/client.go index 5dd334d..17bee95 100644 --- a/client.go +++ b/client.go @@ -38,7 +38,7 @@ func client() { app.ui.message = msg log.Printf(msg) } else { - app.ui.echoFileInfo(app.nav) + app.ui.loadFile(app.nav) } defer rcFile.Close() diff --git a/eval.go b/eval.go index e352b56..b388614 100644 --- a/eval.go +++ b/eval.go @@ -125,29 +125,29 @@ func (e *CallExpr) eval(app *App, args []string) { switch e.name { case "up": app.nav.up(1) - app.ui.echoFileInfo(app.nav) + app.ui.loadFile(app.nav) case "half-up": app.nav.up(app.nav.height / 2) - app.ui.echoFileInfo(app.nav) + app.ui.loadFile(app.nav) case "page-up": app.nav.up(app.nav.height) - app.ui.echoFileInfo(app.nav) + app.ui.loadFile(app.nav) case "down": app.nav.down(1) - app.ui.echoFileInfo(app.nav) + app.ui.loadFile(app.nav) case "half-down": app.nav.down(app.nav.height / 2) - app.ui.echoFileInfo(app.nav) + app.ui.loadFile(app.nav) case "page-down": app.nav.down(app.nav.height) - app.ui.echoFileInfo(app.nav) + app.ui.loadFile(app.nav) case "updir": if err := app.nav.updir(); err != nil { app.ui.message = err.Error() log.Print(err) return } - app.ui.echoFileInfo(app.nav) + app.ui.loadFile(app.nav) case "open": dir := app.nav.currDir() @@ -171,7 +171,7 @@ func (e *CallExpr) eval(app *App, args []string) { log.Print(err) return } - app.ui.echoFileInfo(app.nav) + app.ui.loadFile(app.nav) return } @@ -203,14 +203,13 @@ func (e *CallExpr) eval(app *App, args []string) { gExitFlag = true case "bot": app.nav.bot() - app.ui.echoFileInfo(app.nav) + app.ui.loadFile(app.nav) case "top": app.nav.top() - app.ui.echoFileInfo(app.nav) + app.ui.loadFile(app.nav) case "read": s := app.ui.prompt(app.nav, ":") if len(s) == 0 { - app.ui.echoFileInfo(app.nav) return } log.Printf("command: %s", s) @@ -224,23 +223,38 @@ func (e *CallExpr) eval(app *App, args []string) { } case "read-shell": s := app.ui.prompt(app.nav, "$") + if len(s) == 0 { + return + } log.Printf("shell: %s", s) app.runShell(s, nil, false, false) case "read-shell-wait": s := app.ui.prompt(app.nav, "!") + if len(s) == 0 { + return + } log.Printf("shell-wait: %s", s) app.runShell(s, nil, true, false) case "read-shell-async": s := app.ui.prompt(app.nav, "&") + if len(s) == 0 { + return + } log.Printf("shell-async: %s", s) app.runShell(s, nil, false, true) case "search": s := app.ui.prompt(app.nav, "/") + if len(s) == 0 { + return + } log.Printf("search: %s", s) app.ui.message = "sorry, search is not implemented yet!" // TODO: implement case "search-back": s := app.ui.prompt(app.nav, "?") + if len(s) == 0 { + return + } log.Printf("search-back: %s", s) app.ui.message = "sorry, search-back is not implemented yet!" // TODO: implement @@ -288,7 +302,7 @@ func (e *CallExpr) eval(app *App, args []string) { log.Print(err) return } - app.ui.echoFileInfo(app.nav) + app.ui.loadFile(app.nav) default: cmd, ok := gOpts.cmds[e.name] if !ok { @@ -307,7 +321,7 @@ func (e *ExecExpr) eval(app *App, args []string) { log.Printf("shell: %s -- %s", e, args) app.ui.clearMsg() app.runShell(e.expr, args, false, false) - app.ui.echoFileInfo(app.nav) + app.ui.loadFile(app.nav) case "!": log.Printf("shell-wait: %s -- %s", e, args) app.runShell(e.expr, args, true, false) diff --git a/ui.go b/ui.go index e7a5123..56fd346 100644 --- a/ui.go +++ b/ui.go @@ -235,55 +235,14 @@ 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 - } - +func (win *Win) printr(reg []string) { fg, bg := termbox.ColorDefault, termbox.ColorDefault - buf := bufio.NewScanner(reader) - - for i := 0; i < win.h && buf.Scan(); i++ { - for _, r := range buf.Text() { - if unicode.IsSpace(r) { - continue - } - if !unicode.IsPrint(r) && r != EscapeCode { - fg = termbox.AttrBold - win.print(0, 0, fg, bg, "binary") - return nil - } - } + for i, l := range reg { + win.print(2, i, fg, bg, l) } - if buf.Err() != nil { - return fmt.Errorf("printing regular file: %s", buf.Err()) - } - - 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()) - } - - if buf.Err() != nil { - return fmt.Errorf("printing regular file: %s", buf.Err()) - } - - return nil + return } type UI struct { @@ -292,6 +251,8 @@ type UI struct { msgwin *Win menuwin *Win message string + regprev []string + dirprev *Dir } func getWidths(wtot int) []int { @@ -352,7 +313,7 @@ func (ui *UI) renew() { ui.msgwin.renew(wtot, 1, 0, htot-1) } -func (ui *UI) echoFileInfo(nav *Nav) { +func (ui *UI) loadFile(nav *Nav) { dir := nav.currDir() if len(dir.fi) == 0 { @@ -362,6 +323,65 @@ func (ui *UI) echoFileInfo(nav *Nav) { curr := nav.currFile() ui.message = fmt.Sprintf("%v %v %v", curr.Mode(), humanize(curr.Size()), curr.ModTime().Format(time.ANSIC)) + + if !gOpts.preview { + return + } + + path := nav.currPath() + + if curr.IsDir() { + dir := newDir(path) + dir.load(nav.inds[path], nav.poss[path], nav.height, nav.names[path]) + ui.dirprev = dir + } else if curr.Mode().IsRegular() { + var reader io.Reader + + if len(gOpts.previewer) != 0 { + cmd := exec.Command(gOpts.previewer, path, strconv.Itoa(nav.height)) + + out, err := cmd.Output() + if err != nil { + msg := fmt.Sprintf("previewing file: %s", err) + ui.message = msg + log.Print(msg) + } + + reader = bytes.NewReader(out) + } else { + f, err := os.Open(path) + if err != nil { + msg := fmt.Sprintf("opening file: %s", err) + ui.message = msg + log.Print(msg) + } + + reader = f + } + + ui.regprev = nil + + buf := bufio.NewScanner(reader) + + for i := 0; i < nav.height && buf.Scan(); i++ { + for _, r := range buf.Text() { + if unicode.IsSpace(r) { + continue + } + if !unicode.IsPrint(r) && r != EscapeCode { + ui.regprev = []string{"binary"} + return + } + } + ui.regprev = append(ui.regprev, buf.Text()) + } + + if buf.Err() != nil { + msg := fmt.Sprintf("loading file: %s", buf.Err()) + ui.message = msg + log.Print(msg) + } + } } func (ui *UI) clearMsg() { @@ -418,21 +438,9 @@ func (ui *UI) draw(nav *Nav) { } if f.IsDir() { - dir := newDir(path) - dir.load(nav.inds[path], nav.poss[path], nav.height, nav.names[path]) - preview.printd(dir, nav.marks) + preview.printd(ui.dirprev, nav.marks) } else if f.Mode().IsRegular() { - file, err := os.Open(path) - if err != nil { - msg := fmt.Sprintf("opening file: %s", err) - ui.message = msg - log.Print(msg) - } - - if err := preview.printr(file); err != nil { - ui.message = err.Error() - log.Print(err) - } + preview.printr(ui.regprev) } } }