Candyrice/sh/values/power/battery.sh

54 lines
1.7 KiB
Bash
Raw Normal View History

2024-08-01 17:24:08 +00:00
#!/bin/bash
2024-12-10 14:52:39 +00:00
# Define the battery directory
battery_dir="/sys/class/power_supply/BAT0"
2024-08-01 17:24:08 +00:00
2024-12-10 14:52:39 +00:00
# Read relevant battery info
capacity=$(cat "$battery_dir/capacity")
capacity_level=$(cat "$battery_dir/capacity_level")
status=$(cat "$battery_dir/status")
# Normalize the values (remove spaces and convert to lowercase)
status="${status//[[:space:]]/}" # Remove spaces
status="${status,,}" # Convert to lowercase
capacity_level="${capacity_level//[[:space:]]/}"
capacity_level="${capacity_level,,}"
# Define icons for various statuses and capacity levels
declare -A icons
# Charging Icons (based on capacity level)
icons["charging_critical"]="⚡️"
icons["charging_low"]="🔋"
icons["charging_normal"]="🔋"
icons["charging_high"]="🔋"
icons["charging_full"]="🔌"
# Discharging Icons (based on capacity level)
icons["discharging_critical"]="⚡️"
icons["discharging_low"]="🔋"
icons["discharging_normal"]="🔋"
icons["discharging_high"]="🔋"
icons["discharging_full"]="🔋"
# Not Charging Icon (simplified to one icon)
notcharging_icon="🔋"
# Unknown Icon (simplified to one icon)
unknown_icon="❓"
# Determine the icon based on status and capacity level
if [[ "$status" == "charging" || "$status" == "discharging" ]]; then
icon_key="${status}_${capacity_level}"
icon="${icons[$icon_key]:-"❓"}" # Default to ❓ if no specific icon is found
elif [[ "$status" == "not charging" ]]; then
icon=$notcharging_icon
elif [[ "$status" == "unknown" ]]; then
icon=$unknown_icon
2024-08-01 17:24:08 +00:00
else
2024-12-10 14:52:39 +00:00
icon="❓" # Fallback icon if the status doesn't match known values
2024-08-01 17:24:08 +00:00
fi
2024-12-10 14:52:39 +00:00
# Output the status in JSON format
echo "{\"level\":\"$capacity\",\"status\":\"$status\",\"icon\":\"$icon\"}"