Last active
December 10, 2015 09:12
-
-
Save Dih5/e2c9035d147bb703366b to your computer and use it in GitHub Desktop.
Display colored battery charge. Useful for shell prompts.
This file contains 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
#!/bin/bash | |
show_help() { | |
cat << EOF | |
Usage: ${0##*/} [-hz] [-b NUM] | |
Print level of battery charge (1-100) formatted with colors. | |
-h display this help and exit. | |
-b NUM print a NUM bars instead of a number, e.g. '▸▸▸▸▸▸▹▹▹▹'. | |
-z use zsh colors instead of bash. Remember to autoload -U colors && colors in .zshrc. | |
EOF | |
} | |
#bash colors | |
green='\033[1;32m' | |
yellow='\033[0;33m' | |
red='\033[1;31m' | |
default='\033[0m' | |
barNumber=0 | |
OPTIND=1 | |
while getopts "hb:z" opt; do | |
case "$opt" in | |
h) | |
show_help | |
exit 0 | |
;; | |
b) | |
barNumber="$OPTARG" | |
;; | |
z) | |
green='%{$fg[green]%}' | |
yellow='%{$fg[yellow]%}' | |
red='%{$fg[red]%}' | |
default='%{$reset_color%}' | |
;; | |
\?) | |
show_help >&2 | |
exit 1 | |
;; | |
esac | |
done | |
shift "$((OPTIND-1))" | |
if [ -e "/sys/class/power_supply/BAT0/capacity" ] | |
then | |
level=$(cat /sys/class/power_supply/BAT0/capacity) | |
else | |
# try the deprecated directory | |
if [ -e "/proc/acpi/battery/BAT0/capacity" ] | |
then | |
level=$(cat /proc/acpi/battery/BAT0/capacity) | |
else | |
#Battery probably not present (or perhaps in another directory) | |
#Just print nothing | |
exit 0 | |
fi | |
fi | |
if ((level>=70)) | |
then | |
color=$green | |
elif ((level>=30)) | |
then | |
color=$yellow | |
else | |
color=$red | |
fi | |
if ((barNumber!=0)) | |
then | |
filled=$((level*barNumber/100)) | |
level='' | |
for ((i=0; i<$filled; i++)); do level+='▸'; done | |
for ((i=$filled; i<$barNumber; i++)); do level+='▹'; done | |
fi | |
echo -e ${color}${level}${default} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment