diff --git a/app.go b/app.go index 7365b0cc..04fb9f6d 100644 --- a/app.go +++ b/app.go @@ -466,13 +466,20 @@ func (app *app) loop() { } app.ui.draw(app.nav) case path := <-app.nav.delChan: - delete(app.nav.dirCache, path) - delete(app.nav.regCache, path) - - delete(app.nav.selections, path) + deletePathRecusrive(app.nav.selections, path) if len(app.nav.selections) == 0 { app.nav.selectionInd = 0 } + + deletePathRecusrive(app.nav.regCache, path) + + deletePathRecusrive(app.nav.dirCache, path) + currPath := app.nav.currDir().path + if currPath == path || strings.HasPrefix(currPath, path+string(filepath.Separator)) { + if wd, err := os.Getwd(); err == nil { + app.nav.getDirs(wd) + } + } case ev := <-app.ui.evChan: e := app.ui.readEvent(ev, app.nav) if e == nil { diff --git a/misc.go b/misc.go index b33c959e..d93e03cf 100644 --- a/misc.go +++ b/misc.go @@ -378,6 +378,19 @@ func makeCollator(localeStr string, opts ...collate.Option) (*collate.Collator, return collate.New(localeTag, opts...), nil } +// This function deletes entries from a map if the key is either the given path +// or a subpath of it. +// This is useful for clearing cached data when a directory is moved or deleted. +func deletePathRecusrive[T any](m map[string]T, path string) { + delete(m, path) + prefix := path + string(filepath.Separator) + for k := range m { + if strings.HasPrefix(k, prefix) { + delete(m, k) + } + } +} + // We don't need no generic code // We don't need no type control // No dark templates in compiler diff --git a/nav.go b/nav.go index 19883a2f..fd1a60c9 100644 --- a/nav.go +++ b/nav.go @@ -1488,8 +1488,8 @@ func (nav *nav) rename() error { // existed and was deleted. In this case the cache entries should be deleted // before loading newPath to prevent displaying a stale preview. However, // this clears only the current instance of lf, and not any other instances. - delete(nav.regCache, newPath) - delete(nav.dirCache, newPath) + deletePathRecusrive(nav.regCache, newPath) + deletePathRecusrive(nav.dirCache, newPath) dir := nav.loadDir(filepath.Dir(newPath)) if dir.loading {