aboutsummaryrefslogtreecommitdiffstats
path: root/bin/cache-sample.sh
diff options
context:
space:
mode:
authorDanilo M. <danix@danix.xyz>2026-09-17 12:20:39 +0200
committerDanilo M. <danix@danix.xyz>2026-09-17 12:20:39 +0200
commit9a479e198142a486413270c0e5dec8ed4405e023 (patch)
tree3d6a8e8282a44516af0d358b29a290e4467942d4 /bin/cache-sample.sh
parent518b2969108ed3d728e562614c05e40400129240 (diff)
downloadconky-theme-udt-9a479e198142a486413270c0e5dec8ed4405e023.tar.gz
conky-theme-udt-9a479e198142a486413270c0e5dec8ed4405e023.zip
feat: add the disks and cache samplers
Diffstat (limited to 'bin/cache-sample.sh')
-rwxr-xr-xbin/cache-sample.sh33
1 files changed, 33 insertions, 0 deletions
diff --git a/bin/cache-sample.sh b/bin/cache-sample.sh
new file mode 100755
index 0000000..cac8911
--- /dev/null
+++ b/bin/cache-sample.sh
@@ -0,0 +1,33 @@
+#!/bin/bash
+# Sample ~/.cache sizes into a cache file, for widgets/cache.lua.
+#
+# du walks the tree and costs about 100ms warm, which is fine every 15 minutes
+# and unthinkable on a 2-second draw. The old conky config used the same
+# interval for the same reason.
+
+set -u
+
+CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/udt"
+CACHE="$CACHE_DIR/cachesize.txt"
+TARGET="${XDG_CACHE_HOME:-$HOME/.cache}"
+
+mkdir -p "$CACHE_DIR"
+
+TMP="$CACHE.tmp.$$"
+trap 'rm -f "$TMP"' EXIT
+umask 077
+
+# Total first, then the four largest children. `sort -h` compares human sizes
+# numerically; the Lua side converts them again because it needs the ratio to
+# the total, not just the order.
+{
+ du -sh "$TARGET" 2>/dev/null
+ du -sh "$TARGET"/* 2>/dev/null | sort -hr | head -4
+} > "$TMP"
+
+if [ ! -s "$TMP" ]; then
+ echo "cache-sample: du produced nothing" >&2
+ exit 1
+fi
+
+mv -f "$TMP" "$CACHE"