[download]

local/bin/battery

   1 #!/bin/bash
   2 
   3 get(){ # {{{
   4     busctl get-property org.freedesktop.UPower \
   5     $1 org.freedesktop.UPower.Device $2 |
   6     cut -d ' ' -f 2- | sed 's/^"//; s/"$//'
   7 } # }}}
   8 
   9 state=('Unknown' 'Charging' 'Discharging' 'Empty' 'Full' 'Pending' 'Pending')
  10 type=('Unknown' 'Line Power' 'Battery' 'Ups' 'Monitor' 'Mouse' 'Keyboard' 'Pda' 'Phone')
  11 
  12 upower -e | while read i; do
  13     m=$(get $i Model)
  14     p=$(get $i Percentage)
  15     s=$(get $i State)
  16     # t=$(get $i Type)
  17 
  18     [[ $m ]] && printf '%s\t%s\t%s\n' "${state[$s]}" "$p%" "$m"
  19 done | sort -k 1 -k 2n
  20 
  21 # adb devices | sed '1d; /^$/d' | while read i _; do
  22 #     m=$(adb -s $i shell getprop ro.product.model | tr -d '\r\n')
  23 #     p=$(adb -s $i shell cat /sys/class/power_supply/battery/capacity | tr -d \\r)
  24 #     s=$(adb -s $i shell cat /sys/class/power_supply/battery/status   | tr -d \\r)
  25 #
  26 #     [[ $m ]] && printf '%3d%%\t%s\t%s\n' $p $s "$m"
  27 # done