From 9d271de53e4aced2cf85efb4bbb1802946795fe6 Mon Sep 17 00:00:00 2001
From: tavo <tavo@tavo.one>
Date: Sat, 28 Dec 2024 14:54:46 -0600
Subject: [PATCH] handle err

---
 scripts/status/cpustat    | 15 +++++++++++++--
 scripts/status/layoutstat |  5 +++++
 scripts/status/micstat    |  5 +++++
 scripts/status/netstat    |  7 +++++++
 scripts/status/volstat    |  5 +++++
 5 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/scripts/status/cpustat b/scripts/status/cpustat
index 0eef91f..c3075f1 100755
--- a/scripts/status/cpustat
+++ b/scripts/status/cpustat
@@ -3,8 +3,19 @@
 # 'https://stackoverflow.com/questions/9229333/how-to-get-overall-cpu-usage-e-g-57-on-linux'
 # – Yeti Oct 21, 2017 at 9:21
 
-cat "$XDG_CACHE_HOME"/cpustat
+if [ -n "$XDG_CACHE_HOME" ] ; then
+    cache_file="$XDG_CACHE_HOME"/cpustat
+elif [ -n "$HOME" ] ; then
+    mkdir -p "$HOME/.local/cache"
+    cache_file="$HOME/.local/cache"/cpustat
+else
+    exit 1
+fi
+
+if [ -f "$cache_file" ] ; then
+    cat "$cache_file"
+fi
 
 load=$({ head -n1 /proc/stat;sleep 0.3;head -n1 /proc/stat; } | awk '/^cpu /{u=$2-u;s=$4-s;i=$5-i;w=$6-w}END{print int(0.5+100*(u+s+w)/(u+s+i+w))}')
 
-echo "$load%" > "$XDG_CACHE_HOME"/cpustat
+echo "$load%" > "$cache_file"
diff --git a/scripts/status/layoutstat b/scripts/status/layoutstat
index 511a6e2..f6811b7 100755
--- a/scripts/status/layoutstat
+++ b/scripts/status/layoutstat
@@ -3,5 +3,10 @@
 
 layout="$(setxkbmap -query | awk '/layout/{ print $2 }' | sed 's/latam/la/')"
 
+if [ "$?" != 0 ] ; then
+    echo "Failed to get layout"
+    exit 1
+fi
+
 echo "$layout"
 # notify-send -u low --replace-id=12 "󰌌 Layout" "$layout"
diff --git a/scripts/status/micstat b/scripts/status/micstat
index 664ed10..dbb5ce1 100755
--- a/scripts/status/micstat
+++ b/scripts/status/micstat
@@ -4,6 +4,11 @@
 
 info=$(amixer get Capture | grep -m 1 '[0-9]*%')
 
+if [ "$?" != 0 ] ; then
+    echo "Failed to get Master volume"
+    exit 1
+fi
+
 if [ -z "${info##*\[off\]}" ] ; then
     echo "󰍭 ──────────"
     exit 0
diff --git a/scripts/status/netstat b/scripts/status/netstat
index 2a9fe29..17284ef 100755
--- a/scripts/status/netstat
+++ b/scripts/status/netstat
@@ -22,11 +22,18 @@ int() { # Type of interface & status
     # If down interfaces, exit with icon
     grep -xq 'down' /sys/class/net/w*/operstate 2>/dev/null && echo "󰤮" && return 0
     grep -xq 'down' /sys/class/net/e*/operstate 2>/dev/null && echo "󰲛" && return 0
+
+    return 1
 }
 
 # Run 'int'
 icon=$(int)
 
+if [ "$?" != 0 ] ; then
+    echo "Failed to get interface type"
+    exit 1
+fi
+
 # Check if a VPN is enabled
 vpn="$( \
     sed "s/.*//" /sys/class/net/tun*/operstate 2>/dev/null
diff --git a/scripts/status/volstat b/scripts/status/volstat
index c527006..e515568 100755
--- a/scripts/status/volstat
+++ b/scripts/status/volstat
@@ -4,6 +4,11 @@
 
 info=$(amixer get Master | grep -m 1 '[0-9]*%')
 
+if [ "$?" != 0 ] ; then
+    echo "Failed to get Master volume"
+    exit 1
+fi
+
 if [ -z "${info##*\[off\]}" ] ; then
     echo "󰖁 ──────────"
     exit 0