From 7c3cfa8865f4c38e0649db29f6aed402e306a807 Mon Sep 17 00:00:00 2001 From: Joe Lim <50560759+joelim-work@users.noreply.github.com> Date: Sat, 30 Mar 2024 19:29:15 +1100 Subject: [PATCH] Add watch option --- eval.go | 9 +++++++++ go.mod | 1 + go.sum | 2 ++ nav.go | 31 +++++++++++++++++++++++++++++++ opts.go | 2 ++ 5 files changed, 45 insertions(+) diff --git a/eval.go b/eval.go index a406c32b..28f11286 100644 --- a/eval.go +++ b/eval.go @@ -185,6 +185,15 @@ func (e *setExpr) eval(app *app, args []string) { app.ui.sort() app.ui.loadFile(app, true) } + case "watch", "nowatch", "watch!": + err = applyBoolOpt(&gOpts.watch, e) + if err == nil { + if gOpts.watch { + app.nav.startWatcher() + } else { + app.nav.stopWatcher() + } + } case "wrapscan", "nowrapscan", "wrapscan!": err = applyBoolOpt(&gOpts.wrapscan, e) case "wrapscroll", "nowrapscroll", "wrapscroll!": diff --git a/go.mod b/go.mod index 10886b44..e1364f23 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.18 require ( github.com/djherbis/times v1.6.0 + github.com/fsnotify/fsnotify v1.7.0 github.com/gdamore/tcell/v2 v2.7.4 github.com/mattn/go-runewidth v0.0.15 golang.org/x/sys v0.18.0 diff --git a/go.sum b/go.sum index b10dc490..b3ea3cde 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/djherbis/times v1.6.0 h1:w2ctJ92J8fBvWPxugmXIv7Nz7Q3iDMKNx9v5ocVH20c= github.com/djherbis/times v1.6.0/go.mod h1:gOHeRAz2h+VJNZ5Gmc/o7iD9k4wW7NMVqieYCY99oc0= +github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= +github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko= github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg= github.com/gdamore/tcell/v2 v2.7.4 h1:sg6/UnTM9jGpZU+oFYAsDahfchWAFW8Xx2yFinNSAYU= diff --git a/nav.go b/nav.go index f620222e..f7c64f53 100644 --- a/nav.go +++ b/nav.go @@ -16,6 +16,7 @@ import ( "time" "github.com/djherbis/times" + "github.com/fsnotify/fsnotify" ) type linkState byte @@ -466,6 +467,8 @@ type nav struct { previewLoading bool jumpList []string jumpListInd int + watcher *fsnotify.Watcher + watcherEvents <-chan fsnotify.Event } func (nav *nav) loadDirInternal(path string) *dir { @@ -2065,3 +2068,31 @@ func (nav *nav) calcDirSize() error { return nil } + +func (nav *nav) startWatcher() { + if nav.watcher != nil { + return + } + log.Printf("start watch") + + watcher, err := fsnotify.NewWatcher() + if err != nil { + log.Printf("start watcher: %s", err) + return + } + + nav.watcher = watcher + nav.watcherEvents = watcher.Events +} + +func (nav *nav) stopWatcher() { + if nav.watcher == nil { + return + } + log.Printf("stop watch") + + nav.watcher.Close() + + nav.watcher = nil + nav.watcherEvents = nil +} diff --git a/opts.go b/opts.go index fc211e05..c84ef2f7 100644 --- a/opts.go +++ b/opts.go @@ -64,6 +64,7 @@ var gOpts struct { smartcase bool smartdia bool waitmsg string + watch bool wrapscan bool wrapscroll bool findlen int @@ -209,6 +210,7 @@ func init() { gOpts.smartcase = true gOpts.smartdia = false gOpts.waitmsg = "Press any key to continue" + gOpts.watch = false gOpts.wrapscan = true gOpts.wrapscroll = false gOpts.findlen = 1