]> gitweb.fluxo.info Git - scripts.git/commitdiff
Adding battery script for awesome
authorSilvio Rhatto <rhatto@riseup.net>
Sat, 3 May 2014 21:48:46 +0000 (18:48 -0300)
committerSilvio Rhatto <rhatto@riseup.net>
Sat, 3 May 2014 21:48:46 +0000 (18:48 -0300)
battery [new file with mode: 0755]

diff --git a/battery b/battery
new file mode 100755 (executable)
index 0000000..12682d3
--- /dev/null
+++ b/battery
@@ -0,0 +1,60 @@
+#!/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'
+# 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 (($charge <= 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>"