Last active
December 20, 2015 08:08
-
-
Save draftcode/6097983 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package main | |
| import ( | |
| "fmt" | |
| "os/exec" | |
| "regexp" | |
| "strconv" | |
| "strings" | |
| "time" | |
| ) | |
| func Watch() { | |
| percentagePattern := regexp.MustCompile(`(\d+)%`) | |
| prev := int64(0) | |
| for { | |
| cmd := exec.Command("acpi") | |
| out_bytes, err := cmd.Output() | |
| if err != nil { | |
| fmt.Println(err) | |
| time.Sleep(time.Minute) | |
| continue | |
| } | |
| out := string(out_bytes) | |
| p := percentagePattern.FindStringSubmatch(out) | |
| i, _ := strconv.ParseInt(p[1], 10, 8) | |
| if strings.Contains(out, "Discharging") && | |
| i != prev && i <= 15 { | |
| exec.Command("notify-send", "Battery low alert").Run() | |
| } | |
| prev = i | |
| time.Sleep(time.Minute) | |
| } | |
| } | |
| func main() { | |
| Watch() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment