tmux2k/scripts/battery.sh

135 lines
2.6 KiB
Bash
Raw Normal View History

2022-08-09 06:28:35 +05:30
#!/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
2023-03-29 14:52:27 +05:30
current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$current_dir"/utils.sh
2022-08-09 06:28:35 +05:30
linux_acpi() {
2023-03-29 14:52:27 +05:30
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
2022-08-09 06:28:35 +05:30
}
2023-03-29 14:52:27 +05:30
battery_percent() {
# Check OS
case $(uname -s) in
2022-08-09 06:28:35 +05:30
Linux)
2023-03-29 14:52:27 +05:30
percent=$(linux_acpi percent)
[ -n "$percent" ] && echo " $percent"
;;
2022-08-09 06:28:35 +05:30
Darwin)
2024-02-25 21:25:41 +05:30
echo $(pmset -g batt | grep -Eo '[0-9]?[0-9]?[0-9]%' | sed 's/%//g')
2023-03-29 14:52:27 +05:30
;;
2022-08-09 06:28:35 +05:30
FreeBSD)
2023-03-29 14:52:27 +05:30
echo $(apm | sed '8,11d' | grep life | awk '{print $4}')
;;
2022-08-09 06:28:35 +05:30
2023-03-29 14:52:27 +05:30
CYGWIN* | MINGW32* | MSYS* | MINGW*)
# leaving empty - TODO - windows compatability
;;
2022-08-09 06:28:35 +05:30
2023-03-29 14:52:27 +05:30
*) ;;
esac
2022-08-09 06:28:35 +05:30
}
2023-03-29 14:52:27 +05:30
battery_status() {
# Check OS
case $(uname -s) in
2022-08-09 06:28:35 +05:30
Linux)
2023-03-29 14:52:27 +05:30
status=$(linux_acpi status)
;;
2022-08-09 06:28:35 +05:30
Darwin)
2023-03-29 14:52:27 +05:30
status=$(pmset -g batt | sed -n 2p | cut -d ';' -f 2 | tr -d " ")
;;
2022-08-09 06:28:35 +05:30
FreeBSD)
2023-03-29 14:52:27 +05:30
status=$(apm | sed '8,11d' | grep Status | awk '{printf $3}')
;;
2022-08-09 06:28:35 +05:30
2023-03-29 14:52:27 +05:30
CYGWIN* | MINGW32* | MSYS* | MINGW*)
2024-02-29 18:29:21 +05:30
# leaving empty - TODO - windows compatibility
2023-03-29 14:52:27 +05:30
;;
2022-08-09 06:28:35 +05:30
2023-03-29 14:52:27 +05:30
*) ;;
2022-08-09 06:28:35 +05:30
2023-03-29 14:52:27 +05:30
esac
case $status in
discharging | Discharging)
echo ''
;;
2022-08-09 06:28:35 +05:30
high)
2023-03-29 14:52:27 +05:30
echo ''
;;
2022-08-09 06:28:35 +05:30
charging)
2023-03-29 14:52:27 +05:30
echo ''
;;
2022-08-09 06:28:35 +05:30
*)
2023-03-29 14:52:27 +05:30
echo ''
;;
esac
2023-11-15 08:17:52 +05:30
}
battery_label() {
bat_perc=$1
if [ "$bat_perc" -gt 90 ]; then
echo " "
elif [ "$bat_perc" -gt 75 ]; then
echo " "
elif [ "$bat_perc" -gt 50 ]; then
echo " "
elif [ "$bat_perc" -gt 25 ]; then
echo " "
elif [ "$bat_perc" -gt 10 ]; then
echo " "
else
2024-02-21 08:39:37 +05:30
echo "󱉝 "
2023-11-15 08:17:52 +05:30
fi
2022-08-09 06:28:35 +05:30
}
2023-03-29 14:52:27 +05:30
main() {
bat_stat=$(battery_status)
2024-02-29 18:29:21 +05:30
bat_perc=" $(battery_percent)"
2023-11-15 08:17:52 +05:30
bat_label=$(get_tmux_option "@tmux2k-battery-label" "$(battery_label "$bat_perc")")
2023-03-29 14:52:27 +05:30
2024-02-25 21:25:41 +05:30
if [ -z "$bat_stat" ]; then
2023-11-15 08:17:52 +05:30
echo "$bat_label $bat_perc%"
2024-02-25 21:25:41 +05:30
elif [ -z "$bat_perc" ]; then
2023-11-15 08:17:52 +05:30
echo "$bat_stat $bat_label"
2023-03-29 14:52:27 +05:30
else
2023-11-15 08:17:52 +05:30
echo "$bat_stat $bat_label $bat_perc%"
2023-03-29 14:52:27 +05:30
fi
2022-08-09 06:28:35 +05:30
}
main