testsAndMisc/linux_configuration/scripts/periodic_background/i3-configuration/i3blocks/battery_status.sh
Krzysztof kuhy Rudnicki db6276b3ff refactor(linux_configuration): move remaining dirs + scripts/ to meta/
- Move fresh-install/ → scripts/single_use/fresh-install/
- Move hosts/ → scripts/periodic_background/hosts/
- Move i3-configuration/ → scripts/periodic_background/i3-configuration/
- Delete linux_configuration/LaTeX/, nix-poc/, report/ (dead dirs)
- Move repo-root scripts/ → meta/scripts/
- Update root .pre-commit-config.yaml: scripts/ → meta/scripts/ (9 entries)
- Update run.sh ARTIFACT_INIT_SCRIPT to meta/scripts/
- Update fresh-install/main.sh: hosts/install.sh + i3-configuration/install.sh paths
- Update check_python_location.sh: add meta/scripts/ to exception list
- Fix midnight flakiness in test_recent_workout_returns_true: use timezone-aware
  local noon instead of now-1h to avoid SQL date() boundary issues
2026-05-15 00:53:01 +02:00

63 lines
1.6 KiB
Bash
Executable File

#!/bin/bash
# i3blocks battery indicator, zero-fork per invocation.
#
# Reads /sys/class/power_supply directly instead of forking `acpi | awk`.
# Uses only bash builtins (read, printf, arithmetic, parameter expansion).
set -u
# Nerd Font glyph: battery-full icon (U+F240).
ICON=$'\uf240'
bat=
for d in /sys/class/power_supply/BAT*/; do
[[ -d $d ]] && {
bat=$d
break
}
done
if [[ -z $bat ]]; then
# Desktop with no battery — emit empty block so i3bar hides it.
echo
exit 0
fi
cap='N/A'
[[ -r ${bat}capacity ]] && read -r cap < "${bat}capacity"
status=''
[[ -r ${bat}status ]] && read -r status < "${bat}status"
# Compute time remaining from energy_now/power_now (µWh / µW → hours).
# Falls back to charge_now/current_now on batteries that expose charge instead.
time_str=''
num=0
den=0
if [[ -r ${bat}energy_now && -r ${bat}power_now ]]; then
read -r num < "${bat}energy_now"
read -r den < "${bat}power_now"
elif [[ -r ${bat}charge_now && -r ${bat}current_now ]]; then
read -r num < "${bat}charge_now"
read -r den < "${bat}current_now"
fi
if ((den > 0 && num > 0)); then
total_min=$((num * 60 / den))
printf -v time_str '%02d:%02d' "$((total_min / 60))" "$((total_min % 60))"
fi
color='#50FA7B'
if [[ $cap =~ ^[0-9]+$ ]]; then
if ((cap < 15)); then
color='#FF5555'
elif ((cap < 35)); then
color='#F1FA8C'
fi
fi
[[ $status == Charging ]] && color='#8BE9FD'
printf -v body '%s %s%%' "$ICON" "$cap"
[[ -n $time_str ]] && body+=", $time_str"
[[ $status == Charging ]] && body+=', '
printf '<span color="%s">%s</span>\n' "$color" "$body"