summaryrefslogtreecommitdiff
path: root/mac/.config/sketchybar/plugins/memory.sh
diff options
context:
space:
mode:
authorTheSiahxyz <164138827+TheSiahxyz@users.noreply.github.com>2025-01-24 20:35:27 +0900
committerTheSiahxyz <164138827+TheSiahxyz@users.noreply.github.com>2025-01-24 20:35:27 +0900
commitc80a54e42b52ce297f0f2f71af23c562832025c7 (patch)
treedcce8bb977a770f473325d48f6f70b21d9818a40 /mac/.config/sketchybar/plugins/memory.sh
init
Diffstat (limited to 'mac/.config/sketchybar/plugins/memory.sh')
-rw-r--r--mac/.config/sketchybar/plugins/memory.sh49
1 files changed, 49 insertions, 0 deletions
diff --git a/mac/.config/sketchybar/plugins/memory.sh b/mac/.config/sketchybar/plugins/memory.sh
new file mode 100644
index 0000000..6917458
--- /dev/null
+++ b/mac/.config/sketchybar/plugins/memory.sh
@@ -0,0 +1,49 @@
+#!/bin/bash
+
+source "$CONFIG_DIR/globalstyles.sh"
+
+# Get total physical memory in bytes
+total_memory=$(sysctl -n hw.memsize)
+
+# Get memory page size in bytes
+page_size=$(vm_stat | grep "page size of" | awk '{print $8}' | sed 's/\.$//') # Correctly strip the period at the end
+
+# Get various memory statistics from vm_stat
+vm_stat=$(vm_stat)
+pages_free=$(echo "$vm_stat" | grep "Pages free:" | awk '{print $3}' | sed 's/\.$//') # Remove dot at the end
+pages_active=$(echo "$vm_stat" | grep "Pages active:" | awk '{print $3}' | sed 's/\.$//')
+pages_inactive=$(echo "$vm_stat" | grep "Pages inactive:" | awk '{print $3}' | sed 's/\.$//')
+pages_speculative=$(echo "$vm_stat" | grep "Pages speculative:" | awk '{print $3}' | sed 's/\.$//')
+pages_wired_down=$(echo "$vm_stat" | grep "Pages wired down:" | awk '{print $4}' | sed 's/\.$//')
+compressed_pages=$(echo "$vm_stat" | grep "Pages occupied by compressor:" | awk '{print $5}' | sed 's/\.$//')
+
+# Calculate total used memory pages
+total_used_pages=$((pages_active + pages_wired_down + compressed_pages))
+
+# Convert pages to bytes
+total_used_memory_bytes=$((total_used_pages * page_size))
+
+# Calculate memory used percentage as an integer
+USAGE=$((total_used_memory_bytes * 100 / total_memory))
+COUNT="$(memory_pressure | grep "System-wide memory free percentage:" | awk '{ val = 100 - $5; if (val < 10) printf("%1.0f\n", val); else printf("%02.0f\n", val) }')"
+
+COLOR=$RED
+
+case "$COUNT" in
+[5-6][0-9]) # 50-69%
+ COLOR=$YELLOW
+ ;;
+[3-4][0-9]) # 20-49%
+ COLOR=$GREEN
+ ;;
+[1-2][0-9]) # 10-19%
+ COLOR=$LAVENDER
+ ;;
+[0-9]) # 0-9%
+ COLOR=$WHITE
+ ;;
+esac
+
+sketchybar -m --set "$NAME" \
+ label="$COUNT / $USAGE%" \
+ icon.color=$COLOR