From ffca40a27cf8dac3b398b47fd386bacfe3782914 Mon Sep 17 00:00:00 2001 From: candifloss Date: Tue, 10 Dec 2024 20:22:39 +0530 Subject: [PATCH] change battery script --- sh/values/battery.sh | 143 ++++++++++++++----------------------------- 1 file changed, 47 insertions(+), 96 deletions(-) diff --git a/sh/values/battery.sh b/sh/values/battery.sh index e58a7c3..bc29632 100755 --- a/sh/values/battery.sh +++ b/sh/values/battery.sh @@ -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\"}" \ No newline at end of file +# Output the status in JSON format +echo "{\"level\":\"$capacity\",\"status\":\"$status\",\"icon\":\"$icon\"}"