Add watch option

This commit is contained in:
Joe Lim committed 2024-03-30 19:29:15 +11:00
1 parent 1e10e95f4d
commit 7c3cfa8865
5 files changed
+45

No files matched your search

+9
View File
@@ -185,6 +185,15 @@ func (e *setExpr) eval(app *app, args []string) {
app.ui.sort() app.ui.sort()
app.ui.loadFile(app, true) 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!": case "wrapscan", "nowrapscan", "wrapscan!":
err = applyBoolOpt(&gOpts.wrapscan, e) err = applyBoolOpt(&gOpts.wrapscan, e)
case "wrapscroll", "nowrapscroll", "wrapscroll!": case "wrapscroll", "nowrapscroll", "wrapscroll!":
+1
View File
@@ -4,6 +4,7 @@ go 1.18
require ( require (
github.com/djherbis/times v1.6.0 github.com/djherbis/times v1.6.0
github.com/fsnotify/fsnotify v1.7.0
github.com/gdamore/tcell/v2 v2.7.4 github.com/gdamore/tcell/v2 v2.7.4
github.com/mattn/go-runewidth v0.0.15 github.com/mattn/go-runewidth v0.0.15
golang.org/x/sys v0.18.0 golang.org/x/sys v0.18.0
+2
View File
@@ -1,5 +1,7 @@
github.com/djherbis/times v1.6.0 h1:w2ctJ92J8fBvWPxugmXIv7Nz7Q3iDMKNx9v5ocVH20c= github.com/djherbis/times v1.6.0 h1:w2ctJ92J8fBvWPxugmXIv7Nz7Q3iDMKNx9v5ocVH20c=
github.com/djherbis/times v1.6.0/go.mod h1:gOHeRAz2h+VJNZ5Gmc/o7iD9k4wW7NMVqieYCY99oc0= 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 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg= github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell/v2 v2.7.4 h1:sg6/UnTM9jGpZU+oFYAsDahfchWAFW8Xx2yFinNSAYU= github.com/gdamore/tcell/v2 v2.7.4 h1:sg6/UnTM9jGpZU+oFYAsDahfchWAFW8Xx2yFinNSAYU=
+31
View File
@@ -16,6 +16,7 @@ import (
"time" "time"
"github.com/djherbis/times" "github.com/djherbis/times"
"github.com/fsnotify/fsnotify"
) )
type linkState byte type linkState byte
@@ -466,6 +467,8 @@ type nav struct {
previewLoading bool previewLoading bool
jumpList []string jumpList []string
jumpListInd int jumpListInd int
watcher *fsnotify.Watcher
watcherEvents <-chan fsnotify.Event
} }
func (nav *nav) loadDirInternal(path string) *dir { func (nav *nav) loadDirInternal(path string) *dir {
@@ -2065,3 +2068,31 @@ func (nav *nav) calcDirSize() error {
return nil 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
}
+2
View File
@@ -64,6 +64,7 @@ var gOpts struct {
smartcase bool smartcase bool
smartdia bool smartdia bool
waitmsg string waitmsg string
watch bool
wrapscan bool wrapscan bool
wrapscroll bool wrapscroll bool
findlen int findlen int
@@ -209,6 +210,7 @@ func init() {
gOpts.smartcase = true gOpts.smartcase = true
gOpts.smartdia = false gOpts.smartdia = false
gOpts.waitmsg = "Press any key to continue" gOpts.waitmsg = "Press any key to continue"
gOpts.watch = false
gOpts.wrapscan = true gOpts.wrapscan = true
gOpts.wrapscroll = false gOpts.wrapscroll = false
gOpts.findlen = 1 gOpts.findlen = 1