From 13e76d64b6858f7e0643ce96041c67ca1e495ecb 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 | 28 ++++++++++++++++++++++---- waybar/.config/waybar/config | 10 +++++++-- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/scripts/.local/share/scripts/cgm-value b/scripts/.local/share/scripts/cgm-value index 4ce559b..2d5b6ea 100755 --- a/scripts/.local/share/scripts/cgm-value +++ b/scripts/.local/share/scripts/cgm-value @@ -1,14 +1,33 @@ #!/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" @@ -16,3 +35,4 @@ check_connectivity() { } 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": "󰁔", + } } }