Guarantee preview/previewClear ordering (#537)

* Use chan struct{} in place of chan bool

* Revert "nav.checkReg now returns a boolean instead of calling nav.preview"

* Guarantee preview/previewClear ordering

* Pass the file path to the cleaner script
This commit is contained in:
neeshy authored and GitHub committed 2020-12-27 04:23:03 +03:00
1 parent 72567c9237
commit 1e94f4533e
9 files changed
+48 -41

No files matched your search

+5 -8
View File
@@ -25,7 +25,7 @@ type app struct {
ui *ui ui *ui
nav *nav nav *nav
ticker *time.Ticker ticker *time.Ticker
quitChan chan bool quitChan chan struct{}
cmd *exec.Cmd cmd *exec.Cmd
cmdIn io.WriteCloser cmdIn io.WriteCloser
cmdOutBuf []byte cmdOutBuf []byte
@@ -38,7 +38,7 @@ func newApp(screen tcell.Screen) *app {
ui := newUI(screen) ui := newUI(screen)
nav := newNav(ui.wins[0].h) nav := newNav(ui.wins[0].h)
quitChan := make(chan bool, 1) quitChan := make(chan struct{}, 1)
app := &app{ app := &app{
ui: ui, ui: ui,
@@ -213,7 +213,7 @@ func (app *app) loop() {
continue continue
} }
go app.nav.previewClear() app.nav.previewChan <- ""
log.Print("bye!") log.Print("bye!")
@@ -312,10 +312,7 @@ func (app *app) loop() {
app.ui.draw(app.nav) app.ui.draw(app.nav)
case r := <-app.nav.regChan: case r := <-app.nav.regChan:
if app.nav.checkReg(r) { app.nav.checkReg(r)
win := app.ui.wins[len(app.ui.wins)-1]
go app.nav.preview(r.path, win)
}
app.nav.regCache[r.path] = r app.nav.regCache[r.path] = r
@@ -407,7 +404,7 @@ func (app *app) runShell(s string, args []string, prefix string) {
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
go app.nav.previewClear() app.nav.previewChan <- ""
app.ui.pause() app.ui.pause()
defer app.ui.resume() defer app.ui.resume()
defer app.nav.renew() defer app.nav.renew()
+1
View File
@@ -42,6 +42,7 @@ func run() {
app.ui.echoerrf("reading history file: %s", err) app.ui.echoerrf("reading history file: %s", err)
} }
go app.nav.previewLoop(app.ui)
app.loop() app.loop()
app.ui.screen.Fini() app.ui.screen.Fini()
} }
+1
View File
@@ -601,6 +601,7 @@ Preview filtering is disabled and files are displayed as they are when the value
Set the path of a cleaner file. This file will be called if previewing is enabled, the previewer is set, and the previously selected file had its preview cache disabled. Set the path of a cleaner file. This file will be called if previewing is enabled, the previewer is set, and the previously selected file had its preview cache disabled.
The file should be executable. The file should be executable.
One argument is passed to the file; the path to the file whose preview should be cleaned.
Preview clearing is disabled when the value of this option is left empty. Preview clearing is disabled when the value of this option is left empty.
promptfmt string (default "\033[32;1m%u@%h\033[0m:\033[34;1m%w/\033[0m\033[1m%f\033[0m") promptfmt string (default "\033[32;1m%u@%h\033[0m:\033[34;1m%w/\033[0m\033[1m%f\033[0m")
+3 -2
View File
@@ -638,8 +638,9 @@ displayed as they are when the value of this option is left empty.
Set the path of a cleaner file. This file will be called if previewing is Set the path of a cleaner file. This file will be called if previewing is
enabled, the previewer is set, and the previously selected file had its enabled, the previewer is set, and the previously selected file had its
preview cache disabled. The file should be executable. Preview clearing is preview cache disabled. The file should be executable. One argument is
disabled when the value of this option is left empty. passed to the file; the path to the file whose preview should be cleaned.
Preview clearing is disabled when the value of this option is left empty.
promptfmt string (default "\033[32;1m%u@%h\033[0m:\033[34;1m%w/\033[0m\033[1m%f\033[0m") promptfmt string (default "\033[32;1m%u@%h\033[0m:\033[34;1m%w/\033[0m\033[1m%f\033[0m")
+2 -2
View File
@@ -699,7 +699,7 @@ func (e *callExpr) eval(app *app, args []string) {
log.Printf("writing selection file: %s", err) log.Printf("writing selection file: %s", err)
} }
app.quitChan <- true app.quitChan <- struct{}{}
return return
} }
@@ -708,7 +708,7 @@ func (e *callExpr) eval(app *app, args []string) {
cmd.eval(app, e.args) cmd.eval(app, e.args)
} }
case "quit": case "quit":
app.quitChan <- true app.quitChan <- struct{}{}
case "top": case "top":
app.nav.top() app.nav.top()
app.ui.loadFile(app.nav) app.ui.loadFile(app.nav)
+1 -1
View File
@@ -719,7 +719,7 @@ Set the path of a previewer file to filter the content of regular files for prev
cleaner string (default '') (not called if empty) cleaner string (default '') (not called if empty)
.EE .EE
.PP .PP
Set the path of a cleaner file. This file will be called if previewing is enabled, the previewer is set, and the previously selected file had its preview cache disabled. The file should be executable. Preview clearing is disabled when the value of this option is left empty. Set the path of a cleaner file. This file will be called if previewing is enabled, the previewer is set, and the previously selected file had its preview cache disabled. The file should be executable. One argument is passed to the file; the path to the file whose preview should be cleaned. Preview clearing is disabled when the value of this option is left empty.
.PP .PP
.EX .EX
promptfmt string (default "\e033[32;1m%u@%h\e033[0m:\e033[34;1m%w/\e033[0m\e033[1m%f\e033[0m") promptfmt string (default "\e033[32;1m%u@%h\e033[0m:\e033[34;1m%w/\e033[0m\e033[1m%f\e033[0m")
+31 -23
View File
@@ -285,6 +285,7 @@ type nav struct {
moveTotalChan chan int moveTotalChan chan int
deleteCountChan chan int deleteCountChan chan int
deleteTotalChan chan int deleteTotalChan chan int
previewChan chan string
dirChan chan *dir dirChan chan *dir
regChan chan *reg regChan chan *reg
dirCache map[string]*dir dirCache map[string]*dir
@@ -398,6 +399,7 @@ func newNav(height int) *nav {
moveTotalChan: make(chan int, 1024), moveTotalChan: make(chan int, 1024),
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),
dirChan: make(chan *dir), dirChan: make(chan *dir),
regChan: make(chan *reg), regChan: make(chan *reg),
dirCache: make(map[string]*dir), dirCache: make(map[string]*dir),
@@ -457,6 +459,27 @@ func (nav *nav) position() {
} }
} }
func (nav *nav) previewLoop(ui *ui) {
var path string
for {
p, ok := <-nav.previewChan
if !ok {
return
}
if len(p) != 0 {
win := ui.wins[len(ui.wins)-1]
nav.preview(p, win)
path = p
} else if len(gOpts.previewer) != 0 && len(gOpts.cleaner) != 0 && nav.volatilePreview {
cmd := exec.Command(gOpts.cleaner, path)
if err := cmd.Run(); err != nil {
log.Printf("cleaning preview: %s", err)
}
nav.volatilePreview = false
}
}
}
func (nav *nav) preview(path string, win *win) { func (nav *nav) preview(path string, win *win) {
reg := &reg{loadTime: time.Now(), path: path} reg := &reg{loadTime: time.Now(), path: path}
defer func() { nav.regChan <- reg }() defer func() { nav.regChan <- reg }()
@@ -487,8 +510,8 @@ func (nav *nav) preview(path string, win *win) {
if err := cmd.Wait(); err != nil { if err := cmd.Wait(); err != nil {
if e, ok := err.(*exec.ExitError); ok { if e, ok := err.(*exec.ExitError); ok {
if e.ExitCode() != 0 { if e.ExitCode() != 0 {
nav.volatilePreview = true
reg.volatile = true reg.volatile = true
nav.volatilePreview = true
} }
} else { } else {
log.Printf("loading file: %s", err) log.Printf("loading file: %s", err)
@@ -525,37 +548,24 @@ func (nav *nav) preview(path string, win *win) {
} }
} }
func (nav *nav) previewClear() { func (nav *nav) loadReg(path string) *reg {
if len(gOpts.cleaner) != 0 && nav.volatilePreview {
nav.volatilePreview = false
cmd := exec.Command(gOpts.cleaner)
if err := cmd.Run(); err != nil {
log.Printf("cleaning preview: %s", err)
}
}
}
func (nav *nav) loadReg(path string, win *win) *reg {
r, ok := nav.regCache[path] r, ok := nav.regCache[path]
if !ok || r.volatile { if !ok || r.volatile {
r := &reg{loading: true, loadTime: time.Now(), path: path, volatile: true} r := &reg{loading: true, loadTime: time.Now(), path: path, volatile: true}
nav.regCache[path] = r nav.regCache[path] = r
go nav.preview(path, win) nav.previewChan <- path
return r return r
} }
if nav.checkReg(r) { nav.checkReg(r)
go nav.preview(path, win)
}
return r return r
} }
func (nav *nav) checkReg(reg *reg) bool { func (nav *nav) checkReg(reg *reg) {
s, err := os.Stat(reg.path) s, err := os.Stat(reg.path)
if err != nil { if err != nil {
return false return
} }
now := time.Now() now := time.Now()
@@ -563,15 +573,13 @@ func (nav *nav) checkReg(reg *reg) bool {
// XXX: Linux builtin exFAT drivers are able to predict modifications in the future // XXX: Linux builtin exFAT drivers are able to predict modifications in the future
// https://bugs.launchpad.net/ubuntu/+source/ubuntu-meta/+bug/1872504 // https://bugs.launchpad.net/ubuntu/+source/ubuntu-meta/+bug/1872504
if s.ModTime().After(now) { if s.ModTime().After(now) {
return false return
} }
if s.ModTime().After(reg.loadTime) { if s.ModTime().After(reg.loadTime) {
reg.loadTime = now reg.loadTime = now
return true nav.previewChan <- reg.path
} }
return false
} }
func (nav *nav) sort() { func (nav *nav) sort() {
+2 -2
View File
@@ -13,7 +13,7 @@ var (
gCopyFile bool gCopyFile bool
gFileList []string gFileList []string
gConnList = make(map[int]net.Conn) gConnList = make(map[int]net.Conn)
gQuitChan = make(chan bool, 1) gQuitChan = make(chan struct{}, 1)
gListener net.Listener gListener net.Listener
) )
@@ -115,7 +115,7 @@ Loop:
} }
} }
case "quit": case "quit":
gQuitChan <- true gQuitChan <- struct{}{}
for _, c := range gConnList { for _, c := range gConnList {
fmt.Fprintln(c, "echo server is quitting...") fmt.Fprintln(c, "echo server is quitting...")
c.Close() c.Close()
+2 -3
View File
@@ -625,12 +625,11 @@ func (ui *ui) loadFile(nav *nav) {
return return
} }
go nav.previewClear() nav.previewChan <- ""
if curr.IsDir() { if curr.IsDir() {
ui.dirPrev = nav.loadDir(curr.path) ui.dirPrev = nav.loadDir(curr.path)
} else if curr.Mode().IsRegular() { } else if curr.Mode().IsRegular() {
win := ui.wins[len(ui.wins)-1] ui.regPrev = nav.loadReg(curr.path)
ui.regPrev = nav.loadReg(curr.path, win)
} }
} }