Skip to content

Simpler implementation of dirpreviews #1958

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -336,7 +335,6 @@ func (app *app) loop() {
app.quit()

app.nav.previewChan <- ""
app.nav.dirPreviewChan <- nil

log.Print("bye!")

Expand Down Expand Up @@ -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)
Expand Down
88 changes: 0 additions & 88 deletions nav.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -480,8 +477,6 @@ type nav struct {
}

func (nav *nav) loadDirInternal(path string) *dir {
nav.startPreview()

d := &dir{
loading: true,
loadTime: time.Now(),
Expand All @@ -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
Expand Down Expand Up @@ -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) ||
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 := &reg{loadTime: time.Now(), path: path}
defer func() { nav.regChan <- reg }()
Expand Down
41 changes: 14 additions & 27 deletions ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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})
}
}

Expand Down Expand Up @@ -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})
}
}
}
Expand Down