add caffine script

Amolith created

Change summary

dot_local/bin/executable_caffeine | 37 +++++++++++++++++++++++++++++++++
1 file changed, 37 insertions(+)

Detailed changes

dot_local/bin/executable_caffeine 🔗

@@ -0,0 +1,37 @@
+#!/usr/bin/env bash
+
+STATE_FILE="$XDG_CACHE_HOME/caffeine_state"
+PID_FILE="$XDG_CACHE_HOME/caffeine.pid"
+
+caffeine_pid() {
+    systemd-inhibit --list | awk '
+        $1 == "Caffeine" && $5 == "systemd-inhibit" { print $4 }
+    '
+}
+
+case $1 in
+toggle)
+    PID="$(caffeine_pid)"
+    if [ -n "$PID" ]; then
+        kill "$PID" 2>/dev/null
+        rm -f "$STATE_FILE" "$PID_FILE"
+        xset +dpms s on
+        notify-send "Caffeine off" "System might go to sleep."
+    else
+        systemd-inhibit --what=idle:sleep:shutdown --mode=block \
+            --who="Caffeine" --why="User-activated session protection" \
+            sleep infinity &
+        echo $! >"$PID_FILE"
+        touch "$STATE_FILE"
+        xset -dpms s off s noblank
+        notify-send "Caffeine on" "System will stay awake."
+    fi
+    ;;
+status)
+    if [ -n "$(caffeine_pid)" ]; then
+        echo "󰅶"
+    else
+        echo "󰾪"
+    fi
+    ;;
+esac