diff --git a/dotfiles/lib/functions/codex_tmux b/dotfiles/lib/functions/codex_tmux index 70c26ad7..dd0ff1d8 100755 --- a/dotfiles/lib/functions/codex_tmux +++ b/dotfiles/lib/functions/codex_tmux @@ -3,7 +3,6 @@ function codex_tmux { base=$(basename "$PWD") name="$base" - if tmux has-session -t "$name" 2>/dev/null; then i=2 while tmux has-session -t "${name}-${i}" 2>/dev/null; do diff --git a/dotfiles/lib/functions/codex_tmux_resume b/dotfiles/lib/functions/codex_tmux_resume new file mode 100755 index 00000000..bcab68b0 --- /dev/null +++ b/dotfiles/lib/functions/codex_tmux_resume @@ -0,0 +1,32 @@ +#!/usr/bin/env sh + +function codex_tmux_resume { + base=$(basename "$PWD") + target=$(tmux list-sessions -F '#S' 2>/dev/null | awk -v base="$base" ' + $0 == base { print; found=1; exit } + { + prefix = base "-" + if (substr($0, 1, length(prefix)) == prefix) { + rest = substr($0, length(prefix) + 1) + if (rest ~ /^[0-9]+$/) { + n = rest + 0 + if (n > max) { max = n; best = $0 } + } + } + } + END { if (found != 1 && best != "") print best } + ') + + if [ -n "$target" ]; then + if [ -n "$TMUX" ]; then + tmux switch-client -t "$target" + else + tmux attach-session -t "$target" + fi + return 0 + fi + + codex_tmux "$@" +} + +codex_tmux_resume "$@"