forked from 27medkamal/tmux-session-wizard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session-wizard.sh
executable file
·45 lines (39 loc) · 1.29 KB
/
session-wizard.sh
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
#!/usr/bin/env bash
# Usage: t <optional zoxide-like dir, relative or absolute path>
# If no argument is given, a combination of existing sessions and a zoxide query will be displayed in a FZF
__fzfcmd() {
[ -n "$TMUX_PANE" ] && { [ "${FZF_TMUX:-0}" != 0 ] || [ -n "$FZF_TMUX_OPTS" ]; } &&
echo "fzf-tmux ${FZF_TMUX_OPTS:--d${FZF_TMUX_HEIGHT:-40%}} -- " || echo "fzf"
}
# Parse optional argument
if [ "$1" ]; then
# Argument is given
eval "$(zoxide init bash)"
RESULT=$(z $@ && pwd)
else
# No argument is given. Use FZF
RESULT=$((tmux list-sessions -F "#{session_name}: #{session_windows} window(s)\
#{?session_grouped, (group ,}#{session_group}#{?session_grouped,),}\
#{?session_attached, (attached),}"; zoxide query -l) | $(__fzfcmd) --reverse)
if [ -z "$RESULT" ]; then
exit 0
fi
fi
# Get or create session
if [[ $RESULT == *":"* ]]; then
# RESULT comes from list-sessions
SESSION=$(echo $RESULT | awk '{print $1}')
SESSION=${SESSION//:/}
else
# RESULT is a path
SESSION=$(basename "$RESULT" | tr . - | tr ' ' - | tr ':' - | tr '[:upper:]' '[:lower:]')
if ! tmux has-session -t=$SESSION 2> /dev/null; then
tmux new-session -d -s $SESSION -c "$RESULT"
fi
fi
# Attach to session
if [ -z "$TMUX" ]; then
tmux attach -t $SESSION
else
tmux switch-client -t $SESSION
fi