#!/usr/bin/env sh
fmt='#{session_id}:|#S|(#{session_attached} attached)'
t="tmux switch-client -t"
[ -z "$TMUX" ] && t="tmux attach-session -t"
sess="$({ tmux display-message -p -F "$fmt" && tmux list-sessions -F "$fmt"; } \
| awk '!seen[$1]++' \
| column -t -s'|' \
| sk -q '$' --reverse --prompt 'switch session: ' -1 --preview "tmux-session-preview {}" \
| cut -d':' -f1)"
[ -z "$sess" ] && exit 1
$t "$sess"
# | while read w; do
# set W (tmux lsw -t"{}" -F'#{window_id}#{T:tree_mode_format}' | grep ^"$w")
# echo " ${W##$w}"
# end