From 06f6aa1ffd66c5d970da90a034d52a8a0db804df Mon Sep 17 00:00:00 2001 From: Sam Date: Sun, 21 Jul 2024 14:46:11 +0100 Subject: [PATCH] MODIFY: updated battery-status script --- .../desktop/dwm/scripts/battery-status.nix | 52 ++++++++++++++++--- 1 file changed, 45 insertions(+), 7 deletions(-) diff --git a/home/common/optional/desktop/dwm/scripts/battery-status.nix b/home/common/optional/desktop/dwm/scripts/battery-status.nix index 4159417..affee5d 100644 --- a/home/common/optional/desktop/dwm/scripts/battery-status.nix +++ b/home/common/optional/desktop/dwm/scripts/battery-status.nix @@ -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" '') ]; }