2020-03-14 22:42:06 +01:00
|
|
|
#!/usr/bin/env bash
|
2020-10-17 00:38:29 +02:00
|
|
|
# setting the locale, some users have issues with different locales, this forces the correct one
|
|
|
|
export LC_ALL=en_US.UTF-8
|
2020-05-27 00:30:17 +02:00
|
|
|
|
2022-05-11 10:53:37 +02:00
|
|
|
current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
|
|
|
source $current_dir/utils.sh
|
|
|
|
|
2020-05-21 17:32:20 +02:00
|
|
|
linux_acpi() {
|
2021-07-06 10:22:36 +02:00
|
|
|
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
|
2020-05-21 17:32:20 +02:00
|
|
|
}
|
2020-05-27 00:30:17 +02:00
|
|
|
|
2020-03-14 22:42:06 +01:00
|
|
|
battery_percent()
|
|
|
|
{
|
2021-07-06 10:22:36 +02:00
|
|
|
# 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
|
2020-03-14 22:42:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
battery_status()
|
|
|
|
{
|
2021-07-06 10:22:36 +02:00
|
|
|
# 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 ''
|
|
|
|
;;
|
2022-08-10 15:14:17 +02:00
|
|
|
high|Full)
|
2021-07-06 10:22:36 +02:00
|
|
|
echo ''
|
|
|
|
;;
|
2022-08-10 15:14:17 +02:00
|
|
|
charging|Charging)
|
2021-07-06 10:22:36 +02:00
|
|
|
echo 'AC'
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo 'AC'
|
|
|
|
;;
|
|
|
|
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
|
2020-03-14 22:42:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
main()
|
|
|
|
{
|
2021-10-17 02:29:28 +02:00
|
|
|
bat_label=$(get_tmux_option "@dracula-battery-label" "♥")
|
2021-07-06 10:22:36 +02:00
|
|
|
bat_stat=$(battery_status)
|
|
|
|
bat_perc=$(battery_percent)
|
|
|
|
|
|
|
|
if [ -z "$bat_stat" ]; then # Test if status is empty or not
|
2021-10-17 02:29:28 +02:00
|
|
|
echo "$bat_label $bat_perc"
|
2021-07-06 10:22:36 +02:00
|
|
|
elif [ -z "$bat_perc" ]; then # In case it is a desktop with no battery percent, only AC power
|
2021-10-17 02:29:28 +02:00
|
|
|
echo "$bat_label $bat_stat"
|
2021-07-06 10:22:36 +02:00
|
|
|
else
|
2021-10-17 02:29:28 +02:00
|
|
|
echo "$bat_label $bat_stat $bat_perc"
|
2021-07-06 10:22:36 +02:00
|
|
|
fi
|
2020-03-14 22:42:06 +01:00
|
|
|
}
|
|
|
|
|
2020-03-15 00:57:01 +01:00
|
|
|
#run main driver program
|
2020-03-14 22:42:06 +01:00
|
|
|
main
|
|
|
|
|