From f7599e9564b0101a1731987d95b32e081eea0a71 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Mon, 14 Nov 2022 22:22:43 -0500 Subject: [PATCH] Change the linux battery script to be triggered by changes in battery state via upower. --- .../sway/files/waybar_battery_linux.bash | 103 ++++++++---------- ansible/roles/sway/tasks/linux.yaml | 1 + 2 files changed, 48 insertions(+), 56 deletions(-) diff --git a/ansible/roles/sway/files/waybar_battery_linux.bash b/ansible/roles/sway/files/waybar_battery_linux.bash index 96274d3..9b4ffea 100644 --- a/ansible/roles/sway/files/waybar_battery_linux.bash +++ b/ansible/roles/sway/files/waybar_battery_linux.bash @@ -1,72 +1,63 @@ #!/usr/bin/env bash # -# Read battery status in FreeBSD +# Read battery status in Linux set -euo pipefail IFS=$'\n\t' DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -SLEEP_INTERVAL=${SLEEP_INTERVAL:-30} +batteries=$(upower --enumerate | grep BAT) -batteries=$(find /sys/class/power_supply -name 'BAT*') +function generate_battery_update { + local battery_path=$1 + local battery_status=$(upower --show-info "$battery_path") + local battery_state=$(grep 'state:' <<<"$battery_status" | awk '{print $2}') + local battery_percentage=$(grep 'percentage:' <<<"$battery_status" | awk '{print $2}') + battery_percentage=${battery_percentage%\%} + local time_remaining=$(grep 'time to empty:' <<<"$battery_status" | sed 's/\W*time to empty:\W*//g') + local battery_icon="" + local class="" -while true; do - text="" - tooltip="" - percentage="" - class="" - - while read bat; do - battery_now=$(cat "$bat/charge_now") - battery_capacity=$(cat "$bat/charge_full") - battery_percentage=$(( battery_now * 100 / battery_capacity )) - battery_status=$(cat "$bat/status") - seconds_remaining="-1" - - if [ $battery_status = "Charging" ]; then - if [ $battery_percentage -eq 100 ]; then - battery_icon="" - else - battery_icon="" - fi - elif [ $battery_percentage -le 20 ]; then - battery_icon="" - elif [ $battery_percentage -le 40 ]; then - battery_icon="" - elif [ $battery_percentage -le 60 ]; then - battery_icon="" - elif [ $battery_percentage -le 80 ]; then - battery_icon="" - elif [ $battery_percentage -lt 100 ]; then - battery_icon="" + if [ $battery_state = "charging" ]; then + if [ $battery_percentage -eq 100 ]; then + battery_icon="" else - battery_icon="" + battery_icon="" fi + elif [ $battery_percentage -le 20 ]; then + battery_icon="" + elif [ $battery_percentage -le 40 ]; then + battery_icon="" + elif [ $battery_percentage -le 60 ]; then + battery_icon="" + elif [ $battery_percentage -le 80 ]; then + battery_icon="" + elif [ $battery_percentage -lt 100 ]; then + battery_icon="" + else + battery_icon="" + fi - if [ $battery_percentage -le 15 ]; then - class="critical" - elif [ $battery_percentage -le 30 ]; then - class="warning" - fi - - if [ $seconds_remaining -eq -1 ]; then - time_remaining="unknown time remaining." - else - time_remaining=$(printf '%dh:%dm:%ds\n' $((seconds_remaining/3600)) $((seconds_remaining%3600/60)) $((seconds_remaining%60)) ) - fi - - text="$text${battery_percentage}% ${battery_icon}" - tooltip="$tooltip${time_remaining}" - percentage="$percentage${battery_percentage}" - done<<<"$batteries" - - - jq --unbuffered --compact-output <