From ee5b3f4813f91831222728b7e666615eea9a8d12 Mon Sep 17 00:00:00 2001 From: Yegor Bychin <48245894+sigasigasiga@users.noreply.github.com> Date: Sun, 1 Aug 2021 17:00:10 +0300 Subject: [PATCH] add an option to disable directory cache (#673) * add an option to disable directory cache * change option name to `dircache` * change 4 whitespaces to tab --- app.go | 14 ++++++++------ eval.go | 6 ++++++ nav.go | 55 ++++++++++++++++++++++++++++++++----------------------- opts.go | 2 ++ 4 files changed, 48 insertions(+), 29 deletions(-) diff --git a/app.go b/app.go index 8544236a..96606e10 100644 --- a/app.go +++ b/app.go @@ -372,13 +372,15 @@ func (app *app) loop() { case d := <-app.nav.dirChan: app.nav.checkDir(d) - prev, ok := app.nav.dirCache[d.path] - if ok { - d.ind = prev.ind - d.sel(prev.name(), app.nav.height) - } + if gOpts.dircache { + prev, ok := app.nav.dirCache[d.path] + if ok { + d.ind = prev.ind + d.sel(prev.name(), app.nav.height) + } - app.nav.dirCache[d.path] = d + app.nav.dirCache[d.path] = d + } for i := range app.nav.dirs { if app.nav.dirs[i].path == d.path { diff --git a/eval.go b/eval.go index 86db9c9a..5e58021f 100644 --- a/eval.go +++ b/eval.go @@ -26,6 +26,12 @@ func (e *setExpr) eval(app *app, args []string) { gOpts.autoquit = false case "autoquit!": gOpts.autoquit = !gOpts.autoquit + case "dircache": + gOpts.dircache = true + case "nodircache": + gOpts.dircache = false + case "dircache!": + gOpts.dircache = !gOpts.dircache case "dircounts": gOpts.dircounts = true case "nodircounts": diff --git a/nav.go b/nav.go index 4e215da4..4fa6ca2c 100644 --- a/nav.go +++ b/nav.go @@ -332,33 +332,42 @@ type nav struct { volatilePreview bool } -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, - hiddenfiles: gOpts.hiddenfiles, - ignorecase: gOpts.ignorecase, - ignoredia: gOpts.ignoredia, - } - nav.dirCache[path] = d - go func() { - d := newDir(path) - d.sort() - d.ind, d.pos = 0, 0 - nav.dirChan <- d - }() - return d +func (nav *nav) loadDirInternal(path string) *dir { + d := &dir{ + loading: true, + loadTime: time.Now(), + path: path, + sortType: gOpts.sortType, + hiddenfiles: gOpts.hiddenfiles, + ignorecase: gOpts.ignorecase, + ignoredia: gOpts.ignoredia, } - - nav.checkDir(d) - + go func() { + d := newDir(path) + d.sort() + d.ind, d.pos = 0, 0 + nav.dirChan <- d + }() return d } +func (nav *nav) loadDir(path string) *dir { + if gOpts.dircache { + d, ok := nav.dirCache[path] + if !ok { + d = nav.loadDirInternal(path) + nav.dirCache[path] = d + return d + } + + nav.checkDir(d) + + return d + } else { + return nav.loadDirInternal(path) + } +} + func (nav *nav) checkDir(dir *dir) { s, err := os.Stat(dir.path) if err != nil { diff --git a/opts.go b/opts.go index 6e6e230c..fbb54174 100644 --- a/opts.go +++ b/opts.go @@ -30,6 +30,7 @@ type sortType struct { var gOpts struct { anchorfind bool autoquit bool + dircache bool dircounts bool dironly bool drawbox bool @@ -74,6 +75,7 @@ var gOpts struct { func init() { gOpts.anchorfind = true gOpts.autoquit = false + gOpts.dircache = true gOpts.dircounts = false gOpts.dironly = false gOpts.drawbox = false