-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmux-pane-view
executable file
·289 lines (247 loc) · 7.46 KB
/
tmux-pane-view
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/bin/bash
cached_client_path="/tmp/tmux-pane-view-client"
cached_target_path="/tmp/tmux-pane-target"
cached_client_path_alt="/tmp/tmux-pane-view-client-alt"
cached_target_path_alt="/tmp/tmux-pane-target-alt"
alt_client="false"
alt_client_arg=""
for arg in "$@"; do
case "$arg" in
--class=*)
class="${arg#--class=}"
;;
--alacritty_theme=*)
alacritty_theme="${arg#--alacritty_theme=}"
;;
--cmd=*)
cmd="${arg#--cmd=}"
;;
--alt-client)
alt_client="true"
alt_client_arg=" --alt-client"
;;
esac
done
get_view_client_path () {
if [ $alt_client == "true" ] || [ "$1" == "--alt" ]; then
echo $cached_client_path_alt
else
echo $cached_client_path
fi
}
get_view_client () {
if [ -f $(get_view_client_path $@) ]; then
cat $(get_view_client_path $@)
fi
}
get_target_path () {
if { [ "$1" == "--alt" ]; } \
|| { [ "$1" != "--invert" ] && [ "$alt_client" == "true" ]; } \
|| { [ "$1" == "--invert" ] && [ "$alt_client" == "false" ]; }; then
echo "$cached_target_path_alt"
else
echo "$cached_target_path"
fi
}
get_target () {
local path=$(get_target_path $@)
if [ -f $path ]; then
cat $path
fi
}
set_status_inner () {
local target="$1"
local mode="$2"
local tSet="tmux set -p -t $target"
if [ $mode = "none" ]; then
$tSet -u status
$tSet -u status-left
$tSet -u status-left-length
$tSet -u status-position
$tSet -u status-bg
$tSet -u status-fg
else
local view_client=$(get_view_client)
local escaped_target=$(echo "$target" | sed 's/%/%%/g')
local pv_label="[PANE-VIEW "
if [ $alt_client = 'true' ]; then
pv_label+="(ALT) "
fi
pv_label+="$escaped_target -> $view_client]"
local ssh_label="[ssh]"
set_left () {
local left+="$1 "
local left_length=$((${#left} + 1))
$tSet status-left "$left"
$tSet status-left-length $left_length
}
case "$mode" in
"ssh")
set_left "$ssh_label"
$tSet status on
$tSet status-position top
$tSet status-bg blue
$tSet status-fg white
;;
"ssh+pane-view")
set_left "$ssh_label ███████ $pv_label ███████"
$tSet status on
$tSet status-position top
$tSet status-fg white
if [ $alt_client == "true" ]; then
$tSet status-bg "#5353c6"
else
$tSet status-bg "blue"
fi
;;
"pane-view")
set_left "$pv_label"
$tSet status on
$tSet status-position top
$tSet status-fg black
if [[ $alt_client = "true" ]]; then
$tSet status-bg "#797979"
else
$tSet status-bg "white"
fi
;;
esac
fi
}
set_status () {
local target="$1"
if [ $target = $(get_target --alt) ]; then
alt_client="true"
fi
local p="$target"0.0
local mode="$2"
local status_on=$(tmux display -p -t $p "#{status}")
local old_left=$(tmux display -p -t $p "#{status-left}")
case "$mode" in
"no-status")
set_status_inner $p "none"
;;
"exit-ssh")
if [ $status_on = "on" ] && [[ $old_left == *"PANE-VIEW"* ]]; then
set_status_inner $p "pane-view"
else
set_status_inner $p "none"
fi
;;
"ssh")
if [[ $old_left != *"ssh"* ]]; then
if [ $status_on = "on" ] && [[ $old_left == *"PANE-VIEW"* ]]; then
set_status_inner $p "ssh+pane-view"
else
set_status_inner $p "ssh"
fi
fi
;;
"pane-view")
if [[ $old_left != *"PANE-VIEW"* ]]; then
if [ $status_on = "on" ] && [[ $old_left == *"ssh"* ]]; then
set_status_inner $p "ssh+pane-view"
else
set_status_inner $p "pane-view"
fi
fi
;;
esac
}
set_new_target () {
local view_client=$(get_view_client)
local target="$1"
if [ $target = $(get_target --invert) ]; then
echo "$target" already in use.
exit 1
elif [ -z "$view_client" ] || [ -z "$(tmux list-clients | grep -w $view_client)" ]; then
echo "No pane-view client found."
exit 1
elif [ -z "$target" ]; then
echo "No pane-view target found."
exit 1
fi
local old_target=$(get_target)
if [ -n "$old_target" ] && [ "$old_target" != "$target" ]; then
set_status $old_target "no-status"
fi
local target_path=$(get_target_path)
echo $target>$target_path
set_status $target "pane-view"
tmux switch-client -c "$view_client" -t "$target"
}
if [ -z "$1" ] || [ -n "$alacritty_theme" ] || [ -n "$class" ]; then
target_path=$(get_target_path)
client_path=$(get_view_client_path)
class_arg="--class "
if [ -n "$class" ]; then
class_arg+="$class"
else
class_arg+="tmux-pane-view"
fi
theme_arg=""
if [ -n "$alacritty_theme" ]; then
theme_arg="--config-file $alacritty_theme"
fi
sessions=$(tmux list-sessions -F "#{session_name} #{pane_height} #{pane_width}" -f '#{m:1,#{session_attached}}')
# initially attach the view to the widest pane:
max_width=0
while read -r line; do
if [[ $line == *"_child"* ]]; then
width=$(echo "$line" | awk '{print $3}')
if (( width > max_width )); then
max_width=$width
widest=$(echo "$line" | awk '{print $1}')
fi
fi
done <<< "$sessions"
alacritty -o font.size=14 $class_arg $theme_arg -e sh -c \
"
trap '
target=\$(cat \"$target_path\");
rm -f $client_path $target_path;
if [ -n \"\$target\" ]; then
tmux set -u -pt \$target status;
tmux set -u -pt \$target status-left;
tmux set -u -pt \$target status-position;
fi
' EXIT;
tmux-pane-view --record-client-id$alt_client_arg && tmux attach-session -t \"$widest:\"
" &
sleep 1
set_new_target "$widest:"
exit 0
fi
if [ "$1" == "--target-current-pane" ]; then
parent_pane=$(tmux display -p "#{pane_id}")
child_session=""$parent_pane"_child:"
set_new_target $child_session
exit 0
fi
if [ "$1" == "--pane-to-session" ]; then
parent_pane="$TMUX_PANE"
child_session="$parent_pane"_child
if [ -n "$cmd" ]; then
tmux new -d -s $child_session "$cmd"
else
tmux new -d -s $child_session
fi
tmux-pane-reflect-ssh $child_session
TMUX= tmux attach -t $child_session:
exit 0
fi
if [ "$1" == "--record-client-id" ]; then
client_path=$(get_view_client_path)
tmp_sess="tmux-pane-view-tmp-session$alt_client"
tmux new-session -s $tmp_sess \
"tmux list-clients -F '#{client_name} #{client_session}' \
| grep -w $tmp_sess \
| awk '{print \$1}' \
>$client_path \
&& sleep 0.1 && tmux detach"
exit 0
fi
if [ "$1" == "--set-status" ]; then
set_status $2 $3
exit 0
fi