-
Notifications
You must be signed in to change notification settings - Fork 3
/
fetch_darwin.go
48 lines (38 loc) · 870 Bytes
/
fetch_darwin.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
package idle
import (
"fmt"
"os/exec"
"strings"
"time"
)
type fetcher interface {
Fetch() ([]byte, error)
}
type ioRegFetcher struct{}
func (ioRegFetcher) Fetch() ([]byte, error) {
return exec.Command("ioreg", "-c", "IOHIDSystem").Output()
}
func parseIdleFromIOReg(f fetcher) (time.Duration, error) {
var output time.Duration
var idleInNs string
ioRegOutput, err := f.Fetch()
if err != nil {
return output, err
}
rawStr := string(ioRegOutput)
lines := strings.Split(rawStr, "\n")
for _, line := range lines {
if !strings.Contains(line, "HIDIdleTime") {
continue
}
cols := strings.Split(line, " ")
idleInNs = cols[len(cols)-1]
break
}
return time.ParseDuration(fmt.Sprintf("%sns", idleInNs))
}
// Get idle time for Darwin (OSX)
func Get() (time.Duration, error) {
fetcher := ioRegFetcher{}
return parseIdleFromIOReg(fetcher)
}