From: Moviuro Date: Mon, 15 Sep 2014 10:11:26 +0000 (+0200) Subject: zshrc: rewrite battery function X-Git-Tag: v0.10.0~15 X-Git-Url: http://git.grml.org/?p=grml-etc-core.git;a=commitdiff_plain;h=287c3482e4fc7e8e8880aba60d7e0998cfc75558 zshrc: rewrite battery function 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) --- diff --git a/etc/zsh/zshrc b/etc/zsh/zshrc index 1b051a5..abfde9a 100644 --- a/etc/zsh/zshrc +++ b/etc/zsh/zshrc @@ -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=$(