[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Color bash prompt in function of the charge of battery (acpi based)



Here's an update to my APM version of this. It incoprorates some of Fabio's changes to his ACPI version. It also reads directly from /proc/apm rather than using the apm command because it is faster and easier to parse, but may break for other kernel versions (I'm using 2.4.20).


if [ -f /proc/apm ]; then

  function apm_read
  {
    APM_STRING=(`cat /proc/apm`)
    APM_DRIVER_VERSION=${APM_STRING[0]}
    APM_BIOS_VERSION=${APM_STRING[1]}
    APM_TIME=${APM_STRING[7]}
    if [ $APM_TIME -lt 0 ]; then
      APM_NO_BATT=1
      APM_PERCENT=0
    else
      AMP_NO_BATT=0
      APM_PERCENT=${APM_STRING[6]%%%*}
    fi

    if [ "${APM_STRING[3]}" == "0x00" ]; then
      APM_AC=-
      case $APM_PERCENT in
        10?)  APM_COLOR="1;37"  ;;
         9?)  APM_COLOR="0;34"  ;;
         8?)  APM_COLOR="0;34"  ;;
         7?)  APM_COLOR="0;34"  ;;
         6?)  APM_COLOR="0;34"  ;;
         5?)  APM_COLOR="0;32"  ;;
         4?)  APM_COLOR="0;32"  ;;
         3?)  APM_COLOR="1;33"  ;;
         2?)  APM_COLOR="1;33"  ;;
         1?)  APM_COLOR="0;31"  ;;
          ?)  APM_COLOR="0;31;5";;
          *)  APM_COLOR="0;35"  ;;
      esac
    else
      APM_AC=+
      APM_COLOR="1;37"
    fi
  }

  function apm_dump
  {
    apm_read
    echo "DRIVER_VERSION = $APM_DRIVER_VERSION"
    echo "BIOS_VERSION = $APM_BIOS_VERSION"
    echo "AC = $APM_AC"
    echo "PERCENT = $APM_PERCENT"
    echo "TIME = $APM_TIME"
    echo "NO_BATT = $AMP_NO_BATT"
    echo "COLOR = $APM_COLOR"
  }

  PROMPT_COMMAND=amp_read
  if [ "$TERM" == "linux"]; then
    PS1="\[\e[\${APM_COLOR}m\]\h\$\[\e[0m\] "
  else
    PS1="\[\e[1m\](\$APM_AC\$APM_PERCENT%)\h\$\[\e[0m\] "
  fi

fi



Fabio Sirna wrote:
I do some change on the bash script that I've posted few days ago. This
works better. It must be in the .bashrc.


.,-''-,..,-''-,..,-''-,..,-''-,..,-''-,..,-''-,..,-''-,..,-''-,. # Color the bash prompt in function of the percentage of battery
# with acpi subsystem.
# Based on the originally apm based script that has been posted
# on debian-laptop by Jason Kraftcheck <kraftche@cae.wisc.edu>.
#
# This script is licensed under the GNU GPL version 2 or later,
# see /usr/share/common-licences/GPL on a Debian system or
# http://www.gnu.org/copyleft/gpl.html on the web.
# (c) 2003 Fabio 'farnis' Sirna <farnis@libero.it>

function acpi_percent()
{
 if [ `cat /proc/acpi/battery/BAT0/state | grep present: |cut -d\  -f18` = "yes" ]; then
  {
   CAPACITY=`cat /proc/acpi/battery/BAT0/info |grep "design capacity:"|cut -d\  -f11`
   LEVEL=`cat /proc/acpi/battery/BAT0/state | grep remaining|cut -d\  -f8`
   ACPI_PERCENT=`echo $(( $LEVEL * 100 / $CAPACITY ))`
   if [ "$LEVEL" = "$CAPACITY" ]; then
    echo FULL
   else
    echo $ACPI_PERCENT%
   fi
  }
 else echo "NO BATTERY"
 fi
}

function acpi_charge()
{
 ACPI_CHARGE=`cat /proc/acpi/ac_adapter/AC/state | cut -d\  -f20`
 case $ACPI_CHARGE in
       *on-line*)
         ACPI_CHARGE="+" ;;
       *off-line*)
         ACPI_CHARGE="-" ;;
     esac
     echo $ACPI_CHARGE
}

function acpi_color()
   {
     if  [  "$(acpi_charge)"  =  "+"  ];  then
      {
       if [ `cat /proc/acpi/battery/BAT0/state | grep present: |cut -d\  -f18` = "no" ]; then
        echo  "0;31"
       else echo  "1;32"
      fi
     }
     else
       case  $(acpi_percent)  in
          10?%)  echo  "0;32"  ;;
           9?%)  echo  "0;32"  ;;
           8?%)  echo  "0;32"  ;;
           7?%)  echo  "0;32"  ;;
           6?%)  echo  "0;32"  ;;
           5?%)  echo  "0;32"  ;;
           4?%)  echo  "0;33"  ;;
           3?%)  echo  "0;33"  ;;
           2?%)  echo  "0;33"  ;;
1?%) echo "0;31" ;; ?%) echo "0;31;5" ;;
             *)  echo  "0;35"  ;;
esac
     fi
   }

function  acpi_color_prompt
   {
     PS1='\[\e[$(acpi_color)m\][$(acpi_charge)$(acpi_percent)][\t] \u:\w\$>\[\e[0;37m\] '
     #PS1="\[\e[$(acpi_color)m\]\h\$\[\e[0m\]  "
   }

   #  linux  console
   if  [  "$TERM"  =  "linux"  ];  then
     PROMPT_COMMAND=acpi_color_prompt
   fi

   function  echo_acpi
   {
     echo -n "($(acpi_charge)$(acpi_percent)) "
   }
.,-''-,..,-''-,..,-''-,..,-''-,..,-''-,..,-''-,..,-''-,..,-''-,.




Reply to: