diff --git a/app.go b/app.go index b99df64..13bd1f6 100644 --- a/app.go +++ b/app.go @@ -81,6 +81,11 @@ func (app *app) loop() { return case d := <-app.nav.dirChan: + prev, ok := app.nav.dirCache[d.path] + if ok { + d.find(prev.name(), app.nav.height) + } + app.nav.dirCache[d.path] = d for i := range app.nav.dirs { diff --git a/nav.go b/nav.go index 9f5639e..dc1f43f 100644 --- a/nav.go +++ b/nav.go @@ -8,6 +8,7 @@ import ( "path/filepath" "sort" "strings" + "time" ) type linkState byte @@ -85,24 +86,28 @@ func readdir(path string) ([]*file, error) { } type dir struct { - loading bool // directory is loading from disk - ind int // index of current entry in fi - pos int // position of current entry in ui - path string // full path of directory - fi []*file // displayed files in directory including or excluding hidden ones - all []*file // all files in directory including hidden ones (same array as fi) + loading bool // directory is loading from disk + loadTime time.Time // current loading or last load time + ind int // index of current entry in fi + pos int // position of current entry in ui + path string // full path of directory + fi []*file // displayed files in directory including or excluding hidden ones + all []*file // all files in directory including hidden ones (same array as fi) } func newDir(path string) *dir { + time := time.Now() + fi, err := readdir(path) if err != nil { log.Printf("reading directory: %s", err) } return &dir{ - path: path, - fi: fi, - all: fi, + loadTime: time, + path: path, + fi: fi, + all: fi, } } @@ -270,10 +275,19 @@ func (nav *nav) renew(height int) { nav.height = height for _, d := range nav.dirs { - name := d.name() - d.renew() - d.sort() - d.find(name, height) + go func(d *dir) { + s, err := os.Stat(d.path) + if err != nil { + log.Printf("getting directory info: %s", err) + } + 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.marks {