-
Notifications
You must be signed in to change notification settings - Fork 0
/
nudge.go
81 lines (68 loc) · 1.82 KB
/
nudge.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
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/go-vgo/robotgo"
)
var (
PID int
flagDisabled bool
flagInterval time.Duration
flagKey string
)
// Nudge keeps the computer awake by pressing a provided key. It's simply a
// wrapper for `robotgo.KeyTap()`.
func Nudge(key string, modifiers interface{}) {
robotgo.KeyTap(key, modifiers)
}
// Logs to a file (stdout, stderr, or file)
func log(s string, file *os.File) {
fmt.Fprintf(file, "%s\n", s)
}
// Parse any flags sent to the command
func parseFlags() {
flag.BoolVar(&flagDisabled, "disable", false, "start disabled")
flag.DurationVar(&flagInterval, "interval", 59*time.Second, "interval between nudges")
flag.StringVar(&flagKey, "key", "f16", "key to press")
flag.Parse()
}
func main() {
parseFlags()
PID = os.Getpid()
log(fmt.Sprintf("pid: %d", PID), os.Stderr)
// Set interrupt keys:
sigQuit := make(chan os.Signal, 1)
signal.Notify(sigQuit, syscall.SIGINT, syscall.SIGTERM)
// Setup toggling of disabled status via SIGUSR1:
sigToggle := make(chan os.Signal, 0)
signal.Notify(sigToggle, syscall.SIGUSR1)
go func() {
// Set up an infinite loop to continually listen for SIGUSR1 and toggle
// once per signal:
for {
<-sigToggle
flagDisabled = !flagDisabled
log(fmt.Sprintf("disabled: %t", flagDisabled), os.Stdout)
}
}()
// Set up the nudger:
nudger := time.NewTicker(flagInterval)
defer nudger.Stop()
go func() {
// Nudge at every interval:
for t := range nudger.C {
if !flagDisabled {
Nudge(flagKey, nil)
log(fmt.Sprintf("nudged: %v", t.Local().Format("15:04:05")), os.Stderr)
}
}
}()
log("send SIGINT/SIGTERM to quit, SIGUSR1 to toggle...", os.Stderr)
// Block/keep running goroutine until a quit signal is received:
s := <-sigQuit
log(fmt.Sprintf("%v received", s), os.Stderr)
}