From 34ab9004294e042cc9c5945112f426031efc2c86 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Wed, 12 Mar 2025 04:42:07 +0100
Subject: [PATCH] Improve status.sh.

---
 testing/home/t490s/.nonpath_bins/status.sh | 54 ++++++++++++++--------
 1 file changed, 35 insertions(+), 19 deletions(-)

diff --git a/testing/home/t490s/.nonpath_bins/status.sh b/testing/home/t490s/.nonpath_bins/status.sh
index f65d1df..acded06 100755
--- a/testing/home/t490s/.nonpath_bins/status.sh
+++ b/testing/home/t490s/.nonpath_bins/status.sh
@@ -1,36 +1,52 @@
 #!/bin/sh
 # see sway-bar(5) and swaybar-protocol(7)
 KILO=1000
+WHITE=ffffff
 
 BAT_DIR=/sys/class/power_supply/BAT0
 
-print_battery () {
-    COLOR=ffffff
-    printf '    {"separator_block_width": 20, "full_text": "'
+print_bar_element() {
+    printf '{"full_text": "%s", "separator_block_width": %d, "color": "%s"},\n' "$1" "$2" "$3"
+}
+
+print_online() {
+    print_bar_element "$(ip -4 addr show scope global | grep -oP '(?<=inet\s)\d+(\.\d+){3}')" 20 "${WHITE}"
+}
+
+print_battery() {
+    COLOR="${WHITE}"
     STATUS=$(cat "${BAT_DIR}/status")
+    CHARGING=?
     if [ "${STATUS}" = "Not charging" ]; then
-        printf "_"
+        CHARGING=_
     elif [ "${STATUS}" = "Charging" ]; then
-        printf "^"
+        CHARGING=^
 	COLOR=00ff00
     elif [ "${STATUS}" = "Discharging" ]; then
-        printf "v"
+        CHARGING=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}"
+    NOW=$(printf "%d" $(calc "100 * $(cat ${BAT_DIR}/energy_now) // $(cat ${BAT_DIR}/energy_full)"))
+    MAX=$(printf "%d" $(calc "100 * $(cat ${BAT_DIR}/energy_full) // $(cat ${BAT_DIR}/energy_full_design)"))
+    print_bar_element "${NOW}${CHARGING}${MAX}" 20 "${COLOR}"
+}
+
+print_temperature() {
+    print_bar_element $(calc "$(cat /sys/class/thermal/thermal_zone0/temp) // ${KILO}") 20 "${WHITE}"
+}
+
+print_datetime() {
+    print_bar_element "$(date +'%Y-%m-%d')" 10 "aaaaaa"
+    print_bar_element "$(date +'%H:%M:%S/%Z')" 0 "${WHITE}"
 }
 
-printf '{ "version": 1 }\n[\n' 
+printf '{"version": 1}\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}')"
-  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'
-  sleep 1
+    printf '['
+    print_online
+    print_battery
+    print_temperature
+    print_datetime
+    printf '],'
+    sleep 0.5
 done
-- 
2.30.2