From 685272399d7dbc35aea6028d6741963399d84e3f Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Tue, 19 May 2026 13:01:19 -0500 Subject: feat(tmux-util): add go subcommand (attach-or-create) tmux-util go attaches to a session named if it exists, creates it otherwise. Behavior depends on whether the caller is already inside tmux: - Outside tmux: `tmux attach-session -t ` (existing) or `tmux new-session -s -c $PWD` (new). - Inside tmux (TMUX env set): `tmux switch-client -t ` (existing) or `tmux new-session -d -s -c $PWD` followed by `switch-client` (new). Attaching from inside tmux would nest sessions and break the outer view, so the inside path uses switch-client instead. The existence check uses `tmux has-session -t =` with the leading `=` to force exact-match. Without it, tmux does prefix matching, which would let `go foo` resolve to a session named `foobar`. I added 6 new tests covering both inside/outside-tmux paths, both create/attach paths, plus error handling for missing or empty name arguments. fake-tmux picked up handlers for new-session (mutates state), attach-session and switch-client (record-only), and the `=`-prefix form of has-session. Total suite: 32 tests, all green. --- dotfiles/common/.local/bin/tmux-util | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'dotfiles') diff --git a/dotfiles/common/.local/bin/tmux-util b/dotfiles/common/.local/bin/tmux-util index 2fa861d..4a7b596 100755 --- a/dotfiles/common/.local/bin/tmux-util +++ b/dotfiles/common/.local/bin/tmux-util @@ -35,6 +35,37 @@ Run with no arguments to print this help. EOF } +# ----------------------------------------------------------------------------- +# go +# ----------------------------------------------------------------------------- + +cmd_go() { + local name="${1:-}" + if [ -z "$name" ]; then + echo "tmux-util go: missing session name" >&2 + echo "Usage: tmux-util go " >&2 + return 2 + fi + + # `=name` forces exact match in `tmux has-session` (otherwise tmux does + # prefix matching, which would let `go foo` attach to a session named + # `foobar`). + if tmux has-session -t "=$name" 2>/dev/null; then + if [ -n "${TMUX:-}" ]; then + tmux switch-client -t "$name" + else + tmux attach-session -t "$name" + fi + else + if [ -n "${TMUX:-}" ]; then + tmux new-session -d -s "$name" -c "$PWD" + tmux switch-client -t "$name" + else + tmux new-session -s "$name" -c "$PWD" + fi + fi +} + # ----------------------------------------------------------------------------- # ls # ----------------------------------------------------------------------------- @@ -144,7 +175,10 @@ main() { ls) cmd_ls "$@" ;; - go|pick|find|rename) + go) + cmd_go "$@" + ;; + pick|find|rename) echo "tmux-util: subcommand '$sub' is not implemented yet" >&2 return 2 ;; -- cgit v1.2.3