zshrc: rewrite battery function
authorMoviuro <moviuro+grml@gmail.com>
Mon, 15 Sep 2014 10:11:26 +0000 (12:11 +0200)
committerFrank Terbeck <ft@grml.org>
Mon, 15 Sep 2014 10:24:13 +0000 (12:24 +0200)
Now supports multiple batteries
Now indicates status (^ Charging, v Discharging, = Full or Unknown)
Now supports different functions for different OSes

TODO: add configurability (zstyle, etc.)
TODO: add support for other OSes (ATM, only Linux is supported)

etc/zsh/zshrc

index 1b051a5..abfde9a 100644 (file)
@@ -1649,28 +1649,49 @@ PS4='+%N:%i:%_> '
 #    - debian_chroot
 #    - vcs_info setup and version specific fixes
 
-# display battery status on right side of prompt via running 'BATTERY=1 zsh'
+# display battery status on right side of prompt using 'BATTERY=1' in .zshrc.pre
 if [[ $BATTERY -gt 0 ]] ; then
-    if ! check_com -c acpi ; then
+    if ! islinux ; then
+        # not yet supported
         BATTERY=0
     fi
 fi
 
 battery() {
 if [[ $BATTERY -gt 0 ]] ; then
-    PERCENT="${${"$(acpi 2>/dev/null)"}/(#b)[[:space:]]#Battery <->: [^0-9]##, (<->)%*/${match[1]}}"
-    if [[ -z "$PERCENT" ]] ; then
-        PERCENT='acpi not present'
-    else
-        if [[ "$PERCENT" -lt 20 ]] ; then
-            PERCENT="warning: ${PERCENT}%%"
-        else
-            PERCENT="${PERCENT}%%"
-        fi
+    if islinux ; then
+        batterylinux
     fi
 fi
 }
 
+batterylinux(){
+PERCENT=''
+local batteries bat capacity
+batteries=( /sys/class/power_supply/BAT*(N) )
+if (( $#batteries > 0 )) ; then
+    for bat in $batteries ; do
+        capacity=$(< $bat/capacity)
+        case $(< $bat/status) in
+        Charging)
+            PERCENT+=" ^"
+            ;;
+        Discharging)
+            if (( capacity < 20 )) ; then
+                PERCENT+=" !v"
+            else
+                PERCENT+=" v"
+            fi
+            ;;
+        *) # Full, Unknown
+            PERCENT+=" ="
+            ;;
+        esac
+        PERCENT+="${capacity}%%"
+    done
+fi
+}
+
 # set variable debian_chroot if running in a chroot with /etc/debian_chroot
 if [[ -z "$debian_chroot" ]] && [[ -r /etc/debian_chroot ]] ; then
     debian_chroot=$(</etc/debian_chroot)