-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmux-sessionizer
executable file
·99 lines (84 loc) · 3.37 KB
/
tmux-sessionizer
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
98
99
#!/usr/bin/env bash
# Modified version of ThePrimeagen's tmux-sessionizer to be more useful
# Starting point: https://github.com/ThePrimeagen/.dotfiles/blob/196aa23e0e2dfc6245f69c77bd2b05c8849f9908/bin/.local/bin/tmux-sessionizer
last_session_file=$HOME/.cache/.tmux_last_session
selected=$1
function initialize_tmux_windows() {
local session_name="$1"
local dir="$2"
if [[ $session_name != "vpn" ]]; then
# Window 1: nvim
tmux send-keys -t "${session_name}:1" "nvim" C-m
# Window 2: wrap lazygit
tmux new-window -t "${session_name}:2" -c "$dir"
tmux send-keys -t "${session_name}:2" "wrap lazygit" C-m
tmux rename-window -t "${session_name}:2" "git"
# Window 3: lf file browser
tmux new-window -t "${session_name}:3" -c "$dir"
tmux send-keys -t "${session_name}:3" "wrap lf" C-m
tmux rename-window -t "${session_name}:3" "lf"
# Window 4: just bash
tmux new-window -t "${session_name}:4" -c "$dir"
tmux select-window -t "${session_name}:1"
fi
}
function new_session() {
local session_name="$1"
local dir="$2"
tmux_running=$(pgrep tmux)
inside_tmux=$TMUX
if [[ -z $inside_tmux ]] && [[ -z $tmux_running ]]; then
tmux new-session -s "$session_name" -c "$dir" -d
initialize_tmux_windows "$session_name" "$dir"
tmux attach-session -t "$session_name"
exit 0
fi
if ! tmux has-session -t="$session_name" 2>/dev/null; then
tmux new-session -ds "$session_name" -c "$dir"
initialize_tmux_windows "$session_name" "$dir"
fi
if [[ -n $inside_tmux ]]; then
tmux display -p "#S" > "$last_session_file"
tmux switch-client -t "$session_name"
else
tmux attach-session -d -t "$session_name"
fi
exit 1
}
if [[ $# -gt 1 ]]; then
echo "ERROR! tmux-sessionizer accepts 0 or 1 argument, but $# args were given!" && exit 1
fi
if [[ "$selected" == "--last" ]]; then
TMUX_SESSIONIZER_ACTION=LAST_SESSION
fi
if [[ $# -eq 0 ]]; then
echo '' > /tmp/tmux-sessioniser-action
selected=$("$HOME/.config/vars/repos" | fzf --bind "ctrl-x:execute(echo LAST_SESSION > /tmp/tmux-sessioniser-action)+abort" --bind "ctrl-n:execute(echo NEW_SESSION_HERE > /tmp/tmux-sessioniser-action)+abort" )
TMUX_SESSIONIZER_ACTION=$(cat /tmp/tmux-sessioniser-action)
fi
echo '' > /tmp/tmux-sessioniser-action
if [[ "$TMUX_SESSIONIZER_ACTION" == "LAST_SESSION" ]]; then
if [[ ! -f "$last_session_file" ]]; then
echo "ERROR! No last session file found! ($last_session_file)" && exit 1
fi
session_name=$(cat "$last_session_file" | tr -s '\n' ' ' | tr -dc 'a-zA-Z0-9_-')
if [[ -z "$session_name" ]]; then
echo "ERROR! Could not determine session name from $last_session_file!" && exit 1
fi
if ! tmux has-session -t="$session_name" 2> /dev/null; then
echo "ERROR! Session $session_name does not exist! Removing $last_session_file..."
rm "$last_session_file"
exit 1
fi
new_session "$session_name" "$selected"
elif [[ $TMUX_SESSIONIZER_ACTION == "NEW_SESSION_HERE" ]]; then
session_name=$(pwd | sed 's/.*\///')
session_name=$(echo "$session_name" | tr -s '\n' ' ' | tr -dc 'a-zA-Z0-9_-')
new_session "$session_name" "$(pwd)"
else
if [[ -z $selected ]]; then
exit 1
fi
session_name=$(basename "$selected" | tr . _)
new_session "$session_name" "$selected"
fi