home · contact · privacy
Improve battery status. master
authorChristian Heller <c.heller@plomlompom.de>
Wed, 12 Mar 2025 01:35:12 +0000 (02:35 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Wed, 12 Mar 2025 01:35:12 +0000 (02:35 +0100)
testing/home/t490s/.nonpath_bins/status.sh

index 256d9c09b959b7fa25b7d6d682c0c818a89abc44..f65d1df6b6cbb3a7a67453404de3b87e0417bad2 100755 (executable)
@@ -1,11 +1,34 @@
 #!/bin/sh
 # see sway-bar(5) and swaybar-protocol(7)
 KILO=1000
 #!/bin/sh
 # see sway-bar(5) and swaybar-protocol(7)
 KILO=1000
+
+BAT_DIR=/sys/class/power_supply/BAT0
+
+print_battery () {
+    COLOR=ffffff
+    printf '    {"separator_block_width": 20, "full_text": "'
+    STATUS=$(cat "${BAT_DIR}/status")
+    if [ "${STATUS}" = "Not charging" ]; then
+        printf "_"
+    elif [ "${STATUS}" = "Charging" ]; then
+        printf "^"
+       COLOR=00ff00
+    elif [ "${STATUS}" = "Discharging" ]; then
+        printf "v"
+       COLOR=ffff00
+    else
+        printf "?"
+    fi
+    printf ":%d" $(calc "100 * $(cat ${BAT_DIR}/energy_full) // $(cat ${BAT_DIR}/energy_full_design)")
+    printf ":%d" $(calc "100 * $(cat ${BAT_DIR}/energy_now) // $(cat ${BAT_DIR}/energy_full)")
+    printf '", "color": "%s"},\n' "${COLOR}"
+}
+
 printf '{ "version": 1 }\n[\n' 
 while true; do
   printf '  [\n'
   printf '    {"separator_block_width": 20, "full_text": "%s"},\n' "$(ip -4 addr show scope global | grep -oP '(?<=inet\s)\d+(\.\d+){3}')"
 printf '{ "version": 1 }\n[\n' 
 while true; do
   printf '  [\n'
   printf '    {"separator_block_width": 20, "full_text": "%s"},\n' "$(ip -4 addr show scope global | grep -oP '(?<=inet\s)\d+(\.\d+){3}')"
-  printf '    {"separator_block_width": 20, "full_text": "%s"},\n' "$(cat /sys/class/power_supply/BAT0/status)"
+  print_battery
   printf '    {"separator_block_width": 20, "full_text": "%d° C"},\n' $(calc "$(cat /sys/class/thermal/thermal_zone0/temp) // ${KILO}")
   printf '    {"separator_block_width": 20, "full_text": "%s"},\n' "$(date +'%Y-%m-%d %H:%M:%S/%Z')"
   printf '  ],\n'
   printf '    {"separator_block_width": 20, "full_text": "%d° C"},\n' $(calc "$(cat /sys/class/thermal/thermal_zone0/temp) // ${KILO}")
   printf '    {"separator_block_width": 20, "full_text": "%s"},\n' "$(date +'%Y-%m-%d %H:%M:%S/%Z')"
   printf '  ],\n'