Skip to content

Instantly share code, notes, and snippets.

@thewraven
Created February 20, 2017 18:52
Show Gist options
  • Save thewraven/3c45b1776695841cb28764008189a0cc to your computer and use it in GitHub Desktop.
Save thewraven/3c45b1776695841cb28764008189a0cc to your computer and use it in GitHub Desktop.
Linux battery status - useful for zsh customization
package main
import (
"fmt"
"math"
"os/exec"
"strconv"
"strings"
)
func main() {
cmd := exec.Command("bash", "-c", "upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -E 'percentage' | awk '{print $2}' | sed 's/%//'")
out, err := cmd.Output()
checkErr(err)
v := string(out)
v = strings.Trim(v, "\n")
perc, err := strconv.Atoi(v)
checkErr(err)
charged := int(math.Floor(float64(perc) / 10))
notCharged := 10 - charged
fmt.Println(strings.Repeat("◉ ", charged) + strings.Repeat("⦾ ", notCharged))
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment