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
c=$(cat /sys/class/power_supply/BAT0/capacity);
s=$(cat /sys/class/power_supply/BAT0/status);
s=${s//\ /}
s=${s,,}
# Define the battery directory
battery_dir="/sys/class/power_supply/BAT0"
if [[ $c -eq 0 ]]; then
cl="empty";
elif [[ $c -le 9 ]]; then
cl="critical";
elif [[ $c -le 20 ]]; then
cl="vlow";
elif [[ $c -le 40 ]]; then
cl="low";
elif [[ $c -le 65 ]]; then
cl="medium";
elif [[ $c -le 100 ]]; then
cl="high";
elif [[ $c -eq 100 ]]; then
cl="full";
# 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
else
cl="unknown";
icon="❓" # Fallback icon if the status doesn't match known values
fi
case "$s" in
"unknown")
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\"}"
# Output the status in JSON format
echo "{\"level\":\"$capacity\",\"status\":\"$status\",\"icon\":\"$icon\"}"