From db5a9ce92d8ca9e4581f35839625abd328e2fa82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9ana=20=E6=B1=9F?= Date: Thu, 29 May 2025 15:00:21 +0200 Subject: [PATCH] packages/tmux-sessionizer: tmux-last --- .../by-name/tmux-sessionizer/package.nix | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/nix/packages/by-name/tmux-sessionizer/package.nix b/nix/packages/by-name/tmux-sessionizer/package.nix index c6e22fa9..d222db0b 100644 --- a/nix/packages/by-name/tmux-sessionizer/package.nix +++ b/nix/packages/by-name/tmux-sessionizer/package.nix @@ -2,6 +2,7 @@ writeShellApplication, tmux, procps, + lib, symlinkJoin, }: let tmux-register-session = writeShellApplication { @@ -9,6 +10,12 @@ runtimeInputs = [tmux]; text = '' last=/tmp/TMUX_LAST + + # bail if tmux not running + if ! pgrep tmux >/dev/null 2>&1; then + exit 1 + fi + this="$(tmux display-message -p '#S')" if [ ! -f "$last" ] || [ "$(cat "$last")" != "$this" ]; then echo "$this" >"$last" @@ -43,6 +50,28 @@ fi ''; }; + + tmux-last = writeShellApplication { + name = "tmux-last"; + text = '' + tmux_last=/tmp/TMUX_LAST + if [ ! -f $tmux_last ]; then + echo "Last session is not yet set" + return 1 + fi + + session_name="$(cat $tmux_last)" + session_path="$session_name" + if [ ! -d "$session_path" ]; then + session_path="/tmp" + fi + + # bail if not in tmux, nothing to register + ${lib.getExe tmux-register-session} || : + ${lib.getExe tmux-maybe-create} "$session_name" "$session_path" + ${lib.getExe tmux-attach-or-switch} "$session_name" + ''; + }; in symlinkJoin { name = "tmux-sessionizer"; @@ -50,5 +79,6 @@ in tmux-register-session tmux-maybe-create tmux-attach-or-switch + tmux-last ]; }