dmenu-scripts/bk-tmux-dmenu
2025-09-02 23:35:33 -07:00

46 lines
1.4 KiB
Bash
Executable file

#!/bin/sh
# dmenu-driven tmux session switcher/attacher for dwm + st
# deps: tmux, dmenu, st
set -eu
# List available sessions (quiet if no server/sessions)
sessions="$(tmux list-sessions -F '#S' 2>/dev/null || true)"
[ -n "${sessions}" ] || exit 0
# prefer rofi, fall back to dmenu
pick_session() {
if command -v rofi >/dev/null 2>&1; then
rofi -dmenu -i -p 'tmux sessions:' -lines 10 \
-kb-row-down "Down,Control+n,j" -kb-row-up "Up,Control+p,k"
else
dmenu -i -vi -c -bw 3 -l 10 -h 30 -F -p 'tmux sessions:'
fi
}
chosen="$(printf '%s\n' "$sessions" | pick_session)"
# DMENU="dmenu -i -vi -c -bw 3 -l 10 -h 30 -F -p 'tmux sessions:'"
# chosen="$(printf '%s\n' "$sessions" | eval "$DMENU")"
[ -n "$chosen" ] || exit 0
# 1) If this script is run from inside a tmux client, switch in-place.
if [ "${TMUX-}" ]; then
exec tmux switch-client -t "${chosen}"
fi
# 2) If any st-based tmux client exists, switch that client to the chosen session.
# (client_termname is usually 'st-256color' for st)
client_tty="$(
tmux list-clients -F '#{client_tty} #{client_termname}' 2>/dev/null \
| awk '$2 ~ /^st/ { print $1; exit }' || true
)"
if [ -n "${client_tty}" ]; then
# switch that client to the chosen session
tmux switch-client -c "${client_tty}" -t "${chosen}"
exit 0
fi
# 3) Fallback: launch a new st and attach.
exec st -e tmux attach -t "${chosen}"