diff --git a/app.go b/app.go index dfe1c7ba..e03c07e5 100644 --- a/app.go +++ b/app.go @@ -258,7 +258,6 @@ func (app *app) writeHistory() error { // separate goroutines and sent here for update. func (app *app) loop() { go app.nav.previewLoop(app.ui) - go app.nav.dirPreviewLoop(app.ui) var serverChan <-chan expr if !gSingleMode { @@ -336,7 +335,6 @@ func (app *app) loop() { app.quit() app.nav.previewChan <- "" - app.nav.dirPreviewChan <- nil log.Print("bye!") @@ -510,7 +508,6 @@ func (app *app) loop() { func (app *app) runCmdSync(cmd *exec.Cmd, pause_after bool) { app.nav.previewChan <- "" - app.nav.dirPreviewChan <- nil if err := app.ui.suspend(); err != nil { log.Printf("suspend: %s", err) diff --git a/nav.go b/nav.go index 0935ff0c..d2753290 100644 --- a/nav.go +++ b/nav.go @@ -175,7 +175,6 @@ type dir struct { ignoredia bool // ignoredia value from last sort locale string // locale value from last sort noPerm bool // whether lf has no permission to open the directory - lines []string // lines of text to display if directory previews are enabled } func newDir(path string) *dir { @@ -187,7 +186,6 @@ func newDir(path string) *dir { } return &dir{ - loading: gOpts.dirpreviews, // Directory is loaded after previewer function exits. loadTime: time, path: path, files: files, @@ -450,7 +448,6 @@ type nav struct { deleteCountChan chan int deleteTotalChan chan int previewChan chan string - dirPreviewChan chan *dir dirChan chan *dir regChan chan *reg fileChan chan *file @@ -480,8 +477,6 @@ type nav struct { } func (nav *nav) loadDirInternal(path string) *dir { - nav.startPreview() - d := &dir{ loading: true, loadTime: time.Now(), @@ -500,9 +495,6 @@ func (nav *nav) loadDirInternal(path string) *dir { d := newDir(path) d.sort() d.ind, d.pos = 0, 0 - if gOpts.dirpreviews { - nav.dirPreviewChan <- d - } nav.dirChan <- d }() return d @@ -541,14 +533,10 @@ func (nav *nav) checkDir(dir *dir) { return } - nav.startPreview() dir.loading = true dir.loadTime = now go func() { nd := newDir(dir.path) - if gOpts.dirpreviews { - nav.dirPreviewChan <- nd - } nav.dirChan <- nd }() case dir.sortby != getSortBy(dir.path) || @@ -599,7 +587,6 @@ func newNav(height int) *nav { deleteCountChan: make(chan int, 1024), deleteTotalChan: make(chan int, 1024), previewChan: make(chan string, 1024), - dirPreviewChan: make(chan *dir, 1024), dirChan: make(chan *dir), regChan: make(chan *reg), fileChan: make(chan *file), @@ -718,23 +705,6 @@ func (nav *nav) exportFiles() { } } -func (nav *nav) dirPreviewLoop(ui *ui) { - var prevPath string - for dir := range nav.dirPreviewChan { - if dir == nil && len(gOpts.previewer) != 0 && len(gOpts.cleaner) != 0 && nav.volatilePreview { - cmd := exec.Command(gOpts.cleaner, prevPath) - if err := cmd.Run(); err != nil { - log.Printf("cleaning preview: %s", err) - } - nav.volatilePreview = false - } else if dir != nil { - win := ui.wins[len(ui.wins)-1] - nav.previewDir(dir, win) - prevPath = dir.path - } - } -} - func (nav *nav) previewLoop(ui *ui) { var prev string for path := range nav.previewChan { @@ -783,64 +753,6 @@ func matchPattern(pattern, name, path string) bool { return matched } -func (nav *nav) previewDir(dir *dir, win *win) { - defer func() { - dir.loading = false - nav.dirChan <- dir - }() - - var reader io.Reader - - if len(gOpts.previewer) != 0 { - cmd := exec.Command(gOpts.previewer, dir.path, - strconv.Itoa(win.w), - strconv.Itoa(win.h), - strconv.Itoa(win.x), - strconv.Itoa(win.y)) - - out, err := cmd.StdoutPipe() - if err != nil { - log.Printf("previewing dir: %s", err) - return - } - - if err := cmd.Start(); err != nil { - log.Printf("previewing dir: %s", err) - out.Close() - return - } - - defer func() { - if err := cmd.Wait(); err != nil { - if e, ok := err.(*exec.ExitError); ok { - if e.ExitCode() != 0 { - nav.volatilePreview = true - } - } else { - log.Printf("loading dir: %s", err) - } - } - }() - defer out.Close() - reader = out - buf := bufio.NewScanner(reader) - - for i := 0; i < win.h && buf.Scan(); i++ { - for _, r := range buf.Text() { - if r == 0 { - dir.lines = []string{"\033[7mbinary\033[0m"} - return - } - } - dir.lines = append(dir.lines, buf.Text()) - } - - if buf.Err() != nil { - log.Printf("loading dir: %s", buf.Err()) - } - } -} - func (nav *nav) preview(path string, win *win) { reg := ®{loadTime: time.Now(), path: path} defer func() { nav.regChan <- reg }() diff --git a/ui.go b/ui.go index 523f7516..654e1d2a 100644 --- a/ui.go +++ b/ui.go @@ -356,7 +356,7 @@ type dirStyle struct { role dirRole } -func (win *win) printDir(ui *ui, dir *dir, context *dirContext, dirStyle *dirStyle, previewLoading bool) { +func (win *win) printDir(ui *ui, dir *dir, context *dirContext, dirStyle *dirStyle) { if win.w < 5 || dir == nil { return } @@ -368,25 +368,11 @@ func (win *win) printDir(ui *ui, dir *dir, context *dirContext, dirStyle *dirSty return } fileslen := len(dir.files) - if (dir.loading && fileslen == 0) || (dirStyle.role == Preview && dir.loading && gOpts.dirpreviews) { - if dirStyle.role != Preview || previewLoading { - win.print(ui.screen, 2, 0, messageStyle, "loading...") - } + if dir.loading && fileslen == 0 { + win.print(ui.screen, 2, 0, messageStyle, "loading...") return } - if dirStyle.role == Preview && gOpts.dirpreviews && len(gOpts.previewer) > 0 { - // Print previewer result instead of default directory print operation. - st := tcell.StyleDefault - for i, l := range dir.lines { - if i > win.h-1 { - break - } - - st = win.print(ui.screen, 2, i, st, l) - } - return - } if fileslen == 0 { win.print(ui.screen, 2, 0, messageStyle, "empty") return @@ -735,6 +721,9 @@ func (ui *ui) echoerrf(format string, a ...any) { ui.echoerr(fmt.Sprintf(format, a...)) } +// This represents the preview for a regular file. +// This can also be used to represent the preview of a directory if +// `dirpreviews` is enabled. type reg struct { loading bool volatile bool @@ -767,10 +756,10 @@ func (ui *ui) loadFile(app *app, volatile bool) { return } - if curr.IsDir() { - ui.dirPrev = app.nav.loadDir(curr.path) - } else if curr.Mode().IsRegular() { + if curr.Mode().IsRegular() || (curr.IsDir() && gOpts.dirpreviews) { ui.regPrev = app.nav.loadReg(curr.path, volatile) + } else if curr.IsDir() { + ui.dirPrev = app.nav.loadDir(curr.path) } } @@ -1046,8 +1035,7 @@ func (ui *ui) draw(nav *nav) { } if dir := ui.dirOfWin(nav, i); dir != nil { ui.wins[i].printDir(ui, dir, &context, - &dirStyle{colors: ui.styles, icons: ui.icons, role: role}, - nav.previewLoading) + &dirStyle{colors: ui.styles, icons: ui.icons, role: role}) } } @@ -1075,12 +1063,11 @@ func (ui *ui) draw(nav *nav) { if err == nil { preview := ui.wins[len(ui.wins)-1] - if curr.IsDir() { - preview.printDir(ui, ui.dirPrev, &context, - &dirStyle{colors: ui.styles, icons: ui.icons, role: Preview}, - nav.previewLoading) - } else if curr.Mode().IsRegular() { + if curr.Mode().IsRegular() || (curr.IsDir() && gOpts.dirpreviews) { preview.printReg(ui.screen, ui.regPrev, nav.previewLoading, &ui.sxScreen) + } else if curr.IsDir() { + preview.printDir(ui, ui.dirPrev, &context, + &dirStyle{colors: ui.styles, icons: ui.icons, role: Preview}) } } }