diff options
Diffstat (limited to 'tmux/.local/bin/tmux-ssh')
-rwxr-xr-x | tmux/.local/bin/tmux-ssh | 49 |
1 files changed, 29 insertions, 20 deletions
diff --git a/tmux/.local/bin/tmux-ssh b/tmux/.local/bin/tmux-ssh index 335e75c..b1875e2 100755 --- a/tmux/.local/bin/tmux-ssh +++ b/tmux/.local/bin/tmux-ssh @@ -4,30 +4,39 @@ # shellcheck disable=2016,2089 if [ "$#" -eq 0 ]; then - ATTACHED=$(tmux display -p '#{session_path}') + ATTACHED=$(tmux display -p '#S' 2>/dev/null) + HOST=$(tmux display -p '#{host}' 2>/dev/null) - # shellcheck disable=2046 - set -- $( - grep -P '^[Hh]ost ([^*]+)$' "$HOME/.ssh/config" | grep -Po ' \K(\w+)' | - sed "\|^$HOST\$|d" | sed "\|^${ATTACHED#SSH_}\$|d" | sort -u | - timestamp.sh --type="ssh" --format="{}:{1}" | sort -r | cut -d':' -f2 | - fzf-tmux "$FZF_TMUX_OPTS" -- --multi --print-query -d/ --with-nth -1 | - tr -s '\n' ' ' - ) && [ $# -gt 1 ] && shift + set -- "$( + ( + tmux display -p '#{?#{m:*ssh,#{socket_path}},localhost,}' 2>/dev/null + grep -P '^[Hh]ost ([^*]+)$' "$HOME/.ssh/config" | grep -Po ' \K(\w+)' | sed "\|^$HOST\$|d" | + sed "\|^${ATTACHED}\$|d" | + sort -u | + timestamp.sh --type="ssh" --format="{}:{1}" | + sort -r | + cut -d':' -f2 + ) | + sed '\|^$|d' | + fzf-tmux -p20%,20% -- --print-query -d/ --with-nth -1 | + tail -1 + )" fi -while [ $# -gt 0 ]; do - ssh_host="$1" - shift +if [ -z "$1" ]; then + exit 0 +fi - name="SSH_$ssh_host" - if ! tmux has-session -t "$name" 2>/dev/null; then - tmux new-session -ds "$name" ssh -t "$ssh_host" # 'tmux new -As $USER' +if [ "$1" = "localhost" ] || [ "$1" = "$HOST" ]; then + tmux detach -E 'tmux attach' +else + if ! tmux -L ssh has-session -t "$1"; then + tmux -L ssh new-session -ds "$1" ssh -t "$1" zsh -l -c tmux new -As "$USER" fi -done -if [ -z "$TMUX" ] && [ -z "$name" ]; then - tmux attach-session -t "$name" -elif tmux has-session -t="$name" 2>/dev/null; then - tmux switch-client -t "$name" + if [ -z "$TMUX" ]; then + tmux -L ssh attach -t "$1" + else + tmux detach -E "tmux -L ssh attach -t $1" + fi fi |