From 9855a42ae9e91d1b2af70d40635fc8a5d51b874a Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Tue, 11 Dec 2018 18:14:50 +0100 Subject: [PATCH] Rewrite to use acpi and show remaining time Signed-off-by: Knut Ahlers --- .config/i3blocks/battery | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/.config/i3blocks/battery b/.config/i3blocks/battery index 0956c71..fac1e13 100755 --- a/.config/i3blocks/battery +++ b/.config/i3blocks/battery @@ -1,22 +1,30 @@ #!/bin/bash set -euo pipefail +IFS=$'\n' + ac_conn=0 -for ac in $(find /sys/class/power_supply -type l -name 'A*'); do - [ $(cat "${ac}/online") -eq 1 ] && ac_conn=1 -done +(acpi -a | grep -q on-line) && ac_conn=1 bat_cap=() -for bat in $(find /sys/class/power_supply -type l -name 'B*'); do +for line in $(acpi -b | grep -v 'unavailable' | sed -E 's/.*: ([^,]+), ([0-9]+)%(, ([0-9]+:[0-9]+|)|).*/\1\t\2\t\4/'); do color='#ffffff' - cap=$(cat "${bat}/capacity") + + 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='(??) ' [ ${cap} -lt 50 ] && color='#ffd966' [ ${cap} -lt 25 ] && color='#dd0000' - bat_cap+=("${cap}%") + bat_cap+=("${cap}% ${remain}") done +IFS=' ' + if [ ${ac_conn} -eq 0 ]; then printf '\uf58d %s' "${bat_cap[*]}" else