Created
February 20, 2017 18:52
-
-
Save thewraven/3c45b1776695841cb28764008189a0cc to your computer and use it in GitHub Desktop.
Linux battery status - useful for zsh customization
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" | |
"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