#!/usr/bin/env bash set -Eeuo pipefail bat_dir="/sys/class/power_supply/BAT0" capacity=$(<"$bat_dir/capacity") status=$(<"$bat_dir/status") power=$(sensors -j 2>/dev/null | python3 -c ' import json, sys data = json.load(sys.stdin) for chip, fields in data.items(): if not chip.startswith("BAT"): continue power1 = fields.get("power1") if power1: print(round(next(iter(power1.values())), 1)) break ') text="${capacity}%" [[ -n "$power" ]] && text+=" (${power}W)" icons=("󰁺" "󰁼" "󰁾" "󰂀" "󰁹") if [[ "$status" == "Charging" ]]; then icon="󰂄" else idx=$((capacity * ${#icons[@]} / 101)) icon="${icons[$idx]}" fi class="normal" if [[ "$status" != "Charging" ]]; then if ((capacity <= 15)); then class="critical" elif ((capacity <= 30)); then class="warning" fi fi printf '{"text": "%s %s", "class": "%s", "tooltip": "%s, %s"}\n' \ "$icon" "$text" "$class" "$status" "${power:+${power}W}"