From fcf11be42a2dcdf63616ff04468435921895249e Mon Sep 17 00:00:00 2001 From: Abhishek Keshri Date: Tue, 9 Aug 2022 06:28:35 +0530 Subject: [PATCH] Add battery.sh --- scripts/battery.sh | 129 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100755 scripts/battery.sh diff --git a/scripts/battery.sh b/scripts/battery.sh new file mode 100755 index 0000000..5af933f --- /dev/null +++ b/scripts/battery.sh @@ -0,0 +1,129 @@ +#!/usr/bin/env bash +# setting the locale, some users have issues with different locales, this forces the correct one +export LC_ALL=en_US.UTF-8 + +current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +source $current_dir/utils.sh + +linux_acpi() { + arg=$1 + BAT=$(ls -d /sys/class/power_supply/BAT* | head -1) + if [ ! -x "$(which acpi 2> /dev/null)" ];then + case "$arg" in + status) + cat $BAT/status + ;; + + percent) + cat $BAT/capacity + ;; + + *) + ;; + esac + else + case "$arg" in + status) + acpi | cut -d: -f2- | cut -d, -f1 | tr -d ' ' + ;; + percent) + acpi | cut -d: -f2- | cut -d, -f2 | tr -d '% ' + ;; + *) + ;; + esac + fi +} + +battery_percent() +{ + # Check OS + case $(uname -s) in + Linux) + percent=$(linux_acpi percent) + [ -n "$percent" ] && echo " $percent" + ;; + + Darwin) + echo $(pmset -g batt | grep -Eo '[0-9]?[0-9]?[0-9]%') + ;; + + FreeBSD) + echo $(apm | sed '8,11d' | grep life | awk '{print $4}') + ;; + + CYGWIN*|MINGW32*|MSYS*|MINGW*) + # leaving empty - TODO - windows compatability + ;; + + *) + ;; + esac +} + +battery_status() +{ + # Check OS + case $(uname -s) in + Linux) + status=$(linux_acpi status) + ;; + + Darwin) + status=$(pmset -g batt | sed -n 2p | cut -d ';' -f 2 | tr -d " ") + ;; + + FreeBSD) + status=$(apm | sed '8,11d' | grep Status | awk '{printf $3}') + ;; + + CYGWIN*|MINGW32*|MSYS*|MINGW*) + # leaving empty - TODO - windows compatability + ;; + + *) + ;; + esac + + case $status in + discharging|Discharging) + echo '' + ;; + high) + echo '' + ;; + charging) + echo '' + ;; + *) + echo '' + ;; + esac + ### Old if statements didn't work on BSD, they're probably not POSIX compliant, not sure + # if [ $status = 'discharging' ] || [ $status = 'Discharging' ]; then + # echo '' + # # elif [ $status = 'charging' ]; then # This is needed for FreeBSD AC checking support + # # echo 'AC' + # else + # echo 'AC' + # fi +} + +main() +{ + bat_label=$(get_tmux_option "@tmux2k-battery-label" "") + bat_stat=$(battery_status) + bat_perc=$(battery_percent) + + if [ -z "$bat_stat" ]; then # Test if status is empty or not + echo "$bat_label$bat_perc" + elif [ -z "$bat_perc" ]; then # In case it is a desktop with no battery percent, only AC power + echo "$bat_label $bat_stat" + else + echo "$bat_label $bat_stat$bat_perc" + fi +} + +#run main driver program +main +