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

100
nav.go
View File

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

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()
widths := getWidths(wtot)