2018-12-02 23:43:08 +00:00
|
|
|
#!/bin/bash
|
|
|
|
set -euo pipefail
|
|
|
|
|
2019-12-28 19:46:34 +00:00
|
|
|
# Check no support for battery (likely desktop PC)
|
|
|
|
acpi -b |& grep -q 'No support' && {
|
|
|
|
printf '\uf0e7\n'
|
|
|
|
exit 0
|
|
|
|
} || true
|
|
|
|
|
2018-12-11 17:14:50 +00:00
|
|
|
IFS=$'\n'
|
|
|
|
|
2018-12-02 23:43:08 +00:00
|
|
|
ac_conn=0
|
2018-12-11 17:14:50 +00:00
|
|
|
(acpi -a | grep -q on-line) && ac_conn=1
|
2018-12-02 23:43:08 +00:00
|
|
|
|
|
|
|
bat_cap=()
|
2018-12-11 17:14:50 +00:00
|
|
|
for line in $(acpi -b | grep -v 'unavailable' | sed -E 's/.*: ([^,]+), ([0-9]+)%(, ([0-9]+:[0-9]+|)|).*/\1\t\2\t\4/'); do
|
2018-12-03 14:22:12 +00:00
|
|
|
color='#ffffff'
|
2018-12-11 17:14:50 +00:00
|
|
|
|
|
|
|
state=$(echo "${line}" | cut -d $'\t' -f 1)
|
|
|
|
cap=$(echo "${line}" | cut -d $'\t' -f 2)
|
|
|
|
remain="($(echo "${line}" | cut -d $'\t' -f 3)) "
|
|
|
|
|
|
|
|
[[ ${state} == 'Full' ]] && remain=''
|
|
|
|
[[ ${remain} == '() ' ]] && remain='(??) '
|
2018-12-03 14:22:12 +00:00
|
|
|
|
|
|
|
[ ${cap} -lt 50 ] && color='#ffd966'
|
|
|
|
[ ${cap} -lt 25 ] && color='#dd0000'
|
|
|
|
|
2018-12-11 17:14:50 +00:00
|
|
|
bat_cap+=("<span color=\"${color}\">${cap}% ${remain}</span>")
|
2018-12-02 23:43:08 +00:00
|
|
|
done
|
|
|
|
|
2018-12-11 17:14:50 +00:00
|
|
|
IFS=' '
|
|
|
|
|
2018-12-02 23:43:08 +00:00
|
|
|
if [ ${ac_conn} -eq 0 ]; then
|
2019-12-28 19:46:34 +00:00
|
|
|
# PS not connected, on battery
|
|
|
|
printf '\uf241 %s\n' "${bat_cap[*]}"
|
2018-12-02 23:43:08 +00:00
|
|
|
else
|
2019-12-28 19:46:34 +00:00
|
|
|
# PS connected, on AC
|
|
|
|
printf '\uf376 %s\n' "${bat_cap[*]}"
|
2018-12-02 23:43:08 +00:00
|
|
|
fi
|