-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.go
66 lines (53 loc) · 969 Bytes
/
events.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
61
62
63
64
65
66
package watcher
import (
"fmt"
"time"
"github.com/fsnotify/fsnotify"
"github.com/sethigeet/watcher/watcher/cmd"
)
func sendEvents(w *fsnotify.Watcher) {
for {
select {
case ev, ok := <-w.Events:
if !ok {
return
}
// HACK: A way around a particular way of saving files(ie. rename original file and write a new file with the new contents)
if ev.Op == fsnotify.Remove {
w.Remove(ev.Name)
exists, err := cmd.Exists(ev.Name)
if err == nil && exists {
w.Add(ev.Name)
}
}
eventsChan <- ev.String()
case err, ok := <-w.Errors:
if !ok {
return
}
log.Errorf("An error occurred!\nerror: %s\n", err)
}
}
}
func handleEvents() {
for {
<-eventsChan
if *cmd.Config.Clear {
fmt.Printf("\x1bc")
}
log.Notice("Refreshing...")
time.Sleep(*cmd.Config.RunDelay)
flushEvents()
run()
}
}
func flushEvents() {
for {
select {
case <-eventsChan:
continue
default:
return
}
}
}