Last active
April 23, 2017 11:16
-
-
Save giulioungaretti/f77d5e2f652155ed7d197a8822182fc7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.go | |
// Copyright (C) 2017 unga <giulioungaretti@me.com> | |
// | |
// Distributed under terms of the MIT license. | |
// | |
package main | |
import ( | |
"io/ioutil" | |
"os/exec" | |
"time" | |
log "github.com/Sirupsen/logrus" | |
) | |
type dB map[time.Time][]byte | |
func main() { | |
// check every second | |
ticker := time.NewTicker(time.Second * 1) | |
log.Debug("Started") | |
db := make(dB) | |
xradnr := exec.Command("xrandr") | |
grep := exec.Command("grep", "'\bconnected\b'") | |
piped, err := xradnr.StdoutPipe() | |
if err != nil { | |
log.Fatal(err) | |
} | |
stderr, err := xradnr.StderrPipe() | |
if err != nil { | |
log.Fatal(err) | |
} | |
grepoutput, err := grep.StdoutPipe() | |
if err != nil { | |
log.Fatal(err) | |
} | |
greperror, err := grep.StderrPipe() | |
if err != nil { | |
log.Fatal(err) | |
} | |
grep.Stdin = piped | |
for { | |
select { | |
case ts := <-ticker.C: | |
err = xradnr.Start() | |
if err != nil { | |
log.Error(err) | |
slurp, _ := ioutil.ReadAll(stderr) | |
log.Error("wtf: %s\n", slurp) | |
return | |
} | |
err := grep.Start() | |
if err != nil { | |
log.Errorf("Cant' start grep %s", err) | |
return | |
} | |
err = grep.Wait() | |
if err != nil { | |
log.Error("can't wait grep %s", err) | |
slurp, _ := ioutil.ReadAll(greperror) | |
log.Errorf("wtf: %s\n", slurp) | |
return | |
} | |
slurp, err := ioutil.ReadAll(grepoutput) | |
if err != nil { | |
log.Error("can't copy", err) | |
return | |
} | |
db[ts] = slurp | |
log.Infof("%s", slurp) | |
err = xradnr.Wait() | |
if err != nil { | |
log.Error("can't wait xrandrx", err) | |
return | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment