diff --git a/home-manager/programs/fish/functions/_two_function.fish b/home-manager/programs/fish/functions/_two_function.fish index 89b648529..04c8d4edc 100644 --- a/home-manager/programs/fish/functions/_two_function.fish +++ b/home-manager/programs/fish/functions/_two_function.fish @@ -1,17 +1,31 @@ function _two_function --description "Attach to tmux work session" - set -l restore (tmux list-keys 2>/dev/null | string match -rg '(/\S+/resurrect/scripts/restore\.sh)') - set -l restore $restore[1] - if test -n "$restore" - tmux run-shell "$restore" - end - if tmux has-session -t work 2>/dev/null if test -n "$TMUX" tmux switch-client -t work else tmux attach-session -t work end - else - tmuxinator start work + return + end + + # No work session — try resurrect restore + if test -f ~/.tmux/resurrect/last + # Start a detached session so the server is running + tmux new-session -d -s _restore 2>/dev/null + set -l restore (tmux list-keys 2>/dev/null | string match -rg '(/\S+/resurrect/scripts/restore\.sh)') + set -l restore $restore[1] + if test -n "$restore" + tmux run-shell "$restore" + end + # Clean up temp session if restore created work + if tmux has-session -t work 2>/dev/null + tmux kill-session -t _restore 2>/dev/null + tmux attach-session -t work + return + end + tmux kill-session -t _restore 2>/dev/null end + + # Fallback to tmuxinator + tmuxinator start work end diff --git a/spec/fish/_two_function_test.fish b/spec/fish/_two_function_test.fish index f23ce3891..77e9d1778 100644 --- a/spec/fish/_two_function_test.fish +++ b/spec/fish/_two_function_test.fish @@ -1,23 +1,35 @@ set fn (status dirname)/../../home-manager/programs/fish/functions source $fn/_two_function.fish -# ── no resurrect, no work session → tmuxinator start work ─ +# ── no resurrect file, no work session → tmuxinator start work ─ set log (mktemp) function tmux - if test "$argv[1]" = list-keys; echo ""; return; end if test "$argv[1]" = has-session; return 1; end + if test "$argv[1]" = new-session; return 0; end + if test "$argv[1]" = list-keys; echo ""; return; end + if test "$argv[1]" = kill-session; return 0; end end function tmuxinator; echo $argv >> $log; end +# Ensure no resurrect file interferes +set -l _bak "" +if test -f ~/.tmux/resurrect/last + set _bak (mktemp) + mv ~/.tmux/resurrect/last $_bak +end + _two_function +if test -n "$_bak" + mv $_bak ~/.tmux/resurrect/last +end + @test "missing work session starts via tmuxinator" (grep -c "start work" $log) -ge 1 # ── session exists, outside tmux → attach ──────────────── set log2 (mktemp) set -e TMUX function tmux - if test "$argv[1]" = list-keys; echo ""; return; end if test "$argv[1]" = has-session; return 0; end echo $argv >> $log2 end