From bcec83476cd397f8b8c2fedd32a1304a053689c1 Mon Sep 17 00:00:00 2001 From: coolneng Date: Wed, 24 May 2023 19:07:34 +0200 Subject: [PATCH] Add CGM direction arrow to Waybar --- scripts/.local/share/scripts/cgm-value | 30 +++++++++++++++++++++----- waybar/.config/waybar/config | 10 +++++++-- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/scripts/.local/share/scripts/cgm-value b/scripts/.local/share/scripts/cgm-value index 4ce559b..6de8e17 100755 --- a/scripts/.local/share/scripts/cgm-value +++ b/scripts/.local/share/scripts/cgm-value @@ -1,18 +1,38 @@ #!/bin/sh +NIGHTSCOUT_URL="https://nightscout.coolneng.duckdns.org" +CONNECTIVITY_URL="freebsd.org" + fetch_value() { CGM_VALUE=$(curl -s -H "api-secret: $(pass api/nightscout)" -H "accept: application/json" \ - "https://nightscout.coolneng.duckdns.org/api/v1/entries?count=1" | - grep -o '"sgv":[0-9]*' | cut -d ":" -f 2) - echo "$CGM_VALUE" + "$NIGHTSCOUT_URL/api/v1/entries?count=1") + GLUCOSE=$(echo "$CGM_VALUE" | grep -o '"sgv":[0-9]*' | cut -d ":" -f 2) + DIRECTION=$( + echo "$CGM_VALUE" | grep -o '"direction":"[a-zA-Z]*"' | cut -d ":" -f 2 | tr -d \" + ) +} + +format_output() { + case "$DIRECTION" in + *"Up"*) + printf "{\"text\": \"%s\",\"alt\": \"up\"}" "$GLUCOSE" + ;; + *"Down"*) + printf "{\"text\": \"%s\",\"alt\": \"down\"}" "$GLUCOSE" + ;; + "Flat") + printf "{\"text\": \"%s\",\"alt\": \"flat\"}" "$GLUCOSE" + ;; + esac } check_connectivity() { - if nc -zw1 freebsd.org 443; then + if nc -zw1 "$CONNECTIVITY_URL" 443; then fetch_value else - echo "NA" + echo "{\"text\": \"NA\"}" fi } check_connectivity +format_output diff --git a/waybar/.config/waybar/config b/waybar/.config/waybar/config index d73cf00..8e71ecb 100644 --- a/waybar/.config/waybar/config +++ b/waybar/.config/waybar/config @@ -80,8 +80,14 @@ "interval": 2 }, "custom/cgm-value": { - "format": "󱄦 {}", + "format": "󱄦 {}{icon} ", "exec": "$HOME/.local/share/scripts/cgm-value", - "interval": 2 + "interval": 2, + "return-type":"json", + "format-icons": { + "up": "󰁜", + "down": "󰁃", + "flat": "󰁔", + } } }