aboutsummaryrefslogtreecommitdiff
path: root/battery
blob: 0fe1aebd19a6a8146d23fff0b9932c9b5f23b67a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/bash
#
# See http://blog.lick-me.org/2013/08/yet-another-battery-widget-awesome-3-5-1/
#
 
# Returns battery charge as a percentage.
# Thanks http://blmath.wordpress.com/2010/03/19/bash-function-to-get-battery-charge/
function battery_charge {
  now=`cat /sys/class/power_supply/BAT0/energy_now`
  full=`cat /sys/class/power_supply/BAT0/energy_full`
  out=`echo $now/$full*100 | bc -l | cut -c 1-5`

  echo $out | sed -e 's/\./,/g'
  #echo "Charge: "$out"%"
}
  
# Returns battery capacity as a percentage.
# Thanks http://blmath.wordpress.com/2010/03/19/bash-function-to-get-battery-charge/
function battery_capacity {
  design=`cat /sys/class/power_supply/BAT0/energy_full_design`
  current=`cat /sys/class/power_supply/BAT0/energy_full`
  out=`echo $current/$design*100 | bc -l | cut -c 1-5`

  echo $out | sed -e 's/\./,/g'
  #echo "Capacity: "$out"%"
}

# Basic parameters
healthy='#859900'
low='#b58900'
discharge='#dc322f'

# Abort if no battery is available
if [ ! -e "/sys/class/power_supply/BAT0" ]; then
  exit
fi
 
# Get battery status
if [ -e "/sys/class/power_supply/BAT0/charge" ]; then
  charge=`cat /sys/class/power_supply/BAT0/charge`
else
  charge="`battery_charge`"
fi

# Set battery level indication
if ((`echo $charge | cut -d ',' -f 1` <= 25)); then
  chargeColor=$low
else
  chargeColor=$healthy
fi
 
# Get battery status
status=`cat /sys/class/power_supply/BAT0/status`
 
# Set battery status indication
if [[ "$status" = "Discharging" ]]; then
  statusColor=$discharge
  status="▼"
else
  statusColor=$healthy
  status="▲"
fi
 
# Output
echo "<span color=\"$chargeColor\">$charge%</span> <span color=\"$statusColor\">$status</span>"