-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathste-mux.10s.sh
executable file
·345 lines (290 loc) · 11 KB
/
ste-mux.10s.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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/bin/zsh
# ---------------
# Bitbar Settings
# ---------------
# <bitbar.title>Ste-Mux</bitbar.title>
# <bitbar.version>v1.0</bitbar.version>
# <bitbar.author>Stephen Millard</bitbar.author>
# <bitbar.author.github>sylumer</bitbar.author.github>
# <bitbar.desc>Personalised TMUX Manager for SwiftBar</bitbar.desc>
# -----------------
# SwiftBar Settings
# -----------------
# <swiftbar.hideAbout>true</swiftbar.hideAbout>
# <swiftbar.hideLastUpdated>true</swiftbar.hideLastUpdated>
# <swiftbar.hideRunInTerminal>true</swiftbar.hideRunInTerminal>
# <swiftbar.hideDisablePlugin>true</swiftbar.hideDisablePlugin>
# <swiftbar.refreshOnOpen>false</swiftbar.refreshOnOpen>
# --------------
# Initialisation
# --------------
STEMUXTMUXPLIST=".ste-mux"
if [[ "$SWIFTBAR" = '1' ]]; then
# Running inside of Swift Bar
STEMUXTMUXPLIST="${0:a:h}/$STEMUXTMUXPLIST"
else
# Running outside of Swift Bar
STEMUXTMUXPLIST="./$STEMUXTMUXPLIST"
fi
# If there is no PLIST file, set the default values
if [[ ! -f "$STEMUXTMUXPLIST.plist" ]]; then
# Set the functions file
defaults write "$STEMUXTMUXPLIST" "STEMUXFUNCTIONS" ".ste-mux-functions.sh"
# Set the custom TMUX connections file
defaults write "$STEMUXTMUXPLIST" "STEMUXTMUXCUSTOM" ".ste-mux-tmux-custom.sh"
# Set te custom commands file
defaults write "$STEMUXTMUXPLIST" "STEMUXCUSTOM" ".ste-mux-custom.sh"
# Icons/text to use in the menu bar
# No TMUX sessions
ICON=$(echo "⛫" | base64)
defaults write "$STEMUXTMUXPLIST" "STEMUXICON0" "$ICON"
# >0 TMUX sessions
defaults write "$STEMUXTMUXPLIST" "STEMUXICON1" "$ICON"
# Set the default count of sessions to be symbols rather than numbers
defaults write "$STEMUXTMUXPLIST" "STEMUXICONNUMSYMBOL" "true"
fi
# Some functions are kept in a separate file so that they can be shared by this script, and the custom
# commands script, or simply for personalisation.
# Default is based on the name of this file (with the addition of '-function') but dotted to hide it
# from Swift Bar, and set as a shell file. The file should be executable and be located in the same folder
STEMUXFUNCTIONS=$(defaults read "$STEMUXTMUXPLIST" "STEMUXFUNCTIONS")
# Custom TMUX commands file name.
# Default is the base name of this file but dotted to hide it from Swift Bar, and set as a shell file.
# The file should be executable and be located in the same folder.
STEMUXTMUXCUSTOM=$(defaults read "$STEMUXTMUXPLIST" "STEMUXTMUXCUSTOM")
# Custom commands file name.
# Default is the base name of this file but dotted to hide it from Swift Bar, and set as a shell file.
# The file should be executable and be located in the same folder.
STEMUXCUSTOM=$(defaults read "$STEMUXTMUXPLIST" "STEMUXCUSTOM")
# The menu bar icons (or text) to use for when there are no or some TMUX sessions running on the local machine.
# The content is stored in base64
# Note the default setting is they are both the same, but you can modify your PLIST file to modify this.
STEMUXICON0=$(defaults read "$STEMUXTMUXPLIST" "STEMUXICON0")
STEMUXICON1=$(defaults read "$STEMUXTMUXPLIST" "STEMUXICON1")
# The menu bar count of sessions can be displayed as sumbols (true) or numbers (false)
STEMUXICONNUMSYMBOL=$(defaults read "$STEMUXTMUXPLIST" "STEMUXICONNUMSYMBOL")
# Load the functions file.
# The conditional block allows us to test the code preoperly inside and outside Swift Bar.
# It makes it quicker and easier for debugging doing this.
if [[ "$SWIFTBAR" = '1' ]]; then
# Running inside of Swift Bar
source "${0:a:h}/$STEMUXFUNCTIONS"
else
# Running outside of Swift Bar
source "./$STEMUXFUNCTIONS"
fi
# A file to record the last custom command
# It is a dot file to ensure it does not show as a plugin to Swift Bar
STEMUXLOGFILE=".ste-mux-log.txt"
# The conditional block allows us to test the code preoperly inside and outside Swift Bar.
# It makes it quicker and easier for debugging doing this.
if [[ "$SWIFTBAR" = '1' ]]; then
# Running inside of Swift Bar
STEMUXLOGPATH="${0:a:h}/$STEMUXLOGFILE"
else
# Running outside of Swift Bar
STEMUXLOGPATH="./$STEMUXLOGFILE"
fi
# Store the name and parent folder of this script to use in our custom script file
# Because the SwiftBar meta-data-based scheduler works on cron syntax,
# the finest granularity is 1 minute, so to update more frequently, we need
# to rely on the filename syntax. So in case anyone changes the file name,
# we store it for use by the custom commands script as it will need to call
# back to this one and it stops the user having to modify the script name in
# that file. We just handle it automatically.
export STEMUX=$0
export STEMUXFOLDER=${0:a:h}
# Attempt to clear the screen in case we choose to open to the terminal session
export TERM=xterm
clear &>/dev/null
# ----------
# Operations
# ----------
# If an argument is passed in to actively do something, clear any log file
if [[ ! -z "$1" ]]; then
rm -f $STEMUXLOGPATH
fi
# If the first parameter passed to the script is to open a session, run this code block
if [[ "$1" = 'openSession' ]]; then
tmux attach -t "$2"
fi
# If the first parameter passed to the script is to end a session, run this code block
if [[ "$1" = 'endSession' ]]; then
tmux kill-session -t "$2"
fi
# If the first parameter passed to the script is to open a new unnamed session, run this code block
if [[ "$1" = 'newSessionAnonymous' ]]; then
tmux
fi
# If the first parameter passed to the script is to open a new named session and stay attached, run this code block
if [[ "$1" = 'newSessionNamed-a' ]]; then
echo tmux new -s "$2" "$3" > $STEMUXLOGPATH
tmux new -s "$2" "$3"
fi
# If the first parameter passed to the script is to open a new detached named session, run this code block
if [[ "$1" = 'newSessionNamed-d' ]]; then
echo tmux new -d -s "$2" "$3" > $STEMUXLOGPATH
echo "$1" >> $STEMUXLOGPATH
echo "$2" >> $STEMUXLOGPATH
echo "$3" >> $STEMUXLOGPATH
tmux new -d -s "$2" "$3"
fi
# If the first parameter passed to the script is to open a new SSH session, run this code block
if [[ "$1" = 'newSSHSession' ]]; then
# $2 = user | $3 = server | $4 = port (optional)
# Default the SSH port
if [[ -z $4 ]]; then
sshPort=22
else
sshPort=$4
fi
## SSH
ssh $2@$3 -p $sshPort
fi
# If the first parameter passed to the script is to run an arbitrary terminal command run this code block
if [[ "$1" = 'runCmd' ]]; then
# Custom commands are base64 encoded to avoid some problems with any referenced arguments (e.g. $1 $2) getting interpreted too early
# by encoding them in the menu and decoding them here, we can ensure they are interpreted only at the right time.
cmdToRun=$2
cmdToRun=$(echo $cmdToRun | base64 -d)
# Log the command for debugging
echo $cmdToRun > $STEMUXLOGPATH
echo "---" >> $STEMUXLOGPATH
#R un the command and attempt to log any output
eval ${cmdToRun} >> $STEMUXLOGPATH
fi
# If an argument is passed in to actively do something, play a noise
# This is an audible cue that the action has completed processing.
if [[ ! -z "$1" ]]; then
afplay "/System/Library/Sounds/tink.aiff"
fi
# -----------------------------------------------
# Build Menus & Menu Items for Base TMUX Commands
# -----------------------------------------------
# If there are any TMUX sessions we need to add options to connect to or end those sessions
tmux list-sessions > /dev/null
# TMUX list sessions will fail (code 1 = fail, code 0 = success) if no sessions
if [[ $? = 0 ]]; then
# We have one or more existing sessions
# Build an array of session names and a count of attached sessions
sessions="$(tmux list-sessions -F#S)"
sessionNameArray=(${(ps:\n:)"$(echo $sessions)"})
attachedSessionCount=$(tmux list-sessions | grep "(attached)" | wc -l)
# Menu bar text
ICON=$(echo $STEMUXICON1 | base64 -d)
SESSIONCOUNT=${#sessionNameArray}
# Check the type of numeric output on the menu bar (true = symbolic characters | false = numeric characters)
if [[ "$STEMUXICONNUMSYMBOL" = "true" ]]; then
# Symbols - most compact output
case $SESSIONCOUNT in
1 )
ICON="$ICON⓵"
;;
2 )
ICON="$ICON⓶"
;;
3 )
ICON="$ICON⓷"
;;
4 )
ICON="$ICON⓸"
;;
5 )
ICON="$ICON⓹"
;;
6 )
ICON="$ICON⓺"
;;
7 )
ICON="$ICON⓻"
;;
8 )
ICON="$ICON⓼"
;;
9 )
ICON="$ICON⓽"
;;
* )
ICON="$ICON+⃝"
esac
else
# Colon followed by numbers - most verbose output
ICON="$ICON:$SESSIONCOUNT"
fi
echo "$ICON"
echo "---"
# TMUX menu contains details about TMUX sessions
echo TMUX
# First sub item is the number of attached sessions
# This is just a listing of which TMUX sessions are already attached in a level 2 sub menu
# These are for info and non-actionable
echo "--$attachedSessionCount Attached Sessions: | color=white"
tmux list-sessions | grep "(attached)" | awk 'BEGIN{FS=":"}{print "----"$1}'
# Level 1 sub menu Separator
echo "-----"
# Lists the sessions that are available to attach to in a level 2 sub menu
# These are actionable
echo "--Attach to..."
for i in "${sessionNameArray[@]}"
do
echo "----Attach to session: $i | bash='$0' param1=openSession param2=$i terminal=true"
done
# Lists the sessions that are available to kill to in a level 2 sub menu
# These are actionable
echo "--Terminate..."
for i in "${sessionNameArray[@]}"
do
echo "----Kill session: $i | bash='$0' param1=endSession param2=$i terminal=false"
done
# Level 1 sub menu Separator
echo "-----"
# Always include the standard TMUX session addition
# This will create and attach to the session
echo "--Start New TMUX session | bash='$0' param1=newSessionAnonymous terminal=true"
else
# We have no existing sessions
sessionNameArray=()
# Menu bar text
ICON=$(echo $STEMUXICON0 | base64 -d)
echo "$ICON"
echo "---"
# TMUX menu contains details about TMUX sessions
echo TMUX
echo "---"
# Always include the standard session addition
echo "--Start New TMUX session | bash='$0' param1=newSessionAnonymous terminal=true"
fi
# Add Custom TMUX Commands
# First, we build the appropriate file path.
# This allows us to test the code properly inside and outside Swift Bar.
# It makes it quicker and easier for debugging doing this.
if [[ "$SWIFTBAR" = '1' ]]; then
# Running inside of Swift Bar
CUSTOMTMUXFILE=$(realpath "${0:a:h}/$STEMUXTMUXCUSTOM")
else
# Running outside of Swift Bar
CUSTOMTMUXFILE=$(realpath "./$STEMUXTMUXCUSTOM")
fi
# Second, if the file actually exists, run it to add additional custom commands
if [[ -f "$CUSTOMTMUXFILE" ]]; then
echo "---"
"$CUSTOMTMUXFILE"
fi
# --------------------------------------------
# Build Menus & Menu Items for Custom Commands
# --------------------------------------------
# Call the custom commands file in case we have any commands in it
# First, we build the appropriate file path.
# This allows us to test the code properly inside and outside Swift Bar.
# It makes it quicker and easier for debugging doing this.
if [[ "$SWIFTBAR" = '1' ]]; then
# Running inside of Swift Bar
CUSTOMFILE=$(realpath "${0:a:h}/$STEMUXCUSTOM")
else
# Running outside of Swift Bar
CUSTOMFILE=$(realpath "./$STEMUXCUSTOM")
fi
# Second, if the file actually exists, run it to add additional custom commands
if [[ -f "$CUSTOMFILE" ]]; then
"$CUSTOMFILE"
fi