Files
NixOS-Configs/home/hypr/quickshell/scripts/get_battery.sh

17 lines
656 B
Bash
Executable File

#!/usr/bin/env bash
# Read battery state
bat_path=$(find /sys/class/power_supply/ -name "BAT*" 2>/dev/null | head -n 1)
if [ -n "$bat_path" ] && [ -d "$bat_path" ]; then
capacity=$(cat "$bat_path/capacity" 2>/dev/null || echo 100)
status=$(cat "$bat_path/status" 2>/dev/null || echo "Full")
is_charging=false
if [ "$status" = "Charging" ] || [ "$status" = "Full" ]; then
is_charging=true
fi
echo "{\"present\": true, \"percent\": $capacity, \"status\": \"$status\", \"charging\": $is_charging}"
else
# Desktop without battery
echo "{\"present\": false, \"percent\": 100, \"status\": \"AC\", \"charging\": true}"
fi