-
Notifications
You must be signed in to change notification settings - Fork 4
/
watcher.go
60 lines (52 loc) · 1.31 KB
/
watcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package cmdui
import (
"time"
"github.com/fsnotify/fsnotify"
"github.com/tzapio/tzap/internal/logging/tl"
)
func (ui *CMDUI) WatchSavesToFile(changedWithin time.Duration, times int) error {
// Watch for file changes
var lastChangeTime time.Time
var changes int
// Create a new file watcher to monitor changes to the file
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
// Start watching the file
err = watcher.Add(ui.filePath)
if err != nil {
return err
}
defer watcher.Remove(ui.filePath)
for {
select {
case event := <-watcher.Events:
// If the file was modified, check how long ago the last modification was
if event.Op.Has(fsnotify.Write) {
now := time.Now()
passed := now.Sub(lastChangeTime)
if passed < time.Millisecond*50 {
continue
}
if passed <= changedWithin {
changes++
tl.Logger.Println("file saved - incrementing changes", changes)
} else {
tl.Logger.Println("file saved - resetting changes")
changes = 1
}
lastChangeTime = now
// If we've seen multiple changes within the time window, call the callback
if changes >= times {
tl.Logger.Printf("saved %d times in 2 seconds", times)
changes = 0
return nil
}
}
case err := <-watcher.Errors:
println("error:", err)
return err
}
}
}