-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path.bash_aliases
14689 lines (11998 loc) · 454 KB
/
.bash_aliases
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
#/*
# * This file is part of TangoMan package.
# *
# * Copyright (c) 2024 "Matthias Morin" <mat@tangoman.io>
# *
# * This source file is subject to the MIT license that is bundled
# * with this source code in the file LICENSE.
# */
#--------------------------------------------------
# header
#--------------------------------------------------
# app name
# shellcheck disable=SC2034
APP_NAME=bash_aliases
# app author
# shellcheck disable=SC2034
APP_AUTHOR=TangoMan75
# app version (set from latest git tag)
# shellcheck disable=SC2034
APP_VERSION=0.1.0
# app source repository
# shellcheck disable=SC2034
APP_REPOSITORY=https://github.com/TangoMan75/bash_aliases
# app installation directory
# shellcheck disable=SC2034
APP_INSTALL_DIR=/home/tangoman75/Documents/bash_aliases
# app user config directory
# shellcheck disable=SC2034
APP_USER_CONFIG_DIR=/home/tangoman75/.TangoMan75/bash_aliases/config
#--------------------------------------------------
# Global variables
#--------------------------------------------------
# zsh arrays lower bound start from 1
# shellcheck disable=SC2034
case "${SHELL}" in
'/bin/bash'|'/usr/bin/bash'|'/usr/bin/ash'|'/usr/bin/sh')
LBOUND=0
;;
'/usr/bin/zsh')
LBOUND=1
;;
esac
#--------------------------------------------------
# Colors global variables
#--------------------------------------------------
PRIMARY='\033[97m'
SECONDARY='\033[94m'
SUCCESS='\033[32m'
DANGER='\033[31m'
WARNING='\033[33m'
INFO='\033[95m'
LIGHT='\033[47;90m'
DARK='\033[40;37m'
DEFAULT='\033[0m'
EOL='\033[0m\n'
ALERT_PRIMARY='\033[1;104;97m'
ALERT_SECONDARY='\033[1;45;97m'
ALERT_SUCCESS='\033[1;42;97m'
ALERT_DANGER='\033[1;41;97m'
ALERT_WARNING='\033[1;43;97m'
ALERT_INFO='\033[1;44;97m'
ALERT_LIGHT='\033[1;47;90m'
ALERT_DARK='\033[1;40;37m'
#--------------------------------------------------
# A semantic set of colors functions
#--------------------------------------------------
# Synopsis: echo_* <STRING> [INDENTATION] [PADDING]
# STRING: Text to display.
# INDENTATION: Indentation level (default: 0).
# PADDING: Padding length (default: 0).
## Print primary (bright white text)
echo_primary() {
if [ $# -eq 1 ]; then set -- "$1" 0 0; elif [ $# -eq 2 ]; then set -- "$1" "$2" 0; fi
printf "%*b%b%-*b%b" "$2" '' "${PRIMARY}" "$3" "$1" "${DEFAULT}"
}
## Print secondary (bright blue text)
echo_secondary() {
if [ $# -eq 1 ]; then set -- "$1" 0 0; elif [ $# -eq 2 ]; then set -- "$1" "$2" 0; fi
printf "%*b%b%-*b%b" "$2" '' "${SECONDARY}" "$3" "$1" "${DEFAULT}"
}
## Print success (bright green text)
echo_success() {
if [ $# -eq 1 ]; then set -- "$1" 0 0; elif [ $# -eq 2 ]; then set -- "$1" "$2" 0; fi
printf "%*b%b%-*b%b" "$2" '' "${SUCCESS}" "$3" "$1" "${DEFAULT}"
}
## Print danger (red text)
echo_danger() {
if [ $# -eq 1 ]; then set -- "$1" 0 0; elif [ $# -eq 2 ]; then set -- "$1" "$2" 0; fi
printf "%*b%b%-*b%b" "$2" '' "${DANGER}" "$3" "$1" "${DEFAULT}"
}
## Print warning (orange text)
echo_warning() {
if [ $# -eq 1 ]; then set -- "$1" 0 0; elif [ $# -eq 2 ]; then set -- "$1" "$2" 0; fi
printf "%*b%b%-*b%b" "$2" '' "${WARNING}" "$3" "$1" "${DEFAULT}"
}
## Print info (bright purple text)
echo_info() {
if [ $# -eq 1 ]; then set -- "$1" 0 0; elif [ $# -eq 2 ]; then set -- "$1" "$2" 0; fi
printf "%*b%b%-*b%b" "$2" '' "${INFO}" "$3" "$1" "${DEFAULT}"
}
## Print light (black text over white background)
echo_light() {
if [ $# -eq 1 ]; then set -- "$1" 0 0; elif [ $# -eq 2 ]; then set -- "$1" "$2" 0; fi
printf "%*b%b%-*b%b" "$2" '' "${LIGHT}" "$3" "$1" "${DEFAULT}"
}
## Print dark (white text over black background)
echo_dark() {
if [ $# -eq 1 ]; then set -- "$1" 0 0; elif [ $# -eq 2 ]; then set -- "$1" "$2" 0; fi
printf "%*b%b%-*b%b" "$2" '' "${DARK}" "$3" "$1" "${DEFAULT}"
}
# Synopsis: alert_* <STRING>
# STRING: Text to display.
## Print primary alert (bold white text over bright blue background)
alert_primary() {
printf "${EOL}%b%64s${EOL}%b %-63s${EOL}%b%64s${EOL}\n" "${ALERT_PRIMARY}" '' "${ALERT_PRIMARY}" "$1" "${ALERT_PRIMARY}" '';
}
## Print secondary alert (bold white text over bright purple background)
alert_secondary() {
printf "${EOL}%b%64s${EOL}%b %-63s${EOL}%b%64s${EOL}\n" "${ALERT_SECONDARY}" '' "${ALERT_SECONDARY}" "$1" "${ALERT_SECONDARY}" '';
}
## Print success alert (bold white text over bright green background)
alert_success() {
printf "${EOL}%b%64s${EOL}%b %-63s${EOL}%b%64s${EOL}\n" "${ALERT_SUCCESS}" '' "${ALERT_SUCCESS}" "$1" "${ALERT_SUCCESS}" '';
}
## Print danger alert (bold white text over bright red background)
alert_danger() {
printf "${EOL}%b%64s${EOL}%b %-63s${EOL}%b%64s${EOL}\n" "${ALERT_DANGER}" '' "${ALERT_DANGER}" "$1" "${ALERT_DANGER}" '';
}
## Print warning alert (bold white text over bright orange background)
alert_warning() {
printf "${EOL}%b%64s${EOL}%b %-63s${EOL}%b%64s${EOL}\n" "${ALERT_WARNING}" '' "${ALERT_WARNING}" "$1" "${ALERT_WARNING}" '';
}
## Print info alert (bold white text over bright blue background)
alert_info() {
printf "${EOL}%b%64s${EOL}%b %-63s${EOL}%b%64s${EOL}\n" "${ALERT_INFO}" '' "${ALERT_INFO}" "$1" "${ALERT_INFO}" '';
}
## Print light alert (black text over white background)
alert_light() {
printf "${EOL}%b%64s${EOL}%b %-63s${EOL}%b%64s${EOL}\n" "${ALERT_LIGHT}" '' "${ALERT_LIGHT}" "$1" "${ALERT_LIGHT}" '';
}
## Print dark alert (bold white text over black background)
alert_dark() {
printf "${EOL}%b%64s${EOL}%b %-63s${EOL}%b%64s${EOL}\n" "${ALERT_DARK}" '' "${ALERT_DARK}" "$1" "${ALERT_DARK}" '';
}
## print TangoMan hero
function hero() {
# shellcheck disable=SC2183
printf "\033[0;32m _____%17s_____\n|_ _|___ ___ ___ ___|%5s|___ ___\n | | | .'| | . | . | | | | .'| |\n |_| |__,|_|_|_ |___|_|_|_|__,|_|_|\n%14s|___|%6s\033[33mtangoman.io\033[0m\n\n"
}
hero
## Create ".env" file into "~/.TangoMan75/bash_aliases/config" folder
function _create_env() {
function _usage() {
echo_success 'usage:' "$1" "$2"; echo_primary '_create_env [file] -h (help)\n'
}
#--------------------------------------------------
# Variables
#--------------------------------------------------
local file_path
local folder_path
#--------------------------------------------------
# Parse arguments
#--------------------------------------------------
local arguments=()
local OPTARG
local option
while [ "$#" -gt 0 ]; do
OPTIND=0
while getopts :h option; do
case "${option}" in
h) echo_warning '_create_env\n'
echo_success 'description:' 2 14; echo_primary 'Create ".env "file\n'
_usage 2 14
return 0;;
:) echo_danger "error: \"${OPTARG}\" requires value\n"
return 1;;
\?) echo_danger "error: invalid option \"${OPTARG}\"\n"
return 1;;
esac
done
if [ "${OPTIND}" -gt 1 ]; then
shift $(( OPTIND-1 ))
fi
if [ "${OPTIND}" -eq 1 ]; then
arguments+=("$1")
shift
fi
done
#--------------------------------------------------
# Get argument
#--------------------------------------------------
file_path="$(realpath "${arguments[${LBOUND}]}")"
folder_path="$(dirname "${file_path}")"
#--------------------------------------------------
if [ ! -d "${folder_path}" ]; then
echo_info "mkdir -p \"${folder_path}\"\n"
mkdir -p "${folder_path}"
fi
if [ ! -f "${file_path}" ] ; then
cat > "${file_path}" <<EOT
# ~/.bash_aliases will load this ".env" file
###> git ###
# Default jira server
JIRA_SERVER=
# Default git server
GIT_SERVER=
# Default git username
GIT_USERNAME=
# Use SSH
GIT_SSH=
###< git ###
###> ide ###
DEFAULT_IDE=
###< ide ###
EOT
fi
}
_create_env "${APP_USER_CONFIG_DIR}/.env"
## Load ".env"
function _load_env() {
function _usage() {
echo_success 'usage:' "$1" "$2"; echo_primary '_load_env [file] -h (help)\n'
}
#--------------------------------------------------
# Variables
#--------------------------------------------------
local file_path
#--------------------------------------------------
# Parse arguments
#--------------------------------------------------
local arguments=()
local OPTARG
local option
while [ "$#" -gt 0 ]; do
OPTIND=0
while getopts :h option; do
case "${option}" in
h) echo_warning '_load_env\n'
echo_success 'description:' 2 14; echo_primary 'Load ".env" file\n'
_usage 2 14
return 0;;
\?) echo_danger "error: invalid option \"${OPTARG}\"\n"
return 1;;
esac
done
if [ "${OPTIND}" -gt 1 ]; then
shift $(( OPTIND-1 ))
fi
if [ "${OPTIND}" -eq 1 ]; then
arguments+=("$1")
shift
fi
done
#--------------------------------------------------
# Validate argument count
#--------------------------------------------------
if [ "${#arguments[@]}" -lt 1 ]; then
echo_danger 'error: some mandatory parameter is missing\n'
_usage 2 8
return 1
fi
if [ "${#arguments[@]}" -gt 1 ]; then
echo_danger "error: too many arguments (${#arguments[@]})\n"
_usage 2 8
return 1
fi
#--------------------------------------------------
# Get argument
#--------------------------------------------------
file_path="${arguments[${LBOUND}]}"
#--------------------------------------------------
# Validate values
#--------------------------------------------------
if [ ! -f "${file_path}" ]; then
echo_danger "error: \"${file_path}\" file not found\n"
_usage 2 8
return 1
fi
# shellcheck source=/dev/null
. "${file_path}"
}
_load_env "${APP_USER_CONFIG_DIR}/.env"
## Set parameter to ".env" file
function _set_var() {
function _usage() {
echo_success 'usage:' "$1" "$2"; echo_primary '_set_env [parameter] [value] -f (file) -h (help)\n'
}
#--------------------------------------------------
# Variables
#--------------------------------------------------
local file_path
local parameter
local value
#--------------------------------------------------
# Parse arguments
#--------------------------------------------------
local arguments=()
local OPTARG
local option
while [ "$#" -gt 0 ]; do
OPTIND=0
while getopts :f:h option; do
case "${option}" in
f) file_path="${OPTARG}";;
h) echo_warning '_set_env\n'
echo_success 'description:' 2 14; echo_primary 'Set parameter to ".env" file\n'
_usage 2 14
return 0;;
:) echo_danger "error: \"${OPTARG}\" requires value\n"
return 1;;
\?) echo_danger "error: invalid option \"${OPTARG}\"\n"
return 1;;
esac
done
if [ "${OPTIND}" -gt 1 ]; then
shift $(( OPTIND-1 ))
fi
if [ "${OPTIND}" -eq 1 ]; then
arguments+=("$1")
shift
fi
done
#--------------------------------------------------
# Validate argument count
#--------------------------------------------------
if [ "${#arguments[@]}" -lt 2 ]; then
echo_danger 'error: some mandatory parameter is missing\n'
_usage 2 8
return 1
fi
if [ "${#arguments[@]}" -gt 2 ]; then
echo_danger "error: too many arguments (${#arguments[@]})\n"
_usage 2 8
return 1
fi
#--------------------------------------------------
# Validate values
#--------------------------------------------------
if [ ! -f "${file_path}" ]; then
echo_danger "error: \"${file_path}\" file not found\n"
_usage 2 8
return 1
fi
#--------------------------------------------------
# Get argument
#--------------------------------------------------
parameter="${arguments[${LBOUND}]}"
value="${arguments[$((LBOUND+1))]}"
#--------------------------------------------------
# Execute command
#--------------------------------------------------
# if parameter already exists
if < "${file_path}" grep -q "^${parameter}=.*$"; then
# escape forward slashes
value=$(echo "${arguments[$((LBOUND+1))]}" | sed 's/\//\\\//g')
echo_info "sed -i -E \"s/${parameter}=.*\$/${parameter}=${value}/\" \"${file_path}\"\n"
sed -i -E "s/${parameter}=.*\$/${parameter}=${value}/" "${file_path}"
return 0
fi
echo_info "printf '%s=%s\\\n' \"${parameter}\" \"${value}\" >> \"${file_path}\"\n"
printf '%s=%s\n' "${parameter}" "${value}" >> "${file_path}"
}
## Print bash_aliases documentation
function tango-help() {
alert_primary "TangoMan bash_aliases v${APP_VERSION}"
echo
echo_warning 'commands\n'
# shellcheck disable=SC1004
awk '/^function [a-zA-Z_-]+ ?\(\) ?\{/ { \
COMMAND = substr($2, 0, index($2, "(")-1); \
MESSAGE = substr(PREV, 4); \
if (substr(PREV, 1, 3) == "## " && substr(COMMAND, 1, 1) != "_") \
printf " \033[32m%-26s \033[37m%s\033[0m\n", COMMAND, MESSAGE; \
} { PREV = $0 }' ~/.bash_aliases
echo
echo_warning 'aliases\n'
# shellcheck disable=SC1004
awk -F ' ## ' '/^alias [a-zA-Z_-]+=.+ ## / { \
split($1, ALIAS, "="); \
printf " \033[32m%-20s \033[37m%s\033[0m\n", substr(ALIAS[1], 7), $2; \
}' ~/.bash_aliases
}
## Print current system infos
function tango-info() {
alert_primary "TangoMan System Infos"
echo_secondary "bash_aliases version: ${APP_VERSION}\n"
echo
echo_info "\$(LANG=C date)\n" 2
echo_primary "$(LANG=C date)\n" 2
echo
if [ -n "${HOSTNAME}" ]; then
echo_warning 'hostname\n'
echo_info "echo \${HOSTNAME}\n" 2
echo_primary "${HOSTNAME}\n" 2
echo
fi
if [ -n "${HOST}" ]; then
echo_warning 'host\n'
echo_info "echo \${HOST}\n" 2
echo_primary "${HOST}\n" 2
echo
fi
if [ -n "$(command -v uname)" ]; then
echo_warning 'box\n'
echo_info "uname -n | sed s/\$(whoami)-//\n" 2
echo_primary "$(uname -n | sed s/"$(whoami)"-//)\n" 2
echo
fi
if [ -n "$(command -v whoami)" ]; then
echo_warning 'user name\n'
echo_info "whoami\n" 2
echo_primary "$(whoami)\n" 2
echo
fi
if [ -n "$(command -v id)" ]; then
echo_warning 'user id\n'
echo_info "id --user\n" 2
echo_primary "$(id --user)\n" 2
echo
fi
if [ -n "$(command -v id)" ]; then
echo_warning 'user groups\n'
echo_info "id --groups\n" 2
echo_primary "$(id --groups)\n" 2
echo
fi
if [ -n "$(command -v id)" ]; then
echo_warning 'user groups id\n'
echo_info "id --groups --name\n" 2
echo_primary "$(id --groups --name)\n" 2
echo
fi
if [ -n "$(command -v hostname)" ]; then
echo_warning 'local ip\n'
echo_info "hostname -i\n" 2
echo_primary "$(hostname -i)\n" 2
echo
fi
if [ -n "$(hostname -I 2>/dev/null)" ]; then
echo_warning 'network ip\n'
echo_info "hostname -I | cut -d' ' -f1\n" 2
echo_primary "$(hostname -I | cut -d' ' -f1)\n" 2
echo
fi
if [ -n "${USER}" ]; then
echo_warning 'user\n'
echo_info "echo \${USER}\n" 2
echo_primary "${USER}\n" 2
echo
fi
if [ -z "${USERNAME}" ]; then
echo_warning 'username\n'
echo_info "echo \${USERNAME}\n" 2
echo_primary "${USERNAME}\n" 2
echo
fi
if [ -n "${SHELL}" ]; then
echo_warning 'shell\n'
echo_info "echo \${SHELL}\n" 2
echo_primary "${SHELL}\n" 2
echo
fi
if [ -n "${OSTYPE}" ]; then
echo_warning 'ostype\n'
echo_info "echo \${OSTYPE}\n" 2
echo_primary "${OSTYPE}\n" 2
echo
fi
if [ -n "$(lsb_release -d 2>/dev/null)" ]; then
echo_warning 'os version\n'
echo_info "lsb_release -d | sed 's/Description:\\t//'\n" 2
echo_primary "$(lsb_release -d | sed 's/Description:\t//')\n" 2
echo
fi
if [ -n "$(command -v bash)" ]; then
echo_warning 'bash version\n'
echo_info "bash --version | grep -oE 'version\s[0-9]+\.[0-9]+\.[0-9]+.+$' | sed 's/version //'\n" 2
echo_primary "$(bash --version | grep -oE 'version\s[0-9]+\.[0-9]+\.[0-9]+.+$' | sed 's/version //')\n" 2
echo
fi
if [ -n "$(command -v zsh)" ]; then
echo_warning 'zsh version\n'
echo_info "zsh --version | sed 's/^zsh //'\n" 2
echo_primary "$(zsh --version | sed 's/^zsh //')\n" 2
echo
fi
if [ -n "${DESKTOP_SESSION}" ]; then
echo_warning 'desktop_session\n'
echo_info "echo \${DESKTOP_SESSION}\n" 2
echo_primary "${DESKTOP_SESSION}\n" 2
echo
fi
if [ -n "${XDG_CURRENT_DESKTOP}" ]; then
echo_warning 'xdg_current_desktop\n'
echo_info "echo \${XDG_CURRENT_DESKTOP}\n" 2
echo_primary "${XDG_CURRENT_DESKTOP}\n" 2
echo
fi
if [ -n "$(command -v lshw)" ]; then
echo_warning 'network card\n'
echo_info "lshw -c network\n" 2
echo_primary "$(lshw -c network)\n"
echo
fi
if [ -n "$(command -v ip)" ]; then
echo_warning 'ip (ifconfig)\n'
echo_info "ip address\n" 2
echo_primary "$(ip address)\n" 2
echo
fi
if [ -n "$(command -v lshw)" ]; then
echo_warning 'System infos\n'
echo_info "lshw -short\n" 2
echo_primary "$(lshw -short)\n"
echo
fi
}
## Reload aliases (after update)
function tango-reload() {
echo_secondary 'Reload Aliases\n'
echo
case "${SHELL}" in
'/usr/bin/zsh')
echo_info 'source ~/.zshrc\n'
# shellcheck source=/dev/null
source ~/.zshrc
echo_success ".bash_aliases version: ${APP_VERSION} reloaded\n"
;;
'/usr/bin/bash')
echo_info 'source ~/.bashrc\n'
# shellcheck source=/dev/null
source ~/.bashrc
echo_success ".bash_aliases version: ${APP_VERSION} reloaded\n"
;;
'/bin/bash')
echo_info 'source ~/.bashrc\n'
# shellcheck source=/dev/null
source ~/.bashrc
echo_success ".bash_aliases version: ${APP_VERSION} reloaded\n"
;;
\?)
echo_danger "error: Shell \"${SHELL}\" not handled\n"
return 1
;;
esac
}
## Update tangoman bash_aliases
function tango-update() {
function _usage() {
echo_success 'usage:' "$1" "$2"; echo_primary 'tango-update -h (help)\n'
}
#--------------------------------------------------
# Check git installation
#--------------------------------------------------
if [ ! -x "$(command -v git)" ]; then
echo_danger 'error: git required, enter: "sudo apt-get install -y git" to install\n'
return 1
fi
#--------------------------------------------------
# Check curl installation
#--------------------------------------------------
if [ ! -x "$(command -v curl)" ]; then
echo_danger 'error: curl required, enter: "sudo apt-get install -y curl" to install\n'
return 1
fi
#--------------------------------------------------
# Variables
#--------------------------------------------------
local latest_version
local local_version=()
local remote_version=()
local result
# shellcheck disable=2153
local key=${LBOUND}
#--------------------------------------------------
# Parse options
#--------------------------------------------------
local option
while getopts :h option; do
case "${option}" in
h) echo_warning 'tango-update\n';
echo_success 'description:' 2 14; echo_primary "Update \"${APP_AUTHOR}\" \"${APP_NAME}\"\n"
_usage 2 14
return 0;;
\?) echo_danger "error: invalid option \"${OPTARG}\"\n"
return 1;;
esac
done
#--------------------------------------------------
# Get repository latest tag
#--------------------------------------------------
latest_version="$(curl --silent GET "https://api.github.com/repos/${APP_AUTHOR}/${APP_NAME}/tags" | grep -m 1 '"name":' | sed -E 's/.*"[vV]?([^"]+)".*/\1/')"
if [ -z "${latest_version}" ]; then
echo_danger "error: could not check ${APP_AUTHOR} \"${APP_NAME}\" latest available version\n"
return 1
fi
#--------------------------------------------------
# Compare local with remote version
#--------------------------------------------------
# split each version string with dot character (option 4, short syntax but not shellcheck valid)
# shellcheck disable=2207
local_version=($(echo "${APP_VERSION}" | tr '.' ' '))
# shellcheck disable=2207
remote_version=($(echo "${latest_version}" | tr '.' ' '))
if [ "${SHELL}" = /usr/bin/zsh ]; then
UBOUND=3
else
UBOUND=2
fi
while [ "${key}" -lt "${UBOUND}" ]; do
if [ "${local_version[$key]}" -eq "${remote_version[$key]}" ]; then
key=$(( key + 1 ))
continue
elif [ "${local_version[$key]}" -lt "${remote_version[$key]}" ]; then
result='<'
fi
break
done
#--------------------------------------------------
# Check update available
#--------------------------------------------------
if [ "${result}" = '<' ]; then
echo_primary 'Update available for TangoMan "bash_aliases"\n'
echo_danger "your version: ${APP_VERSION}\n"
echo_warning "latest version: ${latest_version}\n"
else
echo_info "your version: ${APP_VERSION}\n"
echo_info "latest version: ${latest_version}\n"
echo_success 'Your version of TangoMan "bash_aliases" is up-to-date\n'
return 0
fi
#--------------------------------------------------
# Prompt user
#--------------------------------------------------
echo_success 'Do you want to install new version? (yes/no) [no]: '
read -r USER_PROMPT
if [[ ! "${USER_PROMPT}" =~ ^[Yy](ES|es)?$ ]]; then
return 0
fi
#--------------------------------------------------
# Check install folder and clone repository
if [ ! -d "${APP_INSTALL_DIR}" ]; then
# Set install dir to default
APP_INSTALL_DIR=/home/tangoman75/Documents/bash_aliases
echo_warning "\"${APP_INSTALL_DIR}\" not found, cloning source repository\n"
echo_info "git clone --depth=1 \"${APP_REPOSITORY}\" \"${APP_INSTALL_DIR}\"\n"
git clone --depth=1 "${APP_REPOSITORY}" "${APP_INSTALL_DIR}"
fi
#--------------------------------------------------
echo_info "\"${APP_INSTALL_DIR}\" || exit 1\n"
cd "${APP_INSTALL_DIR}" || exit 1
echo_info 'git pull\n'
git pull
echo_info 'make install silent=true\n'
make install silent=true
#--------------------------------------------------
tango-reload
}
#--------------------------------------------------
# general aliases
#--------------------------------------------------
alias cc='clear' ## Clear terminal
alias ccc='history -c && clear' ## Clear terminal & history
alias h='echo_info "history\n"; history' ## Print history
alias hh='echo_info "history|grep\n"; history|grep' ## Search history
alias hhh='cut -f1 -d" " ~/.bash_history|sort|uniq -c|sort -nr|head -n 30' ## Print 30 most used bash commands
alias ll='echo_info "ls -lFh\n"; ls -lFh' ## List non hidden files human readable
alias lll='echo_info "ls -alFh\n"; ls -alFh' ## List all files human readable
alias mkdir='mkdir -p' ## Create directory and required parent directories
alias unmount='umount' ## Unmout drive
alias xx='exit' ## Exit terminal
alias ..='cd ..' ## Jump back 1 directory
alias ...='cd ../../' ## Jump back 2 directories at a time
alias ....='cd ../../../' ## Jump back 3 directories at a time
#--------------------------------------------------
# text editor
#--------------------------------------------------
if [ -x "$(command -v subl)" ]; then
alias s='subl' ## Open with sublime text (requires subl)
fi
#--------------------------------------------------
# clipboard
#--------------------------------------------------
if [ -x "$(command -v xsel)" ]; then
alias xcopy='xsel --input --clipboard' ## Copy to clipboard with xsel (requires xsel)
alias xpaste='xsel --output --clipboard' ## Paste from clipboard with xsel (requires xsel)
fi
if [ -x "$(command -v xclip)" ]; then
alias clip='xclip -selection clipboard' ## Copy selection to clipboard with xclip (requires xclip)
fi
## List installed ides
function _list_ides() {
#--------------------------------------------------
# Variables
#--------------------------------------------------
local ide
local ides=(atom code nano phpstorm pycharm pycharm-community subl vim webstorm)
local result=''
#--------------------------------------------------
for ide in "${ides[@]}"; do
if [ "$(command -v "${ide}")" ]; then
result+="${ide} "
fi
done
echo "${result}"
}
# Default ide
# shellcheck disable=SC2034
DEFAULT_IDE=
alias ide='open-in-ide' ## open-in-ide alias
## Open given files in ide
function open-in-ide() {
function _usage() {
echo_success 'usage:' "$1" "$2"; echo_primary 'open-in-ide [...files] -l [line] -i (ide) -I (select ide) -h (help)\n'
}
#--------------------------------------------------
# Variables
#--------------------------------------------------
local ide
local ides
local line
local select_ide=false
#--------------------------------------------------
ides="$(_list_ides)"
#--------------------------------------------------
# Parse arguments
#--------------------------------------------------
local arguments=()
local OPTARG
local option
while [ "$#" -gt 0 ]; do
OPTIND=0
while getopts :i:l:Ih option; do
case "${option}" in
i) ide="${OPTARG}";;
l) line="${OPTARG}";;
I) select_ide=true;;
h) echo_warning 'open-in-ide\n';
echo_success 'description:' 2 14; echo_primary 'Open given files in ide\n'
_usage 2 14
echo_success 'note:' 2 14; echo_warning "available ides: ${ides}\n"
return 0;;
:) echo_danger "error: \"${OPTARG}\" requires value\n"
return 1;;
\?) echo_danger "error: invalid option \"${OPTARG}\"\n"
return 1;;
esac
done
if [ "${OPTIND}" -gt 1 ]; then
shift $(( OPTIND-1 ))
fi
if [ "${OPTIND}" -eq 1 ]; then
arguments+=("$1")
shift
fi
done
#--------------------------------------------------
# Check handler installation
#--------------------------------------------------
if [ ! -x "$(command -v phpstorm-url-handler)" ]; then
echo_danger 'error: ide-url-handler required, visit: "https://github.com/TangoMan75/ide-url-handler" to install\n'
return 1
fi
#--------------------------------------------------
# Validate argument count
#--------------------------------------------------
if [ -z "${arguments[${LBOUND}]}" ]; then
echo_danger 'error: some mandatory parameter is missing\n'
_usage 2 8
return 1
fi
#--------------------------------------------------
# Validate values
#--------------------------------------------------
if [ -n "${line}" ]; then
if [ "${line}" -lt 1 ] || [[ ! "${line}" =~ ^[0-9]+$ ]]; then
echo_danger 'error: "line" should be a positive integer\n'
_usage 2 8
return 1
fi
fi
#--------------------------------------------------
# Set default values
#--------------------------------------------------
if [ -z "${ide}" ] && [ -z "${DEFAULT_IDE}" ]; then
select_ide=true
fi
if [ -z "${line}" ]; then
line=1
fi
#--------------------------------------------------
# Interactive select ide
#--------------------------------------------------
if [ "${select_ide}" = true ]; then
# prompt user values
PS3=$(echo_success 'Please select default ide : ')
select ide in $(_list_ides) 'other'; do
# validate selection
for item in $(_list_ides) 'other'; do
# find selected item
if [[ "${item}" == "${ide}" ]]; then
# break two encapsulation levels
break 2;
fi
done
done
while [ "${ide}" = 'other' ] || [ -z "${ide}" ]; do
echo_success 'Please enter default ide name : '
read -r ide
# sanitize user entry
ide=$(echo "${ide}" | tr -d '[:space:]')
done
fi
#--------------------------------------------------
# Set default value
#--------------------------------------------------
if [ -z "${ide}" ]; then
ide="${DEFAULT_IDE}"
fi
#--------------------------------------------------
# Validate selection
#--------------------------------------------------
if [ -z "$(command -v "${ide}")" ]; then