Skip to content

Commit

Permalink
Simplify logic for adding watches
Browse files Browse the repository at this point in the history
  • Loading branch information
joelim-work committed Jan 5, 2025
1 parent f67249b commit 4edbf6e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 30 deletions.
32 changes: 8 additions & 24 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ func (app *app) loop() {
}
}

app.addWatchPaths()
app.watchDir(d)

app.ui.draw(app.nav)
case r := <-app.nav.regChan:
Expand Down Expand Up @@ -633,33 +633,17 @@ func (app *app) runShell(s string, args []string, prefix string) {
}
}

func (app *app) addWatchPaths() {
if !gOpts.watch || len(app.nav.dirs) == 0 {
func (app *app) watchDir(dir *dir) {
if !gOpts.watch {
return
}

paths := make(map[string]bool)
app.watch.add(dir.path)

// ensure dircounts are updated in current/parent windows
for _, dir := range app.nav.dirs {
paths[dir.path] = true
for _, file := range dir.allFiles {
if file.IsDir() {
paths[file.path] = true
}
// ensure dircounts are updated for child directories
for _, file := range dir.allFiles {
if file.IsDir() {
app.watch.add(file.path)
}
}

// ensure dircounts are updated in preview window
if curr, err := app.nav.currFile(); err == nil {
if dir, ok := app.nav.dirCache[curr.path]; ok {
for _, file := range dir.allFiles {
if file.IsDir() {
paths[file.path] = true
}
}
}
}

app.watch.add(paths)
}
5 changes: 3 additions & 2 deletions eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ func (e *setExpr) eval(app *app, args []string) {
if err == nil {
if gOpts.watch {
app.watch.start()
app.addWatchPaths()
for _, dir := range app.nav.dirCache {
app.watchDir(dir)
}
} else {
app.watch.stop()
}
Expand Down Expand Up @@ -582,7 +584,6 @@ func preChdir(app *app) {

func onChdir(app *app) {
app.nav.addJumpList()
app.addWatchPaths()
if cmd, ok := gOpts.cmds["on-cd"]; ok {
cmd.eval(app, nil)
}
Expand Down
6 changes: 2 additions & 4 deletions watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,12 @@ func (watch *watch) stop() {
watch.events = nil
}

func (watch *watch) add(paths map[string]bool) {
func (watch *watch) add(path string) {
if watch.watcher == nil {
return
}

for path := range paths {
watch.watcher.Add(path)
}
watch.watcher.Add(path)
}

func (watch *watch) loop() {
Expand Down

0 comments on commit 4edbf6e

Please sign in to comment.