From a1871106e985f9039a842f2ab546743255616e1d Mon Sep 17 00:00:00 2001 From: Gavin Inglis Date: Tue, 7 Apr 2020 15:52:41 -0400 Subject: [PATCH] add support for linux --- scripts/battery.sh | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/scripts/battery.sh b/scripts/battery.sh index c6ad825..7e4bb24 100755 --- a/scripts/battery.sh +++ b/scripts/battery.sh @@ -6,17 +6,49 @@ battery_percent() { - echo $(pmset -g batt | grep -Eo '[0-9]?[0-9]?[0-9]%') + # Check OS + case $(uname -s) in + Linux) + cat /sys/class/power_supply/BAT0/capacity + ;; + + Darwin) + echo $(pmset -g batt | grep -Eo '[0-9]?[0-9]?[0-9]%') + ;; + + CYGWIN*|MINGW32*|MSYS*|MINGW*) + # leaving empty - TODO - windows compatability + ;; + + *) + ;; + esac } battery_status() { - status=$(pmset -g batt | sed -n 2p | cut -d ';' -f 2) + # Check OS + case $(uname -s) in + Linux) + status=$(cat /sys/class/power_supply/BAT0/status) + ;; - if [ $status = 'discharging' ]; then + Darwin) + status=$(pmset -g batt | sed -n 2p | cut -d ';' -f 2) + ;; + + CYGWIN*|MINGW32*|MSYS*|MINGW*) + # leaving empty - TODO - windows compatability + ;; + + *) + ;; + esac + + if [ $status = 'discharging' ] || [ $status = 'Discharging' ]; then echo '' else - echo 'AC ' + echo 'AC ' fi }