Simpler implementation of dirpreviews (#1958)
This commit is contained in:
1 parent
3d5a418c6c
commit
0df0c0f5ff
3 files changed
+14
-118
No files matched your search
@@ -258,7 +258,6 @@ func (app *app) writeHistory() error {
|
|||||||
// separate goroutines and sent here for update.
|
// separate goroutines and sent here for update.
|
||||||
func (app *app) loop() {
|
func (app *app) loop() {
|
||||||
go app.nav.previewLoop(app.ui)
|
go app.nav.previewLoop(app.ui)
|
||||||
go app.nav.dirPreviewLoop(app.ui)
|
|
||||||
|
|
||||||
var serverChan <-chan expr
|
var serverChan <-chan expr
|
||||||
if !gSingleMode {
|
if !gSingleMode {
|
||||||
@@ -336,7 +335,6 @@ func (app *app) loop() {
|
|||||||
app.quit()
|
app.quit()
|
||||||
|
|
||||||
app.nav.previewChan <- ""
|
app.nav.previewChan <- ""
|
||||||
app.nav.dirPreviewChan <- nil
|
|
||||||
|
|
||||||
log.Print("bye!")
|
log.Print("bye!")
|
||||||
|
|
||||||
@@ -510,7 +508,6 @@ func (app *app) loop() {
|
|||||||
|
|
||||||
func (app *app) runCmdSync(cmd *exec.Cmd, pause_after bool) {
|
func (app *app) runCmdSync(cmd *exec.Cmd, pause_after bool) {
|
||||||
app.nav.previewChan <- ""
|
app.nav.previewChan <- ""
|
||||||
app.nav.dirPreviewChan <- nil
|
|
||||||
|
|
||||||
if err := app.ui.suspend(); err != nil {
|
if err := app.ui.suspend(); err != nil {
|
||||||
log.Printf("suspend: %s", err)
|
log.Printf("suspend: %s", err)
|
||||||
|
|||||||
@@ -175,7 +175,6 @@ type dir struct {
|
|||||||
ignoredia bool // ignoredia value from last sort
|
ignoredia bool // ignoredia value from last sort
|
||||||
locale string // locale value from last sort
|
locale string // locale value from last sort
|
||||||
noPerm bool // whether lf has no permission to open the directory
|
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 {
|
func newDir(path string) *dir {
|
||||||
@@ -187,7 +186,6 @@ func newDir(path string) *dir {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return &dir{
|
return &dir{
|
||||||
loading: gOpts.dirpreviews, // Directory is loaded after previewer function exits.
|
|
||||||
loadTime: time,
|
loadTime: time,
|
||||||
path: path,
|
path: path,
|
||||||
files: files,
|
files: files,
|
||||||
@@ -450,7 +448,6 @@ type nav struct {
|
|||||||
deleteCountChan chan int
|
deleteCountChan chan int
|
||||||
deleteTotalChan chan int
|
deleteTotalChan chan int
|
||||||
previewChan chan string
|
previewChan chan string
|
||||||
dirPreviewChan chan *dir
|
|
||||||
dirChan chan *dir
|
dirChan chan *dir
|
||||||
regChan chan *reg
|
regChan chan *reg
|
||||||
fileChan chan *file
|
fileChan chan *file
|
||||||
@@ -480,8 +477,6 @@ type nav struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (nav *nav) loadDirInternal(path string) *dir {
|
func (nav *nav) loadDirInternal(path string) *dir {
|
||||||
nav.startPreview()
|
|
||||||
|
|
||||||
d := &dir{
|
d := &dir{
|
||||||
loading: true,
|
loading: true,
|
||||||
loadTime: time.Now(),
|
loadTime: time.Now(),
|
||||||
@@ -500,9 +495,6 @@ func (nav *nav) loadDirInternal(path string) *dir {
|
|||||||
d := newDir(path)
|
d := newDir(path)
|
||||||
d.sort()
|
d.sort()
|
||||||
d.ind, d.pos = 0, 0
|
d.ind, d.pos = 0, 0
|
||||||
if gOpts.dirpreviews {
|
|
||||||
nav.dirPreviewChan <- d
|
|
||||||
}
|
|
||||||
nav.dirChan <- d
|
nav.dirChan <- d
|
||||||
}()
|
}()
|
||||||
return d
|
return d
|
||||||
@@ -541,14 +533,10 @@ func (nav *nav) checkDir(dir *dir) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
nav.startPreview()
|
|
||||||
dir.loading = true
|
dir.loading = true
|
||||||
dir.loadTime = now
|
dir.loadTime = now
|
||||||
go func() {
|
go func() {
|
||||||
nd := newDir(dir.path)
|
nd := newDir(dir.path)
|
||||||
if gOpts.dirpreviews {
|
|
||||||
nav.dirPreviewChan <- nd
|
|
||||||
}
|
|
||||||
nav.dirChan <- nd
|
nav.dirChan <- nd
|
||||||
}()
|
}()
|
||||||
case dir.sortby != getSortBy(dir.path) ||
|
case dir.sortby != getSortBy(dir.path) ||
|
||||||
@@ -599,7 +587,6 @@ func newNav(height int) *nav {
|
|||||||
deleteCountChan: make(chan int, 1024),
|
deleteCountChan: make(chan int, 1024),
|
||||||
deleteTotalChan: make(chan int, 1024),
|
deleteTotalChan: make(chan int, 1024),
|
||||||
previewChan: make(chan string, 1024),
|
previewChan: make(chan string, 1024),
|
||||||
dirPreviewChan: make(chan *dir, 1024),
|
|
||||||
dirChan: make(chan *dir),
|
dirChan: make(chan *dir),
|
||||||
regChan: make(chan *reg),
|
regChan: make(chan *reg),
|
||||||
fileChan: make(chan *file),
|
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) {
|
func (nav *nav) previewLoop(ui *ui) {
|
||||||
var prev string
|
var prev string
|
||||||
for path := range nav.previewChan {
|
for path := range nav.previewChan {
|
||||||
@@ -783,64 +753,6 @@ func matchPattern(pattern, name, path string) bool {
|
|||||||
return matched
|
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) {
|
func (nav *nav) preview(path string, win *win) {
|
||||||
reg := ®{loadTime: time.Now(), path: path}
|
reg := ®{loadTime: time.Now(), path: path}
|
||||||
defer func() { nav.regChan <- reg }()
|
defer func() { nav.regChan <- reg }()
|
||||||
|
|||||||
@@ -356,7 +356,7 @@ type dirStyle struct {
|
|||||||
role dirRole
|
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 {
|
if win.w < 5 || dir == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -368,25 +368,11 @@ func (win *win) printDir(ui *ui, dir *dir, context *dirContext, dirStyle *dirSty
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
fileslen := len(dir.files)
|
fileslen := len(dir.files)
|
||||||
if (dir.loading && fileslen == 0) || (dirStyle.role == Preview && dir.loading && gOpts.dirpreviews) {
|
if dir.loading && fileslen == 0 {
|
||||||
if dirStyle.role != Preview || previewLoading {
|
win.print(ui.screen, 2, 0, messageStyle, "loading...")
|
||||||
win.print(ui.screen, 2, 0, messageStyle, "loading...")
|
|
||||||
}
|
|
||||||
return
|
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 {
|
if fileslen == 0 {
|
||||||
win.print(ui.screen, 2, 0, messageStyle, "empty")
|
win.print(ui.screen, 2, 0, messageStyle, "empty")
|
||||||
return
|
return
|
||||||
@@ -735,6 +721,9 @@ func (ui *ui) echoerrf(format string, a ...any) {
|
|||||||
ui.echoerr(fmt.Sprintf(format, a...))
|
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 {
|
type reg struct {
|
||||||
loading bool
|
loading bool
|
||||||
volatile bool
|
volatile bool
|
||||||
@@ -767,10 +756,10 @@ func (ui *ui) loadFile(app *app, volatile bool) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if curr.IsDir() {
|
if curr.Mode().IsRegular() || (curr.IsDir() && gOpts.dirpreviews) {
|
||||||
ui.dirPrev = app.nav.loadDir(curr.path)
|
|
||||||
} else if curr.Mode().IsRegular() {
|
|
||||||
ui.regPrev = app.nav.loadReg(curr.path, volatile)
|
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 {
|
if dir := ui.dirOfWin(nav, i); dir != nil {
|
||||||
ui.wins[i].printDir(ui, dir, &context,
|
ui.wins[i].printDir(ui, dir, &context,
|
||||||
&dirStyle{colors: ui.styles, icons: ui.icons, role: role},
|
&dirStyle{colors: ui.styles, icons: ui.icons, role: role})
|
||||||
nav.previewLoading)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1075,12 +1063,11 @@ func (ui *ui) draw(nav *nav) {
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
preview := ui.wins[len(ui.wins)-1]
|
preview := ui.wins[len(ui.wins)-1]
|
||||||
|
|
||||||
if curr.IsDir() {
|
if curr.Mode().IsRegular() || (curr.IsDir() && gOpts.dirpreviews) {
|
||||||
preview.printDir(ui, ui.dirPrev, &context,
|
|
||||||
&dirStyle{colors: ui.styles, icons: ui.icons, role: Preview},
|
|
||||||
nav.previewLoading)
|
|
||||||
} else if curr.Mode().IsRegular() {
|
|
||||||
preview.printReg(ui.screen, ui.regPrev, nav.previewLoading, &ui.sxScreen)
|
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})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user