From 20646bf31a87c1df27d6b766d794281cf173319e Mon Sep 17 00:00:00 2001 From: Joe Lim <50560759+joelim-work@users.noreply.github.com> Date: Sat, 30 Mar 2024 19:57:25 +1100 Subject: [PATCH] Implement directory changes --- app.go | 8 ++++++++ eval.go | 2 ++ nav.go | 26 ++++++++++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/app.go b/app.go index 98835e0b..a4820d3e 100644 --- a/app.go +++ b/app.go @@ -12,6 +12,8 @@ import ( "strings" "syscall" "time" + + "github.com/fsnotify/fsnotify" ) type cmdItem struct { @@ -456,6 +458,12 @@ func (app *app) loop() { case <-app.nav.previewTimer.C: app.nav.previewLoading = true app.ui.draw(app.nav) + case ev := <-app.nav.watcherEvents: + if ev.Has(fsnotify.Create) || ev.Has(fsnotify.Remove) || ev.Has(fsnotify.Rename) { + app.nav.renew() + app.ui.loadFile(app, false) + app.ui.draw(app.nav) + } } } } diff --git a/eval.go b/eval.go index 28f11286..b7389538 100644 --- a/eval.go +++ b/eval.go @@ -190,6 +190,7 @@ func (e *setExpr) eval(app *app, args []string) { if err == nil { if gOpts.watch { app.nav.startWatcher() + app.nav.setWatches() } else { app.nav.stopWatcher() } @@ -573,6 +574,7 @@ func onRedraw(app *app) { } func onSelect(app *app) { + app.nav.setWatches() if cmd, ok := gOpts.cmds["on-select"]; ok { cmd.eval(app, nil) } diff --git a/nav.go b/nav.go index f7c64f53..43cee035 100644 --- a/nav.go +++ b/nav.go @@ -2096,3 +2096,29 @@ func (nav *nav) stopWatcher() { nav.watcher = nil nav.watcherEvents = nil } + +func (nav *nav) setWatches() { + if nav.watcher == nil || len(nav.dirs) == 0 { + return + } + + watches := make(map[string]bool) + for _, dir := range nav.dirs { + watches[dir.path] = true + } + + currFile, err := nav.currFile() + if err == nil && currFile.IsDir() { + watches[currFile.path] = true + } + + for _, watch := range nav.watcher.WatchList() { + if !watches[watch] { + nav.watcher.Remove(watch) + } + } + + for watch := range watches { + nav.watcher.Add(watch) + } +}