Candyrice/sh/values/power/battery.sh

54 lines
1.7 KiB
Bash
Executable File

#!/bin/bash
# Define the battery directory
battery_dir="/sys/class/power_supply/BAT0"
# 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
icon="󰂑" # Fallback icon if the status doesn't match known values
fi
# Output the status in JSON format
echo "{\"level\":\"$capacity\",\"status\":\"$status\",\"icon\":\"$icon\"}"