summaryrefslogtreecommitdiffstatshomepage
path: root/tmux/.local/bin/tmux-ssh
blob: 0bbdc7e79b3c8e60e083da26a106d3d91c3ea204 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/bin/sh

#!/bin/sh
# shellcheck disable=2016,2089

if [ "$#" -eq 0 ]; then
	ATTACHED=$(tmux display -p '#{session_path}')
	lines=$(tmux display -p "#{pane_height}")
	columns=$(tmux display -p "#{pane_width}")
	WIDTH=$([ "$columns" -gt "20" ] && printf %s "20" || printf %s "90%")
	HEIGHT=$([ "$lines" -gt "15" ] && printf %s "15" || printf %s "90%")

	# 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 -p "$WIDTH,$HEIGHT" -- --multi --print-query -d/ --with-nth -1 |
			tr -s '\n' ' '
	) && [ $# -gt 1 ] && shift
fi

exit 0
while [ $# -gt 0 ]; do
	ssh_host="$1"
	shift

	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'
	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"
fi