-
Notifications
You must be signed in to change notification settings - Fork 0
/
brainmux.sh
executable file
·1583 lines (1422 loc) · 53 KB
/
brainmux.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
##############################################################################
# BRAINBOX TMUX SESSION MANAGER
##############################################################################
#
# AUTHOR: Brian Call
# SOURCE: https://github.com/bacall213
# LICENSE: MIT
#
# GOALS:
# - Create more friendly interface for tmux.
# - Create robust and predictable tmux interface for distribution
# with 'brainbox' script package.
# - Grow development skills through the creation of a suite of
# scripts designed to enhance common system administration tasks.
#
# NON-GOALS:
# - Replace tmux. This script enhances tmux, not replaces it.
#
# TESTED PLATFORMS:
# - Ubuntu 12.04 LTS (64-bit)
# - Ubuntu 14.04 LTS (64-bit)
#
# REQUIRED PACAKGES:
# - tmux (provides /usr/bin/tmux, man pages, etc)
#
# FILES:
# - brainmux.sh (this script)
#
# OPTIONAL COMPONENTS:
# - $HOME/.tmux.conf
# - User-specific config (keybindings, terminal settings, etc)
# - tmux interprets the config file, if present, NOT this script
#
# KNOWN ISSUES (KI):
# 1) tmux.conf misconfigurations will cause script to fail
# 2) tmux.conf settings will override script settings
#
# CONVENTIONS USED:
# - Versioning standard:
# - v[major_version].[month(M)].[year(YYYY)]-b[build_num]
# - e.g. v1.1.2014-b20 = Major version 1, January 2014, build 20
#
# REVISION HISTORY:
# - v1.2.2015-b3
# - Completed echo => printf conversion
# - Condensed code width to < 79 chars (including space for VIM line
# numbering)
# - Made more error message friendly to narrow terminals
#
# - v1.2.2015-b2
# - Continued echo => printf conversion
#
# - v1.2.2015-b1
# - Started echo => printf conversion
# - Started reducing line length to <=80 chars
# - Added check for quotes array length < 1
#
# - v1.8.2014-b6
# - Eliminated uncaught error output from 'attach' function
# - Added more helpful output to 'attach' function
# - Added ability to instruct 'sessions' function to exit or not
# - Started shortening lines to < 78 chars
# - Removed brackets from colorized console output to make it more
# friendly.
# - Added 'list' as natural alias for 'sessions'
# - Added 'config' as alias for 'defaults'
# - Updated 'autocompletelist' in case I get around to including that
# level of functionality.
# - Google Style Guide
# - "Put ; do and ; then on the same line as the while, for or if."
# (https://google-styleguide.googlecode.com/svn/trunk/shell.xml?
# showone=Loops#Loops)
#
# - v1.8.2014-b5
# - Moved unnamed session identification to own function
# - Cleaned up process for unnamed session auto-attach
# - Implemented --attach|-attach for named sessions
#
# - v1.8.2014-b4
# - Removed "custom session" flag
# - tmux configs are too complex for it to be realistic to
# consider someone would want to enter a custom config at a
# command line
# - Added "--attach|-attach" flag for unnamed session creation
# - Automatically attaches to newly created session
#
# - v1.8.2014-b3
# - Put CLI parsing in alphabetical order
# - Added friendly message for sessions() when there are no sessions
#
# - v1.8.2014-b2
# - create() function not as dependant on defaults array order
# - Updated a few messages
#
# - v1.8.2014-b1
# - Fixed symlink checker function (flawed logic)
#
# - v1.7.2014-b2
# - check_symlink function created
# - Warning message for bad verbose flags added
#
# - v1.7.2014-b1
# - Initial public release
# TODO:
# 1) Read and intelligently output .tmux.conf or /etc/tmux.conf in
# defaults function.
#
##############################################################################
##############################################################################
##############################################################################
# VARIABLES
##############################################################################
# Formatting Constants
declare -r BOLD=$(tput bold); # Bold
declare -r UL=$(tput smul); # Underline
declare -r NO_UL=$(tput rmul); # No underline
declare -r NORM=$(tput sgr0); # Normal (not bolded)
declare -r BOLD_BG_RED=${BOLD}$(tput setab 1); # Bold w/red background
declare -r BOLD_RED=${BOLD}$(tput setaf 1); # Bold red
declare -r BOLD_BG_YELLOW=${BOLD}$(tput setab 3); # Bold w/yellow background
declare -r BOLD_YELLOW=${BOLD}$(tput setaf 3); # Bold yellow
declare -r BOLD_GREEN=${BOLD}$(tput setaf 2); # Bold green
declare -r BOLD_BG_GREEN=${BOLD}$(tput setab 2); # Bold w/green background
# Script name
# /full/cannonical/name.sh
declare -r SCRIPT_NAME_FULL_LONG=$(readlink -e "$0");
# name.sh
declare -r SCRIPT_NAME_FULL_SHORT=$(basename "$SCRIPT_NAME_FULL_LONG");
# /full/cannonical/name
declare -r SCRIPT_NAME_BASE_LONG=${SCRIPT_NAME_FULL_LONG%%.sh};
# name
declare -r SCRIPT_NAME_BASE_SHORT=$(basename "$SCRIPT_NAME_BASE_LONG");
# Session Defaults Array
# NOTE: create() expects these commands to be in a particular order.
# Changing the order will have unexpected consequences.
declare -r SESSION_DEFAULTS=('tmux new-session -d'
'tmux new-window'
'tmux new-window'
'tmux new-window'
'tmux new-window'
'tmux new-window'
'tmux select-window -t:0');
# 'tmux select-pane -L -t:0')
# Failure Quotes Array
declare -r FAIL_QUOTES=(
"My great concern is not whether you have failed, but whether you are
content with your failure. --Abraham Lincoln"
"Many of life's failures are people who did not realize how close they were
to success when they gave up. --Thomas A. Edison"
"Failure is not fatal, but failure to change might be. --John Wooden"
"If you learn from defeat, you haven't really lost. --Zig Ziglar"
"Remember that failure is an event, not a person. --Zig Ziglar"
"I don't believe in failure. It is not failure if you enjoyed the process.
--Oprah Winfrey"
"They don't make bugs like Bunny anymore. --Olav Mjelde."
"Talk is cheap. Show me the code. --Linus Torvalds"
"Life is full of screwups. You're supposed to fail sometimes. It's a
required part of the human existance. --Sarah Dessen"
"Only those who dare to fail greatly can ever achieve greatly.
--Robert F. Kennedy"
"Giving up is the only sure way to fail. --Gena Showalter"
"The phoenix must burn to emerge. --Janet Fitch"
"There is no failure except in no longer trying. --Chris Bradford"
"The only real mistake is the one from which we learn nothing.
--Henry Ford");
# Initialization for various global variables
cli_cmd=""; # Tracks current argument
arg_pos=""; # Tracks argument position in array
tmux_status="999"; # Impossible value
cli_args=("$@"); # Store all args
debug1=false; # -v : Minimal debug info
debug2=false; # -vv : Some debug info
debug3=false; # -vvv : Most debug info
debug4=false; # -vvvv : All debug info
newest_tmux_session="-1"; # Used by find_newest_session() function
# Stores session name for last-created,
# nameless, session
newest_tmux_timestamp="-1"; # Used by find_newest_session() function
# Stores session timestamp for last-created,
# nameless, session
##############################################################################
##############################################################################
# FUNCTION
##############################################################################
# NAME: quickhelp
# PURPOSE: Display short script help information
# ARGUMENTS:
# - None
# OUTPUT:
# - Script help information
##############################################################################
function quickhelp()
{
# debug4 :: start
if [[ $debug4 == true ]]; then
printf "%s %s\n"\
"[DEBUG][QUICKHELP][${BOLD_GREEN}INFO${NORM}]"\
"Entering 'quickhelp' function";
fi
printf "\n%s %s\n\
\n%s\n%s %s\n%s\n\n%s\n%s\n\
\n%s\n%s\n%s\n\n%s\n%s\n\
\n%s\n%s\n%s\n%s\n\
\n%s\n%s\n%s\n\n%s\n%s\n%s\n%s\n%s\n"\
"${BOLD}USAGE :: $0${NORM} [-v|vv|vvv|vvvv] ${BOLD}command${NORM}"\
"${UL}options${NO_UL}"\
" ${UL}Create and Destroy Sessions${NO_UL}"\
" ${BOLD}create${NORM} [${UL}session_1${NO_UL}"\
"${UL}session_2${NO_UL} ${UL}session_3${NO_UL} ...]"\
" (alias: ${BOLD}new${NORM}, ${BOLD}start${NORM})"\
" ${BOLD}destroy${NORM} [all] ${UL}existing_session${NO_UL}"\
" (aliases: ${BOLD}stop${NORM}, ${BOLD}kill${NORM})"\
" ${UL}Connect and Disconnect Sessions${NO_UL}"\
" ${BOLD}connect${NORM} ${UL}existing_session${NO_UL}"\
" (alias: ${BOLD}attach${NORM})"\
" ${BOLD}disconnect${NORM} ${UL}existing_session${NO_UL}"\
" (alias: ${BOLD}detach${NORM})"\
" ${UL}Session Information${NO_UL}"\
" ${BOLD}info${NORM}"\
" ${BOLD}defaults${NORM} (alias: ${BOLD}config${NORM})"\
" ${BOLD}sessions${NORM} (alias: ${BOLD}list${NORM})"\
" ${UL}General Script Options${NO_UL}"\
" ${BOLD}help${NORM} (this menu)"\
" ${BOLD}helpfull${NORM}"\
" Debug/Verbose Mode (${BOLD}Must be first argument${NORM})"\
" -v : Minimal verbosity/debug information"\
" -vv : More verbose/some internal debug information"\
" -vvv : Most debug information"\
" -vvvv : Full verbosity/All debug information";
# debug4 :: end
if [[ $debug4 == true ]]; then
printf "%s %s\n"\
"[DEBUG][QUICKHELP][${BOLD_GREEN}INFO${NORM}]"\
"Leaving 'quickhelp' function";
fi
# Exit gracefully
exit 0;
}
##############################################################################
##############################################################################
# FUNCTION
##############################################################################
# NAME: fullhelp
# PURPOSE: Display full script help information
# ARGUMENTS:
# - None
# OUTPUT:
# - Script help information
##############################################################################
function fullhelp()
{
# debug4 :: start
if [[ $debug4 == true ]]; then
printf "%s %s\n"\
"[DEBUG][FULLHELP][${BOLD_GREEN}INFO${NORM}]"\
"Entering 'fullhelp' function";
fi
printf "\n%s %s\n\
\n%s\n%s %s\n%s\n\n%s\n%s\n%s\n%s\n\
\n%s\n%s\n\n%s\n%s %s\n%s\n\
\n%s\n%s\n%s\n\n%s\n\n%s\n%s\n\n%s\n\n"\
"${BOLD}USAGE :: $0${NORM} [-v|vv|vvv|vvvv] ${BOLD}command${NORM}"\
"${UL}options${NO_UL}"\
" ${UL}Create and Destroy Sessions${NO_UL}"\
" ${BOLD}create${NORM} [${UL}session_1${NO_UL}"\
"${UL}session_2${NO_UL} ${UL}session_3${NO_UL} ...]"\
" (alias: ${BOLD}new${NORM}, ${BOLD}start${NORM})"\
" Create one or multiple tmux sessions. If no session name"\
" is specified, tmux will create a new session named using"\
" its own mechanisms. If a specified session already"\
" exists, this script will skip over that session."\
" ${BOLD}destroy${NORM} [all] ${UL}existing_session${NO_UL}"\
" (aliases: ${BOLD}stop${NORM}, ${BOLD}kill${NORM})"\
" Destroy single, or all, existing tmux sessions. If 'all'"\
" is specified, this script will try to destroy all"\
"existing"\
" sessions."\
" ${UL}Connect and Disconnect Sessions${NO_UL}"\
" ${BOLD}connect${NORM} ${UL}existing_session${NO_UL}"\
" (alias: ${BOLD}attach${NORM})"\
" Connect to an existing tmux session."\
" ${BOLD}disconnect${NORM} ${UL}existing_session${NO_UL}"\
" (alias: ${BOLD}detach${NORM})"\
" Disconnect from an existing tmux session.";
read -p "(${BOLD}Press <Enter> for next page${NORM})";
printf "\n%s\n%s\n%s\n\n%s\n%s %s\n\n%s\n%s\n\
\n%s\n%s\n%s\n\n%s\n%s\n%s\n%s\n%s\n\
\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s %s\n"\
" ${UL}Session Information${NO_UL}"\
" ${BOLD}info${NORM}"\
" Show tmux session information, if tmux server is active."\
" ${BOLD}defaults${NORM} (alias: ${BOLD}config${NORM})"\
" Show the session defaults that are hard-coded into this"\
"script."\
" ${BOLD}sessions${NORM} (alias: ${BOLD}list${NORM})"\
" Show running tmux sessions."\
" ${UL}General Script Options${NO_UL}"\
" ${BOLD}help${NORM} (this menu)"\
" ${BOLD}helpfull${NORM}"\
" Debug/Verbose Mode (${BOLD}Must be first argument${NORM})"\
" -v : Minimal verbosity/debug information"\
" -vv : More verbose/some internal debug information"\
" -vvv : Most debug information"\
" -vvvv : Full verbosity/All debug information"\
" Output conventions"\
" [${BOLD_GREEN}INFO${NORM}] General debugging information."\
" [${BOLD_YELLOW}WARN${NORM}] Most non-fatal errors, like"\
" conflicts with existing sessions. Script may or may"\
" not exit."\
" [${BOLD_RED}ERROR${NORM}] Major errors. Script will exit"\
" (with limited exceptions). Syntax errors fall into"\
" this category."\
" [${BOLD_BG_RED}FATAL${NORM}] Fatal errors. Script will exit"\
"(no exceptions).";
# debug4 :: end
if [[ $debug4 == true ]]; then
printf "%s %s\n"\
"[DEBUG][FULLHELP][${BOLD_GREEN}INFO${NORM}]"\
"Leaving 'fullhelp' function";
fi
# Exit gracefully
exit 0;
}
##############################################################################
##############################################################################
# FUNCTION
##############################################################################
# NAME: check_symlink
# PURPOSE: Checks for a symlink named the same as the script, but
# without ".sh"
# ARGUMENTS:
# - None
# OUTPUT:
# - No console
# - Create 'brainmux' symlink in current directory if it doesn't
# exist and it's possible.
##############################################################################
function check_symlink()
{
# debug4 :: start
if [[ $debug4 == true ]]; then
printf "%s %s\n"\
"[DEBUG][CHECK_SYMLINK][${BOLD_GREEN}INFO${NORM}] Entering"\
"'check_symlink' function";
fi
# If NOT symlink...
if [[ ! $(test -L "$0" && readlink "$0") ]]; then
if [[ $debug4 == true ]]; then
printf "%s %s\n"\
"[DEBUG][CHECK_SYMLINK][${BOLD_GREEN}INFO${NORM}]"\
"$SCRIPT_NAME_FULL_LONG is NOT a symlink.";
fi
# Check for a symlink
if [[ ! $(test -L "$SCRIPT_NAME_BASE_LONG" &&
readlink "$SCRIPT_NAME_BASE_LONG") ]]; then
# No symlink found in current directory
printf "%s\n" "No symbolink found in execution directory.";
# Use full path so the command is universal
printf "%s %s\n"\
"Run ${BOLD}ln -s $SCRIPT_NAME_FULL_LONG"\
"$SCRIPT_NAME_BASE_LONG${NORM} if you would like one.";
else
# Symlink found: Silently continue, unless debugging
if [[ $debug4 == true ]]; then
printf "%s %s\n%s\n"\
"[DEBUG][CHECK_SYMLINK][${BOLD_GREEN}INFO${NORM}] Found a"\
"symlink during second check:"\
" >> $SCRIPT_NAME_BASE_LONG";
fi
fi
else
# Is a symlink
# Silently continue, unless debugging
if [[ $debug4 == true ]]; then
printf "%s %s\n%s\n"\
"[DEBUG][CHECK_SYMLINK][${BOLD_GREEN}INFO${NORM}] I am a"\
"symlink; actual script name:"\
" >> $SCRIPT_NAME_FULL_LONG";
fi
fi
# debug4 :: end
if [[ $debug4 == true ]]; then
printf "%s %s\n"\
"[DEBUG][CHECK_SYMLINK][${BOLD_GREEN}INFO${NORM}] Leaving"\
"'check_symlink' function";
fi
# No exit
}
##############################################################################
##############################################################################
# FUNCTION
##############################################################################
# NAME: defaults
# PURPOSE: Read tmux defaults and display them
# ARGUMENTS:
# - None
# OUTPUT:
# - tmux defaults as defined by SESSION_DEFAULTS array
##############################################################################
function defaults()
{
# debug4 :: start
if [[ $debug4 == true ]]; then
printf "%s %s\n"\
"[DEBUG][DEFAULTS][${BOLD_GREEN}INFO${NORM}] Entering 'defaults'"\
"function";
fi
# Window layout
printf "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n\
\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n\n"\
" ################ ################"\
" # Window 0 # # Window 1 #"\
" ################ ################"\
" # # # #"\
" # (bash) # # (bash) #"\
" # # # #"\
" # 100% # # 100% #"\
" ################ ################"\
" ################ ################"\
" # Window 2 # # Window 3 #"\
" ################ ################"\
" # # # #"\
" # (bash) # # (bash) #"\
" # # # #"\
" # 100% # # 100% #"\
" ################ ################"\
" ################"\
" # Window 4 #"\
" ################"\
" # #"\
" # (bash) #"\
" # #"\
" # 100% #"\
" ################";
# Options
printf "%s\n\n" "${BOLD}Session creation commands:${NORM}";
# Output default session creation parameters
# Use 'sdefault' to prevent potential collisions with 'default'
for sdefault in "${SESSION_DEFAULTS[@]}"; do
printf " %s\n" "$sdefault";
done
# Print tmux.conf
if [[ -e ~/.tmux.conf ]]; then
printf "\n%s\n\n"\
"${BOLD}Contents of ${UL}~/.tmux.conf${NO_UL}:${NORM}";
while read line; do
printf "%s\n" "$line";
done < ~/.tmux.conf
fi
if [[ -e /etc/tmux.conf ]]; then
printf "\n%s\n\n"\
"${BOLD}Contents of ${UL}/etc/tmux.conf${NO_UL}:${NORM}";
cat /etc/tmux.conf;
fi
# debug4 :: end
if [[ $debug4 == true ]]; then
printf "%s %s\n"\
"[DEBUG][DEFAULTS][${BOLD_GREEN}INFO${NORM}] Leaving 'defaults'"\
"function";
fi
# Exit gracefully
exit 0;
}
##############################################################################
##############################################################################
# FUNCTION
##############################################################################
# NAME: check_session
# PURPOSE: Check if specified session exists
# ARGUMENTS:
# - $1 : Session to check for
# OUTPUT:
# - Sets status of $tmux_status
##############################################################################
function check_session()
{
# debug4 :: start
if [[ $debug4 == true ]]; then
printf "%s %s\n"\
"[DEBUG][CHECK_SESSION][${BOLD_GREEN}INFO${NORM}] Entering"\
"'check_session' function";
fi
tmux_status="999"; # Reset at start of function as a safeguard
local tmuxSession="$1"; # Contains a passed in session name
tmux has-session -t "$tmuxSession" &>/dev/null;
tmux_status=$?;
# debug4 :: Session check
if [[ $debug4 == true ]]; then
printf "%s %s\n%s %s\n"\
"[DEBUG][CHECK_SESSION][${BOLD_GREEN}INFO${NORM}] Session"\
"tested = $tmuxSession"\
"[DEBIG][CHECK_SESSION][${BOLD_GREEN}INFO${NORM}]"\
"tmux_status = $tmux_status";
fi
# debug4 :: end
if [[ $debug4 == true ]]; then
printf "%s %s\n"\
"[DEBUG][CHECK_SESSION][${BOLD_GREEN}INFO${NORM}] Leaving"\
"'check_session' function";
fi
}
##############################################################################
##############################################################################
# FUNCTION
##############################################################################
# NAME: find_newest_session
# PURPOSE: Identify the newest tmux session created based on timestamp
# ARGUMENTS:
# - None
# OUTPUT:
# - Sets global variable 'newest_tmux_session' to the name of the
# last-created tmux session
# - Sets global variable 'newest_tmux_timestamp' to the timestamp for the
# last-created tmux session
##############################################################################
function find_newest_session()
{
# debug4 :: start
if [[ $debug4 == true ]]; then
printf "%s %s\n"\
"[DEBUG][FIND_NEWEST_SESSION][${BOLD_GREEN}INFO${NORM}] Entering"\
"'find_newest_session' function";
fi
local active_tmux_sessions;
active_tmux_sessions=( $(tmux list-sessions -F \
"#{session_created} #{session_name}") );
if [[ $debug4 == true ]]; then
printf "%s %s\n%s\n"\
"[DEBUG][FIND_NEWEST_SESSION][${BOLD_GREEN}INFO${NORM}]"\
"tmux session list..."\
"${active_tmux_sessions[@]}";
fi
# Parse active sessions list
for (( session_index = 0 ; session_index < ${#active_tmux_sessions[@]} ;
session_index++ )); do
# Even values are the timestamp
if [[ $(( session_index % 2 )) == 0 ]]; then
if [[ ${active_tmux_sessions[$session_index]} \
> $newest_tmux_timestamp ]]; then
newest_tmux_timestamp=${active_tmux_sessions[$session_index]};
newest_tmux_session=${active_tmux_sessions[$session_index+1]};
fi
fi
done
if [[ $debug4 == true ]]; then
printf "%s %s\n%s\n"\
"[DEBUG][FIND_NEWEST_SESSION][${BOLD_GREEN}INFO${NORM}] Newest"\
"session ID found = $newest_tmux_session"\
"(timestamp=$newest_tmux_timestamp)";
fi
# debug4 :: end
if [[ $debug4 == true ]]; then
printf "%s %s\n"\
"[DEBUG][FIND_NEWEST_SESSION][${BOLD_GREEN}INFO${NORM}] Leaving"\
"'find_newest_session' function";
fi
# Return
# newest_tmux_session is a global variable set by this function
# newest_tmux_timestamp is a global variable set by this function
# No exit
}
##############################################################################
##############################################################################
# FUNCTION
##############################################################################
# NAME: create
# PURPOSE: Create/start new tmux session
# ARGUMENTS:
# - Command line args : ${cli_args[@]}
# OUTPUT:
# - None (sessions created silently)
##############################################################################
function create()
{
# create() function also aliased as 'start' and 'new'
# debug4 :: start
if [[ $debug4 == true ]]; then
printf "%s %s\n"\
"[DEBUG][CREATE][${BOLD_GREEN}INFO${NORM}] Entering 'create'"\
"function";
fi
local session_args;
session_args=("${cli_args[@]}");
# Check for no session name or "-attach|--attach"
if [[ "${session_args[$arg_pos]}" == "" ||
"${session_args[$arg_pos]}" == "--attach" ||
"${session_args[$arg_pos]}" == "-attach" ]]; then
# debug4 :: No session specified, creating generic session
if [[ $debug4 == true ]]; then
printf "%s %s\n%s %s\n"\
"[DEBUG][CREATE][${BOLD_YELLOW}WARN${NORM}]"\
"No session names specified"\
"[DEBUG][CREATE][${BOLD_GREEN}INFO${NORM}]"\
"Creating unnamed session";
fi
# Parse SESSION_DEFAULTS array and execute commands
# Order doesn't matter here, except for any order tmux requires
for param in "${SESSION_DEFAULTS[@]}"; do
`$param`;
done
# Check for "--attach" or "-attach" flags
if [[ "${session_args[$arg_pos]}" == "--attach" ||
"${session_args[$arg_pos]}" == "-attach" ]]; then
if [[ $debug4 == true ]]; then
printf "%s %s\n%s\n"\
"[DEBUG][CREATE][${BOLD_GREEN}INFO${NORM}] Found --attach or"\
"-attach flag with unnamed session"\
" creation";
fi
# Find last-created session with find_newest_session()
find_newest_session;
# Check for a sane return value
if [[ "$newest_tmux_session" != "-1" ]]; then
if [[ $debug4 == true ]]; then
printf "%s %s %s\n"\
"[DEBUG][CREATE][${BOLD_GREEN}INFO${NORM}]"\
"find_newest_session() returned session name:"\
"$newest_tmux_session";
fi
# Attach to newly created, unnamed, session
tmux attach-session -t $newest_tmux_session;
else
printf "%s %s\n%s %s\n%s\n"\
"${BOLD_YELLOW}WARNING${NORM} \$newest_tmux_session was not"\
"set correctly."\
" This script will continue, but cannot auto-attach"\
"to the newly"\
" created session.";
fi
else
# No session specified, creating generic session
printf "%s %s\n%s %s\n"\
"${BOLD_YELLOW}WARNING${NORM} No session name specified,"\
"tmux will choose one for you. Run this"\
" script with the 'sessions' command to identify"\
"running tmux sessions.";
fi
elif [[ "${session_args[$arg_pos+1]}" == "--attach" ||
"${session_args[$arg_pos+1]}" == "-attach" ]]; then
# Catch the creation of a named session and attach it
# [debug] Identify parsed session name
# Indicate "--attach|-attach" was found
if [[ $debug4 == true ]]; then
printf "%s %s\n%s %s\n%s %s\n"\
"[DEBUG][CREATE][${BOLD_GREEN}INFO${NORM} Noticed request to"\
"attach session after creation"\
"(-attach|--attach parsed from"\
"'\${session_args[\$arg_pos+1]}')"\
"[DEBUG][CREATE][${BOLD_GREEN}INFO${NORM}] Parsed session"\
"name: ${session_args[$arg_pos]}";
fi
# Check to see if a session by the same name exists
# 'check_session' updates $tmux_status variable
# Session does not exist: tmux_status = 1
# Session exists: tmux_status = 0
check_session "${session_args[$arg_pos]}";
# If check_session didn't find an existing session, create it
if [[ "$tmux_status" == "1" ]]; then
# [debug] Creating session
if [[ $debug3 == true ]]; then
printf "%s %s\n"\
"[DEBUG][CREATE][${BOLD_GREEN}INFO${NORM}] Creating session"\
"${session_args[$arg_pos]}";
fi
# Relies on array matching these base values
#{tmux new-session -d} -s ${session_args[$arg_pos]};
#{tmux split-window -h -p 50} -t ${session_args[$arg_pos]};
#{tmux new-window} -t ${session_args[$arg_pos]};
#{tmux new-window} -t ${session_args[$arg_pos]};
#{tmux new-window} -t ${session_args[$arg_pos]};
#{tmux select-window -t:0} -t ${session_args[$arg_pos]}:0;
# Actual defaults array
# tmux new-session -d
# tmux split-window -h -p 50
# tmux new-window
# tmux new-window
# tmux new-window
# tmux select-window -t:0
# Parse SESSION_DEFAULTS array and execute commands
# Order matters, but shouldn't be dependent on the array
# Check each argument first
# Requires checks for 5 unique arguments
for param in "${SESSION_DEFAULTS[@]}"; do
case $param in
"tmux new-session -d")
$param -s ${session_args[$arg_pos]};
if [[ $debug4 == true ]]; then
printf "%s %s\n"\
"[DEBUG][CREATE] Create session executing: $param -s"\
"${session_args[$arg_pos]}";
fi
;;
"tmux split-window -h -p 50")
$param -t ${session_args[$arg_pos]};
if [[ $debug4 == true ]]; then
printf "%s %s\n"\
"[DEBUG][CREATE] Create session executing: $param -t"\
"${session_args[$arg_pos]}";
fi
;;
"tmux new-window")
$param -t ${session_args[$arg_pos]};
if [[ $debug4 == true ]]; then
printf "%s %s\n"\
"[DEBUG][CREATE] Create session executing: $param -t"\
"${session_args[$arg_pos]}";
fi
;;
"tmux select-window -t:0")
$param -t ${session_args[$arg_pos]}:0;
if [[ $debug4 == true ]]; then
printf "%s %s\n"\
"[DEBUG][CREATE] Create session executing: $param -t"\
"${session_args[$arg_pos]}:0";
fi
;;
"tmux select-pane -L -t:0")
$param -t ${session_args[$arg_pos]}:0;
if [[ $debug4 == true ]]; then
printf "%s %s\n"\
"[DEBUG][CREATE] Create session executing: $param -t"\
"${session_args[$arg_pos]}:0";
fi
;;
*)
if [[ $debug4 == true ]]; then
printf "%s %s %s\n"\
"[DEBUG][CREATE][${BOLD_RED}ERROR${NORM}] A tmux"\
"session creation command does not match an expected"\
"value: $param";
else
printf "%s %s\n"\
"${BOLD_YELLOW}WARNING${NORM} Potentially fatal"\
"session creation error. Run with -vvvv for details.";
fi
esac
done
# Attach to named session
tmux attach-session -t ${session_args[$arg_pos]};
else
printf "%s %s\n"\
"${BOLD_YELLOW}WARNING${NORM} Session"\
"'${BOLD}${session_args[$arp_pos]}${NORM}' exists. Skipped.";
fi
else
# No special circumstances, process all provided session names and
# create them if possible
# Check sessions args, if !exists: create, else: error
# Start from array element 1 = elements after "create" command
for i in "${session_args[@]:$arg_pos}"; do
# debug4 :: Identify parsed session name
if [[ $debug4 == true ]]; then
printf "%s %s\n"\
"[DEBUG][CREATE][${BOLD_GREEN}INFO${NORM}] Parsed"\
"session name: $i";
fi
# Check to see if a session by the same name exists
# 'check_session' updates $tmux_status variable
# Session does not exist: tmux_status = 1
# Session exists: tmux_status = 0
check_session "$i";
# If check_session didn't find an existing session, create it
if [[ "$tmux_status" == "1" ]]; then
# debug3 :: Creating session
if [[ $debug3 == true ]]; then
printf "%s %s\n"\
"[DEBUG][CREATE][${BOLD_GREEN}INFO${NORM}]"\
"Creating session $i";
fi
# Relies on array matching these base values
#{tmux new-session -d} -s $i;
#{tmux split-window -h -p 50} -t $i;
#{tmux new-window} -t $i;
#{tmux new-window} -t $i;
#{tmux new-window} -t $i;
#{tmux select-window -t:0} -t $i:0;
# Actual defaults array
# tmux new-session -d
# tmux split-window -h -p 50
# tmux new-window
# tmux new-window
# tmux new-window
# tmux select-window -t:0
# Parse SESSION_DEFAULTS array and execute commands
# Order matters, but shouldn't be dependent on the array
# Check each argument first
# Requires checks for 5 unique arguments
for param in "${SESSION_DEFAULTS[@]}"; do
case $param in
"tmux new-session -d")
if [[ $debug4 == true ]]; then
printf "%s %s\n"\
"[DEBUG][CREATE] Create session executing:"\
"$param -s $i";
$param -s $i;
else
$param -s $i;
fi
;;
"tmux split-window -h -p 50")
if [[ $debug4 == true ]]; then
printf "%s %s\n"\
"[DEBUG][CREATE] Create session executing:"\
"$param -t $i";
$param -t $i;
else
$param -t $i;
fi
;;
"tmux new-window")
if [[ $debug4 == true ]]; then
printf "%s %s\n"\
"[DEBUG][CREATE] Create session executing:"\
"$param -t $i";
$param -t $i;
else
$param -t $i;
fi
;;
"tmux select-window -t:0")
if [[ $debug4 == true ]]; then
printf "%s %s\n"\
"[DEBUG][CREATE] Create session executing:"\
"$param -t $i:0";
$param -t $i:0;
else
$param -t $i:0;
fi
;;
"tmux select-pane -L -t:0")
if [[ $debug4 == true ]]; then
printf "%s %s\n"\
"[DEBUG][CREATE] Create session executing:"\
"$param -t $i:0";
$param -t $i:0;
else
$param -t $i:0;
fi
;;
*)
if [[ $debug4 == true ]]; then
printf "%s %s %s\n"\
"[DEBUG][CREATE][${BOLD_RED}ERROR${NORM}] A tmux"\
"session creation command does not match an expected"\
"value: $param";
else
printf "%s %s\n"\
"${BOLD_YELLOW}WARNING${NORM} Potentially fatal"\
"session creation error. Run with -vvvv for details.";
fi
esac
done
else
# debug3 :: Session exists, skipped
if [[ $debug3 == true ]]; then
printf "%s %s\n"\
"[DEBUG][CREATE][${BOLD_YELLOW}WARN${NORM}] Session"\
"'${BOLD}$i${NORM}' exists. Skipped.";
else
printf "%s %s\n"\
"${BOLD_YELLOW}WARNING${NORM} Session '${BOLD}$i${NORM}'"\
"exists. Skipped.";
fi
fi
done
fi
# debug4 :: end
if [[ $debug4 == true ]]; then
printf "%s %s\n"\
"[DEBUG][CREATE][${BOLD_GREEN}INFO${NORM}] Leaving 'create'"\
"function";
fi
# Exit gracefully
exit 0;
}
##############################################################################
##############################################################################
# FUNCTION
##############################################################################
# NAME: destroy
# PURPOSE: Kill/destroy existing tmux sessions
# ARGUMENTS:
# - session name to kill
# OUTPUT:
# - None
##############################################################################
function destroy()
{
# destroy() function also aliased as 'kill' and 'stop'
# debug4 :: start
if [[ $debug4 == true ]]; then
printf "%s %s\n"\
"[DEBUG][DESTROY][${BOLD_GREEN}INFO${NORM}] Entering 'destroy'"\
"function";
fi
# Variables
local session_args;
session_args=("${cli_args[@]}");
# Kill all sessions if 'all' is the first argument after command
if [[ "${session_args[$arg_pos]}" == "" ]]; then
# debug3 :: No session specified
if [[ $debug3 == true ]]; then
printf "%s %s\n"\
"[DEBUG][DESTROY][${BOLD_RED}ERROR${NORM}] No session"\
"names specified";
fi
# No session specified