From 9301bb1bb4972c3b98a551cd6651c12f50722678 Mon Sep 17 00:00:00 2001 From: Alexander Rosenberg Date: Thu, 16 May 2024 04:18:17 -0700 Subject: [PATCH] Make system-sleep-menu.sh more flexible --- system-menu/system-sleep-menu.sh | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/system-menu/system-sleep-menu.sh b/system-menu/system-sleep-menu.sh index 0d101ea..44d78d7 100755 --- a/system-menu/system-sleep-menu.sh +++ b/system-menu/system-sleep-menu.sh @@ -1,6 +1,10 @@ #!/usr/bin/env zsh -pgrep swayidle && swayidle_state="Enabled" || swayidle_state="Disabled" +local CACHE_PATH="${HOME}/.cache/system-sleep-menu" + +pgrep swayidle >/dev/null 2>&1 \ + && swayidle_state="Enabled" \ + || swayidle_state="Disabled" choice="$(fuzzel --index -d -p "Cur: ${swayidle_state} > " <<'EOF' Enable @@ -12,9 +16,23 @@ EOF case "${choice}" in 0) - swayidle -w & + if [[ "${swayidle_state}" == 'Disabled' ]]; then + local cmd_cache=(${(0)"$(<"${CACHE_PATH}")"}) + ( + set -x + cd "${cmd_cache[1]}" + eval "${cmd_cache[2]}" & + ) + fi ;; 1) - pkill swayidle + pgrep_output=(${(ps: :)"$(pgrep -fa swayidle)"}) + if [[ "${swayidle_state}" = 'Enabled' ]]; then + local si_pid="${pgrep_output[1]}" + local si_pwd="$(readlink "/proc/${si_pid}/cwd")" + local si_cmd="${pgrep_output:1}" + printf '%s\0%s' "${si_pwd}" "${si_cmd}" >"${CACHE_PATH}" + pkill swayidle + fi ;; esac