check modtime/sorting again when dirs are updated

Related #338
This commit is contained in:
Gokcehan 2020-07-16 23:13:40 +03:00
parent f2e3c7a0ce
commit 3e36500901
3 changed files with 50 additions and 56 deletions

4
app.go
View File

@ -268,6 +268,8 @@ func (app *app) loop() {
} }
app.ui.draw(app.nav) app.ui.draw(app.nav)
case d := <-app.nav.dirChan: case d := <-app.nav.dirChan:
app.nav.checkDir(d)
prev, ok := app.nav.dirCache[d.path] prev, ok := app.nav.dirCache[d.path]
if ok { if ok {
d.ind = prev.ind d.ind = prev.ind
@ -296,6 +298,8 @@ func (app *app) loop() {
app.ui.draw(app.nav) app.ui.draw(app.nav)
case r := <-app.nav.regChan: case r := <-app.nav.regChan:
app.nav.checkReg(r)
app.nav.regCache[r.path] = r app.nav.regCache[r.path] = r
curr, err := app.nav.currFile() curr, err := app.nav.currFile()

72
nav.go
View File

@ -286,43 +286,43 @@ type nav struct {
func (nav *nav) loadDir(path string) *dir { func (nav *nav) loadDir(path string) *dir {
d, ok := nav.dirCache[path] d, ok := nav.dirCache[path]
if !ok { if !ok {
d := &dir{loading: true, loadTime: time.Now(), path: path, sortType: gOpts.sortType}
nav.dirCache[path] = d
go func() { go func() {
d := newDir(path) d := newDir(path)
d.sort() d.sort()
d.ind, d.pos = 0, 0 d.ind, d.pos = 0, 0
nav.dirChan <- d nav.dirChan <- d
}() }()
d := &dir{loading: true, path: path, sortType: gOpts.sortType}
nav.dirCache[path] = d
return d return d
} }
s, err := os.Stat(d.path)
if err != nil {
return d return d
} }
func (nav *nav) checkDir(dir *dir) {
s, err := os.Stat(dir.path)
if err != nil {
log.Printf("getting directory info: %s", err)
return
}
switch { switch {
case s.ModTime().After(d.loadTime): case s.ModTime().After(dir.loadTime):
go func() { go func() {
d.loadTime = time.Now() dir.loadTime = time.Now()
nd := newDir(path) nd := newDir(dir.path)
nd.sort() nd.sort()
nd.sel(d.name(), nav.height)
nav.dirChan <- nd nav.dirChan <- nd
}() }()
case d.sortType != gOpts.sortType: case dir.sortType != gOpts.sortType:
go func() { go func() {
d.loading = true dir.loading = true
name := d.name() dir.sort()
d.sort() dir.loading = false
d.sel(name, nav.height) nav.dirChan <- dir
d.loading = false
nav.dirChan <- d
}() }()
} }
return d
} }
func (nav *nav) getDirs(wd string) { func (nav *nav) getDirs(wd string) {
@ -372,20 +372,7 @@ func newNav(height int) *nav {
func (nav *nav) renew() { func (nav *nav) renew() {
for _, d := range nav.dirs { for _, d := range nav.dirs {
go func(d *dir) { nav.checkDir(d)
s, err := os.Stat(d.path)
if err != nil {
log.Printf("getting directory info: %s", err)
return
}
if d.loadTime.After(s.ModTime()) {
return
}
d.loadTime = time.Now()
nd := newDir(d.path)
nd.sort()
nav.dirChan <- nd
}(d)
} }
for m := range nav.selections { for m := range nav.selections {
@ -393,6 +380,7 @@ func (nav *nav) renew() {
delete(nav.selections, m) delete(nav.selections, m)
} }
} }
if len(nav.selections) == 0 { if len(nav.selections) == 0 {
nav.selectionInd = 0 nav.selectionInd = 0
} }
@ -431,6 +419,8 @@ func (nav *nav) preview() {
return return
} }
reg := &reg{loadTime: time.Now(), path: curr.path}
var reader io.Reader var reader io.Reader
if len(gOpts.previewer) != 0 { if len(gOpts.previewer) != 0 {
@ -458,8 +448,6 @@ func (nav *nav) preview() {
reader = f reader = f
} }
reg := &reg{loadTime: time.Now(), path: curr.path}
buf := bufio.NewScanner(reader) buf := bufio.NewScanner(reader)
for i := 0; i < nav.height && buf.Scan(); i++ { for i := 0; i < nav.height && buf.Scan(); i++ {
@ -483,23 +471,25 @@ func (nav *nav) preview() {
func (nav *nav) loadReg(path string) *reg { func (nav *nav) loadReg(path string) *reg {
r, ok := nav.regCache[path] r, ok := nav.regCache[path]
if !ok { if !ok {
go nav.preview() r := &reg{loading: true, loadTime: time.Now(), path: path}
r := &reg{loading: true, path: path}
nav.regCache[path] = r nav.regCache[path] = r
go nav.preview()
return r return r
} }
s, err := os.Stat(r.path) return r
}
func (nav *nav) checkReg(reg *reg) {
s, err := os.Stat(reg.path)
if err != nil { if err != nil {
return r return
} }
if s.ModTime().After(r.loadTime) { if s.ModTime().After(reg.loadTime) {
r.loadTime = time.Now() reg.loadTime = time.Now()
go nav.preview() go nav.preview()
} }
return r
} }
func (nav *nav) sort() { func (nav *nav) sort() {

2
ui.go
View File

@ -477,7 +477,7 @@ func newUI() *ui {
} }
} }
func (ui *ui) renew() { func (ui *ui) renew(nav *nav) {
wtot, htot := termbox.Size() wtot, htot := termbox.Size()
widths := getWidths(wtot) widths := getWidths(wtot)