-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtmux-attach.sh
executable file
·101 lines (94 loc) · 2.68 KB
/
tmux-attach.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/bash
# If we're not in a TMUX session lets attach to one or create a new one
function assessSession() {
read -p "Choose a Session: " session_number
enter=$(echo -e "\n")
if [[ $session_number = $enter ]]; then
SESSION="n"
else
for i in "${UNATTCHED_SESSIONS[@]}"
do
if [[ $i = $session_number ]]; then
SESSION=$session_number
case "$SESSION" in
"X")
SESSION="x"
;;
"N")
SESSION="n"
;;
*)
;;
esac
fi
done
fi
}
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
case $(uname) in
Darwin)
MOTD="bash $SCRIPT_DIR/macmotd/motd.sh"
;;
Linux)
NF=$(which neofetch &> /dev/null && echo $?)
SF=$(which screenfetch &> /dev/null && echo $?)
LF=$(ls $SCRIPT_DIR/linmotd/motd.sh &> /dev/null && echo $?)
if [[ "$LF" == "0" ]]; then
MOTD="$SCRIPT_DIR/linmotd/motd.sh"
elif [[ "$NF" == "0" ]]; then
MOTD="neofetch"
elif [[ "$SF" == "0" ]]; then
MOTD="screenfetch"
else
MOTD="echo No MOTD setup... Think about installing neofetch, screenfetch or creating a linfetch script"
fi
;;
*)
MOTD="Your platform '$(uname)' is not recognised."
;;
esac
if [[ $TERM_PROGRAM == "iTerm.app" ]]; then
if [[ $TMUX_INTEGRATION == "false" ]]; then
TMUX_CMD="tmux"
else
TMUX_CMD='tmux -CC'
fi
ITERM2=TRUE
else
TMUX_CMD="tmux"
ITERM2=FALSE
fi
# First check no sessions are runing
tmux ls > /dev/null 2&>1
# If this exits with 1, no sessions are running, start a new one
if [ $? -ne 0 ]; then
exec $TMUX_CMD new-session "echo && $MOTD && zsh"
exit 0
fi
TMUX_SESSIONS=($(tmux ls -F '#{session_name}, #{session_attached}' |grep -v ', 1' |cut -d, -f1))
#If no sessions are available to attach to just create a new one
if [[ ${#TMUX_SESSIONS[@]} == "0" ]]; then
exec $TMUX_CMD new-session "echo && $MOTD && zsh"
exit 0
fi
echo "TMUX sessions are availble to attach to:"
tmux ls |grep -v attached
echo " : n or ↵ to start a new session"
UNATTCHED_SESSIONS=($(tmux ls -F '#{session_name}, #{session_attached}' |grep -v ', 1' |cut -d, -f1 && echo "n" && echo "N" && echo "X" && echo "x"))
while [[ -z "$SESSION" ]]
do
if [[ -n "$LOOPRUN" ]]; then
echo "Not a valid session number."
fi
assessSession "${UNATTCHED_SESSIONS[@]}"
LOOPRUN=1
done
if [[ "$SESSION" = "n" ]]; then
exec $TMUX_CMD new-session "echo && $MOTD && zsh"
exit 0
elif [[ "$SESSION" = "x" ]]; then
exit 666
else
exec $TMUX_CMD a -t $SESSION
exit 0
fi