change battery script

This commit is contained in:
Candifloss 2024-12-10 20:22:39 +05:30
parent 5f80ac56f3
commit ffca40a27c

View File

@ -1,102 +1,53 @@
#!/bin/bash #!/bin/bash
c=$(cat /sys/class/power_supply/BAT0/capacity); # Define the battery directory
s=$(cat /sys/class/power_supply/BAT0/status); battery_dir="/sys/class/power_supply/BAT0"
s=${s//\ /}
s=${s,,}
if [[ $c -eq 0 ]]; then # Read relevant battery info
cl="empty"; capacity=$(cat "$battery_dir/capacity")
elif [[ $c -le 9 ]]; then capacity_level=$(cat "$battery_dir/capacity_level")
cl="critical"; status=$(cat "$battery_dir/status")
elif [[ $c -le 20 ]]; then
cl="vlow"; # Normalize the values (remove spaces and convert to lowercase)
elif [[ $c -le 40 ]]; then status="${status//[[:space:]]/}" # Remove spaces
cl="low"; status="${status,,}" # Convert to lowercase
elif [[ $c -le 65 ]]; then capacity_level="${capacity_level//[[:space:]]/}"
cl="medium"; capacity_level="${capacity_level,,}"
elif [[ $c -le 100 ]]; then
cl="high"; # Define icons for various statuses and capacity levels
elif [[ $c -eq 100 ]]; then declare -A icons
cl="full";
# 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
else else
cl="unknown"; icon="❓" # Fallback icon if the status doesn't match known values
fi fi
case "$s" in # Output the status in JSON format
"unknown") echo "{\"level\":\"$capacity\",\"status\":\"$status\",\"icon\":\"$icon\"}"
ico=""
;;
"notcharging")
ico=""
;;
"charging")
s="$s $cl";
case "$cl" in
"empty")
ico=""
;;
"critical")
ico=""
;;
"vlow")
ico=""
;;
"low")
ico=""
;;
"medium")
ico=""
;;
"high")
ico=""
;;
"full")
ico=""
;;
"unknown")
ico=""
;;
*)
ico="B"
;;
esac
;;
"discharging")
s="$s $cl";
case "$cl" in
"empty")
ico=""
;;
"critical")
ico=""
;;
"vlow")
ico=""
;;
"low")
ico=""
;;
"medium")
ico=""
;;
"high")
ico=""
;;
"full")
ico=""
;;
"unknown")
ico=""
;;
*)
ico="B"
;;
esac
;;
*)
ico="B"
;;
esac
echo "{\"level\":\"$c\",\"status\":\"$s\",\"icon\":\"$ico\"}"