summaryrefslogtreecommitdiff
path: root/bin/extra/tmux-sessionizer
blob: 34958e5c1b5dbf9bdac1b967a4015344a8d306f8 (plain) (blame)
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 1 ]; then
    selected="$1"
else
	pfx="$HOME/proj" 
 
 selected="$(find "$pfx" $pfx/handmade \
  -mindepth 1 -maxdepth 1 \( -path "$pfx/handmade/*" -o -path "$pfx/*" \) -a -type d -a \( -not -path "$pfx/handmade" \) |
  sed "s@$pfx/@@" | fzf)" 
    { [ "$selected" ] && [ -d "$pfx/$selected" ]; } || exit 1
fi

if [ -z "$selected" ]; then
    exit 0
fi

tmux_running="$(pgrep "tmux" | head -n1)"

if [ -z $TMUX ] && [ -z "$tmux_running" ]; then
    tmux new-session -s "$selected" -c "$pfx"/"$selected"
    exit 0
fi

if ! tmux has-session -t="$selected" 2> /dev/null; then
    tmux new-session -ds "$selected" -c "$pfx"/"$selected"
fi

if [ -z $TMUX ]; then
    tmux attach -t "$selected"
else
    tmux switch-client -t "$selected"
fi