66 lines
1.6 KiB
Bash
Executable File
66 lines
1.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Simple Waybar custom battery module using sysfs
|
|
# Outputs JSON: {"text":"...","tooltip":"..."} for Waybar's custom module
|
|
|
|
BAT_PATH="/sys/class/power_supply/BAT1"
|
|
AC_PATH="/sys/class/power_supply/ACAD"
|
|
|
|
read_file() {
|
|
local f="$1"
|
|
if [[ -r "$f" ]]; then
|
|
tr -d '\n' < "$f"
|
|
fi
|
|
}
|
|
|
|
if [[ ! -d "$BAT_PATH" ]]; then
|
|
echo '{"text":" N/A","tooltip":"Battery device not found (expected BAT1)"}'
|
|
exit 0
|
|
fi
|
|
|
|
capacity="$(read_file "$BAT_PATH/capacity")"
|
|
status="$(read_file "$BAT_PATH/status")"
|
|
present="$(read_file "$BAT_PATH/present")"
|
|
|
|
# Fallbacks
|
|
capacity="${capacity:-0}"
|
|
status="${status:-Unknown}"
|
|
present="${present:-0}"
|
|
|
|
# Choose icon based on capacity
|
|
# Nerd Font icons similar to Waybar's default sequence
|
|
# 0-10, 10-20, ... , 90-100
|
|
icons=( "" "" "" "" "" "" "" "" "" "" "" )
|
|
|
|
idx=$(( capacity / 10 ))
|
|
if (( idx < 0 )); then idx=0; fi
|
|
if (( idx > 10 )); then idx=10; fi
|
|
|
|
icon="${icons[$idx]}"
|
|
|
|
# If charging, override icon with plug
|
|
if [[ "$status" == "Charging" || "$status" == "Full" ]]; then
|
|
icon=""
|
|
fi
|
|
|
|
# AC online?
|
|
ac_online=""
|
|
if [[ -r "$AC_PATH/online" ]]; then
|
|
ac_online="$(read_file "$AC_PATH/online")"
|
|
fi
|
|
|
|
# Build tooltip details
|
|
tooltip="Status: ${status}"
|
|
tooltip+="\nCapacity: ${capacity}%"
|
|
if [[ -n "$ac_online" ]]; then
|
|
tooltip+="\nAC Online: ${ac_online}"
|
|
fi
|
|
|
|
# If battery is reported not present, show N/A
|
|
if [[ "$present" != "1" ]]; then
|
|
echo '{"text":" N/A","tooltip":"Battery not present"}'
|
|
exit 0
|
|
fi
|
|
|
|
# Emit JSON for Waybar
|
|
printf '{"text":"%s %s%%","tooltip":"%s"}\n' "$icon" "$capacity" "$tooltip"
|