summaryrefslogtreecommitdiffstatshomepage
path: root/tmux/.local/bin/tmux-projectr
blob: d32aa9cdf0309043991851e671e4fbc45160748b (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
#!/bin/sh

if [ "$#" -eq 0 ]; then
	GIT_PREVIEW='onefetch --include-hidden {}'
	DIR_PREVIEW='tree -CL 3 --gitignore --noreport --filelimit=50 {}'
	PREVIEW_CMD="($GIT_PREVIEW || $DIR_PREVIEW) 2>/dev/null"

	# shellcheck disable=2046
	set -- $(
		projectr -mgt ~/src ~/pkg ~/dkr ~/doc -P ~/.dotfiles --exclude-cwd |
			fzf-tmux "$FZF_TMUX_OPTS" -- --tac --select-1 --exit-0 -d/ --with-nth 4.. \
				--bind 'focus:transform-header(echo {})' \
				--preview-window='right,75%,<80(hidden)' --preview="$PREVIEW_CMD"
	)

	test -n "$1" || exit 0
fi

if [ -z "$2" ]; then
	if [ -z "$(tmux list-sessions -f "#{==:#{session_path},$1}" -F '#S' 2>/dev/null)" ]; then
		tmux new-session -dc "$1"
	fi

	set -- "$1" "$(tmux list-sessions -f "#{==:#{session_path},$1}" -F '#S' 2>/dev/null)"
elif ! tmux has-session -t "$2" 2>/dev/null; then
	tmux new-session -dc "$1" -s "$2"
fi

if [ -z "$TMUX" ] && [ -z "$2" ]; then
	tmux attach-session -t "$2"
elif tmux has-session -t "$2" 2>/dev/null; then
	tmux switch-client -t "$2"
fi