From 19682c3cad67b6e5fd26848bc5c1c38c16d6ee01 Mon Sep 17 00:00:00 2001 From: ptrcnull Date: Thu, 12 Aug 2021 13:04:48 +0200 Subject: [PATCH] feat: Initial commit --- go.mod | 3 +++ main.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 go.mod create mode 100644 main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..2966d1d --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.ddd.rip/ptrcnull/bat-alert + +go 1.16 diff --git a/main.go b/main.go new file mode 100644 index 0000000..d24823f --- /dev/null +++ b/main.go @@ -0,0 +1,48 @@ +package main + +import ( + "flag" + "log" + "os" + "os/exec" + "strconv" + "strings" + "time" +) + +func notify(msg string) { + cmd := exec.Command("notify-send", "-t", "5000", "bat-alert", msg) + err := cmd.Run() + if err != nil { + log.Println(err) + } +} + +var targetFlag = flag.Int("target", 15, "when to send the alert") + +func main() { + flag.Parse() + sent := false + for { + capacity, err := os.ReadFile("/sys/class/power_supply/BAT0/capacity") + if err != nil { + notify("error: " + err.Error()) + } + + numCap, err := strconv.Atoi(strings.Trim(string(capacity), "\n")) + if err != nil { + notify("error: " + err.Error()) + } + + if numCap < 15 { + if !sent { + sent = true + notify("battery running low!") + } + } else { + sent = false + } + + time.Sleep(time.Second * 10) + } +}