aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/tmux/.local/bin/tmux-sessionizer
blob: 7fe14d70bdf590c795f76368259567d50e386e44 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/bin/sh
# shellcheck disable=2016,2089

PROJECT_DIR="$HOME/src"
INITIAL_DIRS="$HOME/.dotfiles"
{ PREVIEW_CMD=$(cat); } <<'EOF'
selection={}
query=$(printf %s\\n {q} | sed 's|/$||')
width=$(((($(tput cols) * 3) + (4 - 1)) / 4))
ofargs=$([ "$width" -gt 80 ] || echo "--show-logo=never")
if [ -n "$selection" ]; then
  timeout 1s onefetch --hidden $ofargs $selection 2>/dev/null ||
  ([ -e $selection/README.md ] && timeout 1s glow --local --style=dark $selection/README.md) ||
  timeout 1s exa --tree --git-ignore --level=3 --icons $selection
elif [ -n "$query" ]; then
  timeout 3s hut git show --repo "$query" 2>/dev/null ||
  timeout 3s gh repo view "$query" 2>/dev/null
fi
EOF

if [ "$#" -eq 0 ]; then
	DIRS=""

	for initial_dir in $INITIAL_DIRS; do
		if [ -d "$initial_dir" ]; then
			DIRS="$DIRS $(stat -c %Y:"$initial_dir" "$initial_dir")"
		fi
	done

	for project in "$PROJECT_DIR"/*; do
		if [ -d "$project" ]; then
			DIRS="$DIRS $(stat -c %Y:"$project" "$project")"
		fi
	done

	for session in "$XDG_DATA_HOME/nvim/sessions"/*; do
		entry="$(printf %s\\n "$session" | sed "s|^$XDG_DATA_HOME/nvim/sessions/||" | sed 's#%{2}|_{2}#/#g')"
		if [ -d "$entry" ]; then
			DIRS="$DIRS $(stat -c %Y:"$entry" "$session")"
		fi
	done

	if [ -n "$TMUX" ]; then
		attached="$(tmux list-sessions -F '#{?session_attached,#{session_path},}' | sed '/^$/d')"
	fi

	DIRS="$(printf %s\\n "$DIRS" | xargs -I{} -d ' ' sh -c '
  timestamp="$(printf %s "{}" | cut -d":" -f1)"
  dir_path="$(printf %s "{}" | cut -d":" -f2)"
  source="$(printf %s "{}" | cut -d":" -f2)"

	if [ -e "$dir_path/.git" ]; then
    git_timestamp="$(git -C "$dir_path" --no-pager log -1 --all --format="%at" 2>/dev/null || stat -c %Y "$dir_path"/.git)"
  fi

  if [ $git_timestamp -gt $timestamp ]; then
    printf %s:%s\\n $git_timestamp $dir_path
  else
    printf %s\\n {}
  fi
  ' | sort -r | sort -t':' -r -k2 | uniq -s10 | sort -r | cut -d':' -f2 | sed "s#^$attached\$##g" | sed '/^$/d')"

	# shellcheck disable=2046
	set -- $(printf %s\\n "$DIRS" | fzf-tmux -p -- --multi --print-query -d/ --with-nth -1 \
		--preview-window=right,75% --preview="$PREVIEW_CMD" | tr -s '\n' ' ')
fi

while [ $# -gt 0 ]; do
	session_path="$1"
	shift
	if [ ! -d "$session_path" ]; then
		repo=$(printf %s "$session_path" | sed 's/.git$//' | sed 's|/$||')
		remotes="$repo https://git.sr.ht/$repo https://github.com/$repo"
		for remote in $remotes; do
			if timeout 3s git ls-remote "$remote" CHECK_GIT_REMOTE_URL_REACHABILITY; then
				session_path="$HOME/src/$(basename "$remote")"
				git clone "$remote" "$session_path"
				break
			fi
		done
	fi

	if [ ! -d "$session_path" ]; then
		continue
	fi

	name=$(basename "$session_path" | tr . _)
	if ! tmux has-session -t="$name" 2>/dev/null; then
		tmux new-session -ds "$name" -c "$session_path"
	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