-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (72 loc) · 1.6 KB
/
main.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"flag"
"fmt"
"github.com/deckarep/gosx-notifier"
"log"
"os"
"os/exec"
"time"
)
const Version = "0.1.0"
const helpText = `Usage: pbwatch [-n]
pbwatch - display and update current clipboard text on terminal
Options:
-n Send event to desktop notification center
-version Print version
`
func printUsage() {
fmt.Fprintf(os.Stderr, helpText)
}
func clearScreen() {
cmd := exec.Command("clear")
cmd.Stdout = os.Stdout
cmd.Run()
}
func sendNotification(text string) {
note := gosxnotifier.NewNotification(text)
note.Title = "pbwatch"
note.Subtitle = "Copied!"
note.Group = "pbwatch"
err := note.Push()
if err != nil {
log.Fatal(err)
}
}
func main() {
var optNotify bool
var optVersion bool
flags := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
flags.Usage = func() { printUsage() }
flags.BoolVar(&optNotify, "n", false, "Popup copy event")
flags.BoolVar(&optNotify, "notification", false, "Popup copy event")
flags.BoolVar(&optVersion, "version", false, "Version")
if err := flags.Parse(os.Args[1:]); err != nil {
flags.Usage()
os.Exit(1)
}
if optVersion {
fmt.Fprintf(os.Stderr, "pbwatch version %s\n", Version)
os.Exit(0)
}
interval := 500 * time.Millisecond
pbwatch := NewPbwatch(interval)
notifyCh := make(chan string)
go func() {
// suppress notification on boot
text := <-notifyCh
clearScreen()
fmt.Println(text)
// then run loop
for text := range notifyCh {
clearScreen()
fmt.Println(text)
if optNotify {
sendNotification(text)
}
}
}()
pbwatch.Start(notifyCh)
// wait for loop above
pbwatch.Wait()
}