dots

git clone git://mattcarlson.org/repos/dots.git
Log | Files | Refs

bat.sh (1725B)


      1 #!/bin/sh
      2 #
      3 # bat
      4 
      5 # just handle one battery
      6 BAT='/sys/class/power_supply/BAT0/'
      7 BAT_CHARGING_ICON=''
      8 BAT_WARNING_ICON=''
      9 BAT_EMPTY_ICON=''
     10 BAT_QUARTER_ICON=''
     11 BAT_HALF_ICON=''
     12 BAT_THREE_QUARTERS_ICON=''
     13 BAT_FULL_ICON=''
     14 
     15 get_energy_left() { energy_left="$(printf '%.2f' "$(printf '%s\n' "scale=4; ${POWER_SUPPLY_ENERGY_FULL}/${POWER_SUPPLY_ENERGY_FULL_DESIGN} * 100" | bc)")" ; }
     16 
     17 bar() {
     18     # prefix could be charging, warning, or both
     19     PREFIX=''
     20 
     21     # if charging then append charging icon to prefix
     22     [ "${POWER_SUPPLY_STATUS}" = 'Charging' ] && PREFIX="${PREFIX}${BAT_CHARGING_ICON} "
     23 
     24     # battery icon split into empty, quarter, half, three-quarters, and full
     25     case ${POWER_SUPPLY_CAPACITY} in
     26         0)                        ICON="${BAT_EMPTY_ICON}" && PREFIX="${PREFIX}${BAT_WARNING_ICON} "   ;;
     27         [1-9]|1[0-9]|2[0-5])      ICON="${BAT_QUARTER_ICON}" && PREFIX="${PREFIX}${BAT_WARNING_ICON} " ;;
     28         2[6-9]|3[0-9]|4[0-9]|50)  ICON="${BAT_HALF_ICON}"                                              ;;
     29         5[1-9]|6[0-9]|7[0-5])     ICON="${BAT_THREE_QUARTERS_ICON}"                                    ;;
     30         7[6-9]|8[0-9]|9[0-9]|100) ICON="${BAT_FULL_ICON}"                                              ;;
     31     esac
     32 
     33     printf '%s\n' "${PREFIX}${ICON} ${POWER_SUPPLY_CAPACITY}%"
     34 }
     35 
     36 main() {
     37     # no bat and return
     38     [ -d "${BAT}" ] || return 1
     39 
     40     # uevent holds battery variables
     41     [ -f "${BAT}/uevent" ] && . "${BAT}/uevent" 2>/dev/null
     42 
     43     # called from bar
     44     [ ${#} -eq 0 ] && bar
     45 
     46     # bar usage
     47     case ${BLOCK_BUTTON} in
     48         1) get_energy_left
     49            env HERBE_ID=/0 herbe "Energy left: ${energy_left}%"
     50            ;;
     51     esac
     52 }
     53 
     54 main "${@}"