Repository navigation
Expand file tree
/
Copy pathnotify.go
More file actions
75 lines (66 loc) · 1.49 KB
/
Copy pathnotify.go
File metadata and controls
75 lines (66 loc) · 1.49 KB
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
67
68
69
70
71
72
73
74
75
package main
import (
"fmt"
"log"
"os/exec"
"runtime"
"strconv"
"sync"
"time"
)
const notifyDebounce = 5 * time.Second
type commentNotifier struct {
mu sync.Mutex
pending map[string]*notifyBurst
}
type notifyBurst struct {
timer *time.Timer
filename string
name string
n int
}
func newCommentNotifier() *commentNotifier {
return &commentNotifier{pending: map[string]*notifyBurst{}}
}
func (n *commentNotifier) note(key, filename, name string) {
n.mu.Lock()
defer n.mu.Unlock()
b, ok := n.pending[key]
if !ok {
b = ¬ifyBurst{filename: filename, name: name, n: 1}
b.timer = time.AfterFunc(notifyDebounce, func() {
n.flush(key)
})
n.pending[key] = b
return
}
b.n++
b.name = name
b.filename = filename
b.timer.Reset(notifyDebounce)
}
func (n *commentNotifier) flush(key string) {
n.mu.Lock()
b, ok := n.pending[key]
delete(n.pending, key)
n.mu.Unlock()
if !ok {
return
}
body := fmt.Sprintf("%s commented on %s", b.name, b.filename)
if b.n > 1 {
body = fmt.Sprintf("%d comments on %s", b.n, b.filename)
}
log.Printf("notify: %s", body)
sendOSNotification("Gander", body)
}
var sendOSNotification = sendOSNotificationDefault
func sendOSNotificationDefault(title, body string) {
switch runtime.GOOS {
case "darwin":
script := fmt.Sprintf("display notification %s with title %s", strconv.Quote(body), strconv.Quote(title))
_ = exec.Command("osascript", "-e", script).Run()
case "linux":
_ = exec.Command("notify-send", title, body).Run()
}
}