-
Notifications
You must be signed in to change notification settings - Fork 0
/
always_connected
118 lines (109 loc) · 2.88 KB
/
always_connected
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
SCREEN_POOL_ARGS=$(echo $TERM | cut -d"," -f2)
TERM=$(echo $TERM | cut -d"," -f1)
if [[ $(echo $SCREEN_POOL_ARGS | grep -w -- --context=[^-]*) != "" ]]; then
suffix="."$(echo $SCREEN_POOL_ARGS | sed 's/.*--context=\([^-]*\).*/\1/')
else
suffix=""
fi
connect() {
if [[ $STY != "" ]]; then
if [[ $(echo "$@" | grep -w -- --force) == "" ]]; then
echo "Already in a screen, not advised to connect further. Use --force to go ahead anyway"
return
fi
fi
free_screen=$(screen -list | grep "\.console"$suffix"\." | grep Detached | head -n1 | cut -f2)
if [[ $free_screen == "" ]]; then
echo "No free screens found, creating new!"
spawn "$@"
else
screen -x $free_screen
fi
}
spawn() {
if [[ $STY != "" ]]; then
if [[ $(echo "$@" | grep -w -- --force) == "" ]]; then
echo "Already in a screen, not advised to connect further. Use --force to go ahead anyway"
return
fi
fi
last_console_id=$(screen -list | grep "\.console"$suffix"\." | cut -f2 | rev | cut -d"." -f1 | rev | sort -n | tail -n1)
if [[ $last_console_id == "" ]]; then
new_console_id=1
else
new_console_id=$( echo $last_console_id + 1 | bc)
fi
screen -S console$suffix"."$new_console_id
}
migrate() {
if [[ $1 != "" ]]; then
suffix="."$1
else
suffix=""
fi
if [[ $STY == "" ]]; then
echo "Cannot migrate screen when not connected"
return
fi
last_console_id=$(screen -list | grep "\.console"$suffix"\." | cut -f2 | rev | cut -d"." -f1 | rev | sort -n | tail -n1)
if [[ $last_console_id == "" ]]; then
new_console_id=1
else
new_console_id=$( echo $last_console_id + 1 | bc)
fi
new_name=console$suffix"."$new_console_id
old_pid=$(echo $STY | cut -d"." -f1)
screen -X sessionname $new_name
export STY=$old_pid"."$new_name
}
pool() {
total_consoles=$(screen -ls | grep "\.console" | wc -l)
free_consoles=$(screen -ls | grep "\.console" | grep "Detached" | wc -l)
echo $free_consoles/$total_consoles screens are free
if [[ $STY != "" ]]; then
echo "Connected to screen "$STY
fi
}
release_all() {
all_screens=$(screen -ls | grep "\.console" | cut -f2)
while read -r line; do
if [[ "$STY" != "$line" ]]; then
echo "Detaching $line"
screen -D "$line"
fi
done <<< "$all_screens"
screen -wipe
}
rollover() {
echo > /tmp/.always_connected_rollover.$USER
exit
}
reconnect() {
echo $(pwd) > /tmp/.always_connected_rollover.$USER
exit
}
if [[ $(echo $SCREEN_POOL_ARGS | grep -w -- --skip-connect) == "" ]]; then
if [[ $SSH_TTY != "" ]] || [[ $VSCODE_IPC_HOOK_CLI != "" ]]; then
if [[ $STY == "" ]]; then
while
if [ -f /tmp/.always_connected_rollover.$USER ]; then
next_dir=$(cat /tmp/.always_connected_rollover.$USER)
else
next_dir=""
fi
rm -f /tmp/.always_connected_rollover.$USER
if [[ $next_dir == "" ]]; then
connect
else
cd $next_dir
spawn
fi
[ -f /tmp/.always_connected_rollover.$USER ]
do
:
done
echo Exiting
exit
fi
fi
fi