renew directories asynchronously

Mentioned in #92.
This commit is contained in:
Gokcehan 2018-01-27 00:28:07 +03:00
parent 58538c8020
commit 59b2001253
2 changed files with 32 additions and 13 deletions

5
app.go
View File

@ -81,6 +81,11 @@ func (app *app) loop() {
return return
case d := <-app.nav.dirChan: 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 app.nav.dirCache[d.path] = d
for i := range app.nav.dirs { for i := range app.nav.dirs {

40
nav.go
View File

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