Skip to content

Instantly share code, notes, and snippets.

@nilox94
Created June 13, 2018 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nilox94/ff085441ab59b6d60dfb2a8ff4408af9 to your computer and use it in GitHub Desktop.
Save nilox94/ff085441ab59b6d60dfb2a8ff4408af9 to your computer and use it in GitHub Desktop.
Animated progress bar of laptop battery in command line
#!/bin/sh
info=`acpi`
level=`echo $info | grep -Eo '[0-9]{,3}%' | tr -d '%'`
msg=`echo $info | sed 's/.*: //g'`
if [ $# = 0 ]
then columns=$((`tput cols` - 2))
else columns=$(( $1 - 2 ))
fi
blocks=$(( (level*columns)/100 ))
if [ $columns -lt 6 ]
then msg=''
elif test $columns -lt ${#msg}
then msg="$level%"
fi
count=${#msg}
span=$(( (columns - count) / 2 ))
printf ' '
if [ $level -lt 10 ]
then printf '\033[48;5;160m'
elif test $level -lt 30
then printf '\033[48;5;214m'
else printf '\033[48;5;34m'
fi
i=0
while [ $i -lt $columns ]
do
if [ $i -eq $blocks ]; then printf '\033[48;5;235m'; fi
if [ $i -ge $span -a $(( i - span )) -lt $count ]
then
char=`echo $msg | cut -b $(( i - span + 1 ))`
if [ "$char" != ' ' ]
then printf '\033[1m%s' `echo $msg | cut -b $(( i - span + 1))`
else printf ' '
fi
else printf ' '
fi
i=$(( i + 1))
done
printf '\033[0m\n'
unset info level columns blocks msg span char
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment