MODIFY: updated battery-status script

This commit is contained in:
Sam 2024-07-21 14:46:11 +01:00
parent 3421360317
commit 06f6aa1ffd
1 changed files with 45 additions and 7 deletions

View File

@ -2,15 +2,53 @@
{
home.packages = [
(pkgs.writeShellScriptBin "battery-status" ''
status=$(${pkgs.acpi}/bin/acpi)
bat_name=$(echo "$status" | grep -o -e "^[Aa-Zz]*\s[0-9]:")
bat_status=$(echo $status | grep -o -e "[A-Z][a-z]*," | sed "s/,//")
bat_pct=$(echo $status | grep -o -e "[0-9]*%")
bat_time=$(echo $status | grep -o -e "[0-9]*:[0-9]*:[0-9]* [a-z].*")
# Get the current power consumption of the laptop battery
power=$(cat /sys/class/power_supply/BAT0/power_now)
power_watts=$(${pkgs.bc}/bin/bc <<< "scale=3; $power / 1000000")
echo -e "$bat_name ($bat_status)\nCharge: $bat_pct\nTime: $bat_time\nPower consumption: $power_watts W "
# Get the current battery charge capacity
energy=$(cat /sys/class/power_supply/BAT0/energy_now)
# Get the current battery status (charging or discharging)
battery_status=$(cat /sys/class/power_supply/BAT0/status)
# Calculate the time remaining until the battery is empty or full
if [ "$battery_status" == "Charging" ]; then
# Calculate the time remaining until the battery is full
hours=$(${pkgs.bc}/bin/bc <<< "scale=2; $power / $energy")
hours_int=$(${pkgs.bc}/bin/bc <<< "scale=0; $hours / 1")
minutes=$(${pkgs.bc}/bin/bc <<< "scale=0; 60 * ($hours - $hours_int)/1")
if [ "$hours_int" -gt "0" ]; then
# Show hours and minutes if time remaining is greater than or equal to 1 hour
echo "Full in: $hours_int hours $minutes minutes"
else
# Show minutes if time remaining is less than 1 hour
echo "Full in: $minutes minutes"
fi
elif [ "$battery_status" == "Discharging" ]; then
# Calculate the time remaining until the battery is empty
hours=$(${pkgs.bc}/bin/bc <<< "scale=2; $energy / $power")
hours_int=$(${pkgs.bc}/bin/bc <<< "scale=0; $hours / 1")
minutes=$(${pkgs.bc}/bin/bc <<< "scale=0; 60 * ($hours - $hours_int)/1")
if [ "$hours_int" -gt "0" ]; then
# Show hours and minutes if time remaining is greater than or equal to 1 hour
echo "Empty in: $hours_int hours $minutes minutes"
else
# Show minutes if time remaining is less than 1 hour
echo "Empty in: $minutes minutes"
fi
elif [ "$battery_status" == "Full" ]; then
echo "Battery full"
elif [ "$battery_status" == "Not charging" ]; then
echo "Battery full - not charging"
fi
echo "Power consumption: $power_watts W"
'')
];
}