Advertisement
Guest User

laptop battery status in Bash prompt

a guest
May 10th, 2014
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # In .bashrc to add the 'battery_status' to PS1
  2. # From a script written here :
  3. #http://www.basicallytech.com/blog/index.php?/archives/110-Colour-coded-battery-charge-level-and-status-in-your-bash-prompt.html, by Rob NEWCATER
  4. # Most of the work is his, I only did minor editing
  5. # Edited by J.DESROCHES to work on my laptop running "Linux version 3.14.2-1-ARCH (nobody@var-lib-archbuild-testing-  #  x86_64-tobias) (gcc version 4.9.0 (GCC) ) #1 SMP PREEMPT Sun Apr 27 11:28:44 CEST 2014" (output of cat /proc/version)
  6. ########################################################################################################################
  7.  
  8. # By : J.D.
  9. # Battery status :
  10.  
  11. battery_status()
  12. {
  13.         BATTERY=/sys/class/power_supply/BAT1
  14.  
  15.         CHARGE=`cat $BATTERY/capacity`
  16.         BATSTATE=`cat $BATTERY/status`
  17. # Colors for humans
  18.         NON='\033[00m'
  19.         BLD='\033[01m'
  20.         RED='\033[01;31m'
  21.         GRN='\033[01;32m'
  22.         YEL='\033[01;33m'
  23.  
  24.         COLOUR="$RED"
  25.  
  26.         case "${BATSTATE}" in
  27.            'Charged')
  28.            BATSTT="$BLD=$NON"
  29.            ;;
  30.            'Charging')
  31.            BATSTT="$BLD+$NON"
  32.            ;;
  33.            'Discharging')
  34.            BATSTT="$BLD-$NON"
  35.            ;;
  36.         esac
  37.  
  38.         # prevent a charge of more than 100% displaying
  39.         if [ "$CHARGE" -gt "99" ]
  40.         then
  41.            CHARGE=100
  42.         fi
  43.  
  44.     # prevent an error if the battery is not in the laptop (e.g. you have two and take out the primary)
  45.     STATE=`cat $BATTERY/state`
  46.     if [ "$STATE" == 'present: no' ]
  47.     then
  48.         echo -e "${RED}nobat"
  49.     exit
  50.     fi
  51.  
  52.         if [ "$CHARGE" -gt "15" ]
  53.         then
  54.            COLOUR="$YEL"
  55.         fi
  56.  
  57.         if [ "$CHARGE" -gt "30" ]
  58.         then
  59.            COLOUR="$GRN"
  60.         fi
  61.         echo -e "${BATSTT}${COLOUR}${CHARGE}%${NON}"
  62. }
  63.  
  64. # and here's my PS1 if people are looking for ideas :
  65.  
  66. PS1='\[\e[1;34m\]{\d \t}\[\e[m\]\[\e[0;33m\]{\[\e[m\]$(battery_status)\[\e[0;33m\]}\[\e[m\] \[\e[0;32m\]\u\[\e[m\] @ \[\e[0;32m\]\h\[\e[m\] : \[\e[1;34m\]\W\[\e[m\] [\e[1;32m\]\$\[\e[m\]\n\[\e[1;37m\]'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement