home · contact · privacy
Improve status script. master
authorChristian Heller <c.heller@plomlompom.de>
Mon, 17 Mar 2025 22:04:08 +0000 (23:04 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Mon, 17 Mar 2025 22:04:08 +0000 (23:04 +0100)
testing/home/t490s/.nonpath_bins/status.sh

index e3bb5f958167728428337a3de056157ebabb6a6b..8822b2126455e0c17998829baca837b1172ad92d 100755 (executable)
@@ -3,44 +3,58 @@
 
 SYSCLASS_DIR=/sys/class
 
-COL_WHITE=ffffff
-COL_RED=ff0000
-COL_YELLOW=ffff00
-COL_GREEN=00ff00
+COL_WHITE=dddddd
+COL_RED=dd0000
+COL_YELLOW=dddd00
+COL_GREEN=00dd00
 COL_GREY=aaaaaa
 
-print_bar_element() {
-    if [ -z "$3" ]; then
-        SEP_WIDTH=20
-    else
-        SEP_WIDTH="$3"
+print_bar_block() {
+    printf '{"full_text": "%s", ' "$1"
+    if [ ! -z "$2" ]; then
+        printf '"color": "%s", ' "$2"
     fi
-    if [ -z "$2" ]; then
-        COLOR="${COL_WHITE}"
-    else
-        COLOR="$2"
+    if [ "$3" = "0" ]; then
+        printf '"separator": false,\n'
+        printf '"separator_block_width": 0,'
     fi
-    printf '{"full_text": "%s", ' "$1"
-    printf '"color": "%s", ' "${COLOR}"
-    printf '"separator_block_width": %d},\n' "${SEP_WIDTH}"
+    printf '},\n'
 }
 
 print_online() {
-    show_conn() {
+    print_conn() {
+        print_bar_block "$(echo $1 | cut -c1):" "${COL_GREY}" 0
         COLOR="${COL_GREEN}"
-        IP=$(ip --brief addr show | grep "$2" | sed -E 's/ +/\t/g' | cut -f3 | cut -f1 -d'/')
+        IP=$(ip --brief addr show | grep "$1" | sed -E 's/ +/\t/g' | cut -f3 | cut -f1 -d'/')
         if [ -z "${IP}" ]; then
-           IP=-
-           COLOR="${COL_RED}"
+           IP='-'
+           COLOR="${COL_GREY}"
         fi
-        print_bar_element "$1:${IP}" "${COLOR}" "$3"
+        print_bar_block "${IP}" "${COLOR}"
     }
-    show_conn E enp 10
-    show_conn W wlp
+    print_conn enp
+    print_conn wlp
 }
 
 print_battery() {
     BAT_DIR="${SYSCLASS_DIR}/power_supply/BAT0"
+
+    print_percent() {
+        cat_energy() {
+            cat "${BAT_DIR}/energy_$1"
+        }
+        PERCENT=$(printf "%d" $(calc "100 * $(cat_energy $1) // $(cat_energy $2)"))
+        COLOR="${COL_GREEN}"
+        if [ "${PERCENT}" -lt 25 ]; then
+            COLOR="${COL_RED}"
+        elif [ "${PERCENT}" -lt 65 ]; then
+            COLOR="${COL_YELLOW}"
+        fi
+        print_bar_block "${PERCENT}" "${COLOR}" "$3"
+    }
+
+    print_percent now full 0
+
     COLOR="${COL_WHITE}"
     STATUS=$(cat "${BAT_DIR}/status")
     CHARGING=?
@@ -48,20 +62,14 @@ print_battery() {
         CHARGING=-
     elif [ "${STATUS}" = "Charging" ]; then
         CHARGING=^
-       COLOR="${COL_GREEN}"
+        COLOR="${COL_GREEN}"
     elif [ "${STATUS}" = "Discharging" ]; then
         CHARGING=v
-       COLOR="${COL_YELLOW}"
-    fi
-    percent_of() {
-        printf "%d" $(calc "100 * $(cat $1) // $(cat $2)")
-    }
-    NOW=$(percent_of "${BAT_DIR}/energy_now" "${BAT_DIR}/energy_full")
-    MAX=$(percent_of "${BAT_DIR}/energy_full" "${BAT_DIR}/energy_full_design")
-    if [ $NOW -lt 25 ]; then
-       COLOR="${COL_RED}"
+        COLOR="${COL_YELLOW}"
     fi
-    print_bar_element "${NOW}${CHARGING}${MAX}" "${COLOR}"
+    print_bar_block "${CHARGING}" "${COLOR}" 0
+
+    print_percent full full_design
 }
 
 print_temperature() {
@@ -73,14 +81,17 @@ print_temperature() {
     elif [ $TEMPERATURE_IN_C -gt 65 ]; then
         COLOR="${COL_YELLOW}"
     fi
-    print_bar_element "${TEMPERATURE_IN_C}°C" "${COLOR}"
+    print_bar_block "${TEMPERATURE_IN_C}" "${COLOR}" 0
+    print_bar_block '°C' "${COL_GREY}"
 }
 
 print_datetime() {
     DATE=$(date +'%Y-%m-%d')
-    TIME=$(date +'%H:%M:%S/%Z')
-    print_bar_element "${DATE}" "${COL_GREY}" 10
-    print_bar_element "${TIME}"
+    TIME=$(date +'%H:%M:%S')
+    TZ=$(date +'/%Z')
+    print_bar_block "${DATE} " "${COL_GREY}" 0
+    print_bar_block "${TIME}" "" 0
+    print_bar_block "${TZ}" "${COL_GREY}" 0
 }
 
 printf '{"version": 1}\n['