-
Notifications
You must be signed in to change notification settings - Fork 14
/
session-wizard.sh
executable file
·76 lines (66 loc) · 2.28 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
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
#!/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"
}
for argument in "$@"; do
if [[ $argument != "-w" ]]; then
# An argument is given that is not -w: directory
dir=1
break
else
windows=1
fi
done
if [ $dir ]; then
eval "$(zoxide init bash)"
RESULT=$(z "$argument" && pwd)
else
if [ $windows ]; then
RESULT=$(tmux list-windows -a -F "#{session_last_attached} #{session_name}/#{window_name}\
#{?session_grouped, (group ,}#{session_group}#{?session_grouped,),}#{?session_attached,#{?window_active, (attached),},}")
else
RESULT=$(tmux list-sessions -F "#{session_last_attached} #{session_name}: #{session_windows} window(s)\
#{?session_grouped, (group ,}#{session_group}#{?session_grouped,),}#{?session_attached, (attached),}")
fi
# No argument is given. Use FZF
RESULT=$( ( echo "$RESULT" \
| sort -r | (if [ -n "$TMUX" ]; then grep -v " $(tmux display-message -p '#S'):"; else cat; fi) | cut -d' ' -f2-; zoxide query -l) | $(__fzfcmd) --reverse --print-query | tail -n 1)
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//:/}
if [[ $SESSION == *"/"* ]]; then
WINDOW=$(echo $SESSION | awk -F'/' '{ print $2 }')
SESSION=$(echo $SESSION | awk -F'/' '{ print $1 }')
WINDOW=$(echo "$WINDOW" | sed 's/ (attached)//') # remove (attached) if it's present
fi
else
# RESULT is a path
# Quit if directory does not exists
if [ ! -d "$RESULT" ]; then
exit 0
fi
# Promote rank in zoxide.
zoxide add "$RESULT"
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
if [ ! -z "$WINDOW" ]; then
tmux select-window -t $WINDOW
fi