Skip to content

Instantly share code, notes, and snippets.

@draftcode
Last active December 20, 2015 08:08
Show Gist options
  • Select an option

  • Save draftcode/6097983 to your computer and use it in GitHub Desktop.

Select an option

Save draftcode/6097983 to your computer and use it in GitHub Desktop.
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