-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions
executable file
·1602 lines (1254 loc) · 37.6 KB
/
functions
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
## Fix prompt and title function
function fixprompt() {
#tab title with the current folder name
export PROMPT_COMMAND='echo -ne "\033]0; ${PWD##*/}\007"'
export PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND ;} history -a"
}
## Progress
function echo_progress() {
echo -e "`mytimestamp` - ${*}"
}
## Success
function echo_success() {
echo -e "$GREEN`mytimestamp` - ${*}${NOCOLOR}"
}
## Failure
function echo_failure() {
echo -e "$RED`mytimestamp` - ${*}${NOCOLOR}"
}
# Simple calculator
function calc() {
local result="";
result="$(printf "scale=10;$*\n" | bc --mathlib | tr -d '\\\n')";
# └─ default (when `--mathlib` is used) is 20
#
if [[ "$result" == *.* ]]; then
# improve the output for decimal numbers
printf "$result" |
sed -e 's/^\./0./' `# add "0" for cases like ".5"` \
-e 's/^-\./-0./' `# add "0" for cases like "-.5"`\
-e 's/0*$//;s/\.$//'; # remove trailing zeros
else
printf "$result";
fi;
printf "\n";
}
# Reload Chrome via OSA
# more info: https://developer.apple.com/library/mac/documentation/AppleScript/Conceptual/AppleScriptLangGuide/introduction/ASLR_intro.html
function reload-chrome() {
osascript -e '
tell application "Google Chrome"
tell the active tab of its first window
reload
end tell
end tell
'
}
# Word frequency
function wordFreq() {
ruby -F'[^a-zA-Z]+' -ane '
BEGIN { $words = Hash.new(0) }
$F.each { |word| $words[word.downcase] += 1 }
END { $words.each { |word, i| printf "%3d %s\n", i, word } }
' | sort -rn
}
# Change the bash title
function title {
if [ "$1" ]
then
unset PROMPT_COMMAND
echo -ne "\033]0;${*}\007"
else
export PROMPT_COMMAND='echo -ne "\033]0;${PWD/#$HOME/~}\007"'
fi
}
# get a random word from the system dictionary.
function randomWord() {
resource=/usr/share/dict/words;
lineNum=$(cat $resource | wc -l);
cat -n $resource | grep -w $(jot -r 1 1 $lineNum) | cut -f2;
}
#get a random line of a file
function randomLine() {
randomFile="head -$((${RANDOM} % `wc -l < $1` + 1)) $1"
echo `$randomFile | tail -1`
}
# get a random file of a dir
function randomFile() {
files=($1/*.*)
echo ${files[RANDOM % ${#files[@]}]}
}
# Create a new directory and enter it
function mkd() {
mkdir -p "$@" && cd "$_";
}
# Pub key send
function pubkeysend() {
ssh-copy-id -i ~/.ssh/id_rsa.pub $1
}
# SSH + CD
function sshcd() {
ssh -t $1 "cd $2; bash"
}
# Search and Replace based on the params
function srall {
find . -type f -exec sed -i '' s/$1/$2/g {} +
}
# Search and Replace based on params - with extension
function srext {
find . -type f -name '*.$1' -exec sed -i '' s/$2/$3/g {} +
}
# Filter Specific column
function col {
awk -v col=$1 '{print $col}'
}
# Skip X words in line
function skip {
n=$(($1 + 1))
cut -d' ' -f$n-
}
# Change working directory to the top-most Finder window location
function cdfinder() { # cdf
cd "$(osascript -e 'tell app "Finder" to POSIX path of (insertion location as alias)')";
}
# Create a .tar.gz archive, using `zopfli`, `pigz` or `gzip` for compression
function targz() {
local tmpFile="${@%/}.tar";
tar -cvf "${tmpFile}" --exclude=".DS_Store" "${@}" || return 1;
size=$(
stat -f"%z" "${tmpFile}" 2> /dev/null; # OS X `stat`
stat -c"%s" "${tmpFile}" 2> /dev/null # GNU `stat`
);
local cmd="";
if (( size < 52428800 )) && hash zopfli 2> /dev/null; then
# the .tar file is smaller than 50 MB and Zopfli is available; use it
cmd="zopfli";
else
if hash pigz 2> /dev/null; then
cmd="pigz";
else
cmd="gzip";
fi;
fi;
echo "Compressing .tar using \`${cmd}\`…";
"${cmd}" -v "${tmpFile}" || return 1;
[ -f "${tmpFile}" ] && rm "${tmpFile}";
echo "${tmpFile}.gz created successfully.";
}
# Determine size of a file or total size of a directory
function fs() {
if du -b /dev/null > /dev/null 2>&1; then
local arg=-sbh;
else
local arg=-sh;
fi
if [[ -n "$@" ]]; then
du $arg -- "$@";
else
du $arg .[^.]* ./*;
fi;
}
## find which pid is listening on port
function portuse {
lsof -n -i4TCP:"$@" | grep LISTEN
}
# Use Git’s colored diff when available
hash git &>/dev/null;
if [ $? -eq 0 ]; then
function diff() {
git diff --no-index --color-words "$@";
}
fi;
# Create a data URL from a file
function dataurl() {
local mimeType=$(file -b --mime-type "$1");
if [[ $mimeType == text/* ]]; then
mimeType="${mimeType};charset=utf-8";
fi
echo "data:${mimeType};base64,$(openssl base64 -in "$1" | tr -d '\n')";
}
# Create a git.io short URL
function gitio() {
if [ -z "${1}" -o -z "${2}" ]; then
echo "Usage: \`gitio slug url\`";
return 1;
fi;
curl -i https://git.io/ -F "url=${2}" -F "code=${1}";
}
# Start an HTTP server from a directory, optionally specifying the port
function pythonserver() {
local port="${1:-8000}";
sleep 1 && open "http://localhost:${port}/" &
# Set the default Content-Type to `text/plain` instead of `application/octet-stream`
# And serve everything as UTF-8 (although not technically correct, this doesn’t break anything for binary files)
python -c $'import SimpleHTTPServer;\nmap = SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map;\nmap[""] = "text/plain";\nfor key, value in map.items():\n\tmap[key] = value + ";charset=UTF-8";\nSimpleHTTPServer.test();' "$port";
}
# Start a PHP server from a directory, optionally specifying the port
# (Requires PHP 5.4.0+.)
function phpserver() {
local port="${1:-4000}";
local ip=${2:-$(ipconfig getifaddr en0)};
sleep 1 && open "http://${ip}:${port}/" &
php -S "${ip}:${port}";
}
# Compare original and gzipped file size
function gz() {
local origsize=$(wc -c < "$1");
local gzipsize=$(gzip -c "$1" | wc -c);
local ratio=$(echo "$gzipsize * 100 / $origsize" | bc -l);
printf "orig: %d bytes\n" "$origsize";
printf "gzip: %d bytes (%2.2f%%)\n" "$gzipsize" "$ratio";
}
# Syntax-highlight JSON strings or files
# Usage: `json '{"foo":42}'` or `echo '{"foo":42}' | json`
#python -c "import json,sys; print json.dumps(json.load(sys.stdin),indent=4,separators=(',',': '))"
function json() {
jsonnocolor | pygmentize -l javascript;
}
function jsonnocolor() {
if [ -t 0 ]; then # argument
python -mjson.tool <<< "$*"
else # pipe
python -mjson.tool
fi;
}
# bsh
function bsh() {
java -cp .:/usr/local/bin/bsh.jar bsh.Interpreter $*
}
function bshgui() {
java -cp .:/usr/local/bin/bsh.jar bsh.Interpreter $*
}
# xml format
function xmlformat() {
xmllint --format -
}
# Trim spaces
function trim () {
while read -r line; do echo "$line"; done;
}
# Run `dig` and display the most useful info
function digga() {
dig +nocmd "$1" any +multiline +noall +answer;
}
# UTF-8-encode a string of Unicode symbols
function escape() {
printf "\\\x%s" $(printf "$@" | xxd -p -c1 -u);
# print a newline unless we’re piping the output to another program
if [ -t 1 ]; then
echo ""; # newline
fi;
}
# Decode \x{ABCD}-style Unicode escape sequences
function unidecode() {
perl -e "binmode(STDOUT, ':utf8'); print \"$@\"";
# print a newline unless we’re piping the output to another program
if [ -t 1 ]; then
echo ""; # newline
fi;
}
# Get a character’s Unicode code point
function codepoint() {
perl -e "use utf8; print sprintf('U+%04X', ord(\"$@\"))";
# print a newline unless we’re piping the output to another program
if [ -t 1 ]; then
echo ""; # newline
fi;
}
# Show all the names (CNs and SANs) listed in the SSL certificate
# for a given domain
function getcertnames() {
if [ -z "${1}" ]; then
echo "ERROR: No domain specified.";
return 1;
fi;
local domain="${1}";
echo "Testing ${domain}…";
echo ""; # newline
local tmp=$(echo -e "GET / HTTP/1.0\nEOT" \
| openssl s_client -connect "${domain}:443" -servername "${domain}" 2>&1);
if [[ "${tmp}" = *"-----BEGIN CERTIFICATE-----"* ]]; then
local certText=$(echo "${tmp}" \
| openssl x509 -text -certopt "no_aux, no_header, no_issuer, no_pubkey, \
no_serial, no_sigdump, no_signame, no_validity, no_version");
echo "Common Name:";
echo ""; # newline
echo "${certText}" | grep "Subject:" | sed -e "s/^.*CN=//" | sed -e "s/\/emailAddress=.*//";
echo ""; # newline
echo "Subject Alternative Name(s):";
echo ""; # newline
echo "${certText}" | grep -A 1 "Subject Alternative Name:" \
| sed -e "2s/DNS://g" -e "s/ //g" | tr "," "\n" | tail -n +2;
return 0;
else
echo "ERROR: Certificate not found.";
return 1;
fi;
}
# `s` with no arguments opens the current directory in Sublime Text, otherwise
# opens the given location
function s() {
if [ $# -eq 0 ]; then
subl .;
else
subl "$@";
fi;
}
# `a` with no arguments opens the current directory in Atom Editor, otherwise
# opens the given location
function a() {
if [ $# -eq 0 ]; then
atom .;
else
atom "$@";
fi;
}
# `v` with no arguments opens the current directory in Vim, otherwise opens the
# given location
function v() {
if [ $# -eq 0 ]; then
vim .;
else
vim "$@";
fi;
}
# `o` with no arguments opens the current directory, otherwise opens the given
# location
function o() {
if [ $# -eq 0 ]; then
open .;
else
open "$@";
fi;
}
# `tre` is a shorthand for `tree` with hidden files and color enabled, ignoring
# the `.git` directory, listing directories first. The output gets piped into
# `less` with options to preserve color and line numbers, unless the output is
# small enough for one screen.
function tre() {
tree -aC -I '.git|node_modules|bower_components' --dirsfirst "$@" | less -FRNX;
}
# whois a domain or a URL
function whois() {
local domain=$(echo "$1" | awk -F/ '{print $3}') # get domain from URL
if [ -z $domain ] ; then
domain=$1
fi
echo "Getting whois record for: $domain …"
# avoid recursion
# this is the best whois server
# strip extra fluff
/usr/bin/whois -h whois.internic.net $domain | sed '/NOTICE:/q'
}
# resolve IP of hosts
function resolveip() {
ping -c 1 $1 | regex1 "PING.*\((.*)\):"
}
#get my remote ip
# could use: ifconfig.me/ip
function httpip() {
curl https://ipinfo.io/ip
}
function httphost() {
curl -sk http://ifconfig.me/host
}
function httpua() {
curl http://httpbin.org/user-agent
}
function httpgetinfo() {
curl http://httpbin.org/get?show_env=1
}
alias curlanon='curl --proxy socks5://bruno1:8080'
function localip(){
function _localip(){ echo "📶 - "$(ipconfig getifaddr "$1"); }
export -f _localip
local purple="\x1B\[35m" reset="\x1B\[m"
networksetup -listallhardwareports | \
sed -r "s/Hardware Port: (.*)/${purple}\1${reset}/g" | \
sed -r "s/Device: (en.*)$/_localip \1/e" | \
sed -r "s/Ethernet Address:/📘 /g" | \
sed -r "s/(VLAN Configurations)|==*//g"
}
# Extract archives - use: extract <file>
# Based on http://dotfiles.org/~pseup/.bashrc
function extract() {
if [ -f "$1" ] ; then
local filename=$(basename "$1")
local foldername="${filename%%.*}"
local fullpath=`perl -e 'use Cwd "abs_path";print abs_path(shift)' "$1"`
local didfolderexist=false
if [ -d "$foldername" ]; then
didfolderexist=true
read -p "$foldername already exists, do you want to overwrite it? (y/n) " -n 1
echo
if [[ $REPLY =~ ^[Nn]$ ]]; then
return
fi
fi
mkdir -p "$foldername" && cd "$foldername"
case $1 in
*.tar.bz2) tar xjf "$fullpath" ;;
*.tar.gz) tar xzf "$fullpath" ;;
*.tar.xz) tar Jxvf "$fullpath" ;;
*.tar.Z) tar xzf "$fullpath" ;;
*.tar) tar xf "$fullpath" ;;
*.taz) tar xzf "$fullpath" ;;
*.tb2) tar xjf "$fullpath" ;;
*.tbz) tar xjf "$fullpath" ;;
*.tbz2) tar xjf "$fullpath" ;;
*.tgz) tar xzf "$fullpath" ;;
*.txz) tar Jxvf "$fullpath" ;;
*.rar) unrar x "$fullpath" ;;
*.zip) unzip "$fullpath" ;;
*) echo "'$1' cannot be extracted via extract()" && cd .. && ! $didfolderexist && rm -r "$foldername" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
# Add reminder to Reminders.app (OS X 10.8)
# Usage: `remind 'foo'` or `echo 'foo' | remind`
function remind () {
local text
if [ -t 0 ]; then
text="$1" # argument
else
text=$(cat) # pipe
fi
osascript >/dev/null <<EOF
tell application "Reminders"
tell the default list
make new reminder with properties {name:"$text"}
end tell
end tell
EOF
}
# Add note to Notes.app (OS X 10.8)
# Usage: `note 'title' 'body'` or `echo 'body' | note`
# Title is optional
function note () {
local title
local body
if [ -t 0 ]; then
title="$1"
body="$2"
else
title=$(cat)
fi
osascript >/dev/null <<EOF
tell application "Notes"
tell account "iCloud"
tell folder "Notes"
make new note with properties {name:"$title", body:"$title" & "<br><br>" & "$body"}
end tell
end tell
end tell
EOF
}
# Top 10 BASH commands used
# from stackoverflow.com/questions/68372/what-is-your-single-most-favorite-command-line-trick-using-bash#answer-68390
function top10 () {
history | awk 'BEGIN {FS="[ \t]+|\\|"} {print $3}' | sort | uniq -c | sort -nr | head
}
# Copy w/ progress
function cp_p () {
rsync -WavP --human-readable --progress $1 $2
}
# Read JSON Keys
function jsonkeys() {
python -c $'
import json,sys;
obj=json.load(sys.stdin);
for each in obj.keys():
print "%s" % (each)
';
}
# Read JSON Tag
function jsontag() {
python -c $'
import json,sys;
obj=json.load(sys.stdin);
result=obj["'"$1"'"];
if isinstance(result, str):
print(result);
else:
print(json.dumps(result, indent=4));
';
}
# Read JSON Index
function jsonindex() {
python -c $'
import json,sys;
obj=json.load(sys.stdin);
result=obj['"$1"'];
if isinstance(result, str):
print(result);
else:
print(json.dumps(result, indent=4));
';
}
# Read JSON Val in Array
function jsonarrayval() {
python -c $'
import json,sys;
obj=json.load(sys.stdin);
for inst in (item for item in obj):
if "'$1'" in inst:
result = inst["'$1'"]
if isinstance(result, str):
print(result);
else:
print(json.dumps(result, indent=4));
';
}
function paramtest() {
echo "First: $1";
echo "Second: ${*:2}";
}
#Get my timestamp
function mytimestamp() {
date +"%Y-%m-%d %T"
}
#Requote
#https://stackoverflow.com/questions/3260920/preserving-quotes-in-bash-function-parameters
function requote() {
local res=""
for x in "${@}" ; do
# try to figure out if quoting was required for the $x:
grep -q "[[:space:]]" <<< "$x" && res="${res} '${x}'" || res="${res} ${x}"
done
# remove first space and print:
sed -e 's/^ //' <<< "${res}"
}
#Build Jenkins and wait until finished
function jenkinsbuildandwait() {
echo "`mytimestamp` - Building $1";
if [ $# -eq 2 ]; then
curl -v -X POST "$1/build?delay=5sec" -H 'Content-Type: application/x-www-form-urlencoded' --data "$2"
else
curl -v -X POST "$1/buildWithParameters?delay=5sec"
fi
jenkinswaitforbuild "$1/lastBuild"
echo "`mytimestamp` - Finished $1!";
}
# wait for build on jenkins
function jenkinswaitforbuild() {
url="$1/api/json"
#echo "URL: $url"
sleep 10
lastBuildResult=$(curl -s "$url" | jsontag result)
while [ "$lastBuildResult" != "SUCCESS" ];
do
echo "`mytimestamp` - Waiting for $1 to complete (result: $lastBuildResult)";
sleep 10
lastBuildResult=$(curl -s "$url" | jsontag result)
done
if hash terminal-notifier 2> /dev/null; then
terminal-notifier -title "Jenkins Build" -message "Job '$1' was finished"
fi
}
#Random say voice
function sayrandomvoice() {
declare -a expressions=('Agnes' 'Kathy' 'Princess' 'Vicki' 'Victoria' 'Alex' 'Bruce' 'Fred' 'Junior' 'Ralph' 'Albert' 'Bad News' 'Bahh' 'Bells' 'Boing' 'Bubbles' 'Cellos' 'Deranged' 'Good News' 'Hysterical' 'Pipe Organ' 'Trinoids' 'Whisper' 'Zarvox');
index=$( jot -r 1 0 $((${#expressions[@]} - 1)) )
say -v "${expressions[index]}" $*
}
## Echo Jump Dir
function jecho() {
echo "$(j $* | nocolor)";
}
## No colors. :)
function nocolor() {
sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g" | sed "s/\x0f//g"
}
##kill processes with name
function grepkill() {
if [ $# -ne 2 ]; then
echo_failure "usage: grepkill -SIG process" >&2
return;
fi
ps aux | grep -ie "$2" | grep -v "grep" | awk '{print $2}' | xargs kill $1
}
##sort by timestamp alphabetically
function sortbytimestamp() {
for fi in `gls -1v | grep -v ".txt"`
do
echo "Touch $fi..."
touch $fi
sleep 0.5
done
}
# ScreenShot
function ss() {
echo "[ss - press space to select rectangle]"
if [ "$1" != "" ]; then
screencapture -iW $*
else
#clipboard
screencapture -ciW
fi
}
# Safari tools
function safariurl() {
osascript -e 'tell application "Safari" to get URL of current tab of front window'
}
function safarisource() {
osascript -e 'tell application "Safari" to get source of current tab of front window'
}
function safaritext() {
osascript -e 'tell application "Safari" to get text of current tab of front window'
}
#Chrome tools
function chromeurl() {
osascript -e 'tell app "Google Chrome" to URL of active tab of window 1'
}
# list selected finder files
function finderselected() {
osascript <<EOT
tell application "Finder"
set theFiles to selection
set theList to ""
repeat with aFile in theFiles
set theList to theList & POSIX path of (aFile as alias) & "\n"
end repeat
theList
end tell
EOT
}
function regex1 {
gawk 'match($0,/'"$1"'/, ary) { print ary['${2:-'1'}'] }';
}
# Colorize
function logcolorize() {
SEDNOCOLOR="\o033[0m" # unsets color to term's fg color
SEDRED='\o033[31m' # Red
SEDGREEN='\o033[32m' # Green
SEDYELLOW='\o033[33m' # Yellow
SEDBLUE='\o033[34m' # Blue
sed -e 's/\(.*\(INFO\|DEBUG\).*\)/'"$SEDBLUE"'\1'"$SEDNOCOLOR"'/' \
-e 's/\(.*WARN.*\)/'"$SEDYELLOW"'\1'"$SEDNOCOLOR"'/' \
-e 's/\(.*ERROR.*\)/'"$SEDRED"'\1'"$SEDNOCOLOR"'/' \
-e 's/\(.*SEVERE.*\)/'"$SEDRED"'\1'"$SEDNOCOLOR"'/' \
-e 's/\(.*at.*(\(.*:.*\|Native Method\)).*\)/'"$SEDRED"'\1'"$SEDNOCOLOR"'/'
}
## clean CURL request
function cleancurlcb() {
pbpaste | cleancurl | pbcopy
}
## clean CURL request
function cleancurl() {
#curl 'https://totvslabs.fluigdata.com:8443/api/v1/resultpanel/valuedist?pageSize=20&sortBy=value&sortOrder=DESC' -H 'Pragma: no-cache' -H 'Origin: http://totvslabs.fluigdata.com:3002' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: en-US,en;q=0.8,pt-BR;q=0.6,pt;q=0.4' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36' -H 'Content-Type: application/json;charset=UTF-8' -H 'Accept: application/json, text/plain, */*' -H 'Cache-Control: no-cache' -H 'Authorization: 454d6e002cf911e69ff182c16efcba4d' -H 'Connection: keep-alive' -H 'Referer: http://totvslabs.fluigdata.com:3002/' -H 'DNT: 1' --data-binary '[{"index":"STAGING","type":"d05a7b0ec55547e5feb0d5bcdff6cc34_customer","fields":["name","state"]}]' --compressed
perl -pe 's/-H '\''Pragma: .*?'\''//g' | \
perl -pe 's/-H '\''Origin: .*?'\''//g' | \
perl -pe 's/-H '\''User-Agent: .*?'\''//g' | \
perl -pe 's/-H '\''Accept-Encoding: .*?'\''//g' | \
perl -pe 's/-H '\''Accept-Language: .*?'\''//g' | \
perl -pe 's/-H '\''Referer: .*?'\''//g' | \
perl -pe 's/-H '\''Cache-Control: .*?'\''//g' | \
perl -pe 's/-H '\''Connection: .*?'\''//g'
}
## network tools - nmap (aroundme, aroundme 16)
function aroundme() {
nmap -sP `mysubnet $*`
}
function aroundmemac() {
nmap -sP -n `mysubnet $*`
}
function aroundmeos() {
sudo nmap -O --osscan-guess `mysubnet $*`
}
function mysubnet() {
echo "`localip | cut -d"." -f1-3`.0/${1-24}"
}
# Removes elements from an array based on a given regex pattern.
# Usage: filter_arr pattern array
# Usage: filter_arr pattern element1 element2 ...
#https://stackoverflow.com/questions/3578584/bash-how-to-delete-elements-from-an-array-based-on-a-pattern
function filter_arr() {
arr=($@)
arr=(${arr[@]:1})
dirs=($(for i in ${arr[@]}
do echo $i
done | grep $1))
echo ${dirs[@]}
}
## Get protocol + host of an url
function urlhost() {
perl -n -e 'm{(https?://[^/]+)(/[^?]+)};print $1 . "/"'
}
### brew formulas
function brewformulas() {
for formulae in ${1:-~/githubworkspace/homebrew/Library/Formula/*};
do
filename=${formulae##*/}
formula=${filename%.*}
desc=$(cat $formulae| regex1 "desc \"(.*)\"")
head=$(cat $formulae| regex1 "head \"(.*)\"")
echo -e "$formula\t$head\t$desc"
done;
}
# Pipe From - print from specific pattern
function pipefrom() {
sed -n '/'"$1"'/,$p'
}
# Convert whiteboard -- https://gist.github.com/lelandbatey/8677901
function whiteboard() {
convert "$1" -morphology Convolve DoG:15,100,0 -negate -normalize -blur 0x1 -channel RBG -level 60%,91%,0.1 "$2"
}
function __git_dirty {
git diff --quiet HEAD &>/dev/null
[ $? == 1 ] && echo "!"
}
#leftpad
function leftpad() {
num=$1
length=$2
printf %0${length}d $num
}
# best chess move by FEN
function chessbestmove() {
curl -s -XPUT 'http://www.chess-hub.com/moves/calculate' -H 'Content-Type: application/x-www-form-urlencoded' --data "fen%5Bfull_string%5D=$*" | grep "The best move is" | cut -d'>' -f 5 | cut -d'<' -f 1
}
# get fen from game
function chessfen() {
gameResponse=$(curl -s $1)
fen=$(curl -s $1 | pup 'div.moveactions input attr{value}')
echo $fen
}
# get chess.com best move -- using ID
function chessmove() {
bestMove=$(chessbestmove `chessfen $1`)
if [[ $@ == *'-s'* ]]; then
echo "$(chessbestmove $fen)"
else
echo "Game URL: $1, FEN: '$fen', Best Move: $(chessbestmove $fen)"
fi
}
# pull latest and print status
function gitpullandstatus() {
(
echo_progress "________________________________________"
cd $1
currentBranch=$(mybranch)
if [ $? -eq 0 ]
then
echo_progress "Updating ${1}... Current branch: ${currentBranch}. Pending changes: $(mybranchpending)"
if [ $# -eq 2 ] && [ "$currentBranch" != "$2" ]; then
git checkout $2
fi
echo_progress "`git fetch`"
echo_progress "`git pull`"
echo_progress "`git status`"
else
echo_progress "Not updating ${1} - cd failed."
fi
)
}
## edit journal
function journal() {
(
cd ~/githubworkspace/bruno-board
git pull
vi README.md
echo "Exit code: $?"
git add README.md
git commit -m "journal updates"
git push
)
}
## get docker ip
function docker-ip() {
docker inspect --format '{{ .NetworkSettings.IPAddress }}' "$@"
}
function chromecookies() {
# The path for MAC!
CHROME="${HOME}/Library/Application Support/Google/Chrome/Default"
#echo $CHROME
COOKIES="$CHROME/Cookies"
#echo ${COOKIES:-Cookies}
QUERY='select host_key, "TRUE", path, "FALSE", expires_utc, name, value from cookies'
if [[ $# == 1 ]]; then
domain=$1
QUERY="$QUERY where host_key like '$domain'"
fi
#echo $QUERY
# This is to make the exported cookies.txt recognizable by some libraries.
# e.g. http.cookiejar.MozillaCookieJar will deny it without this string... -_-//.
# The library does not trust the programmer but searches for this magic string..
echo "# Netscape HTTP Cookie File"
sqlite3 -separator ' ' "${COOKIES:-Cookies}" "$QUERY"
}
function itermtab() {
osascript -e 'tell application "iTerm" to activate' -e 'tell application "System Events" to tell process "iTerm" to keystroke "t" using command down' -e 'tell application "System Events" to tell process "iTerm" to keystroke "'"$*"'"' -e 'tell application "System Events" to tell process "iTerm" to key code 52'
sleep 0.5
}
function itermtabremote() {
osascript -e 'tell application "iTerm" to activate' \
-e 'tell application "System Events" to tell process "iTerm" to keystroke "r" using {command down, control down}' \
-e 'tell application "System Events" to tell process "iTerm" to keystroke "'"$*"'"' \
-e 'tell application "System Events" to tell process "iTerm" to key code 52'
sleep 0.5
}
function itermsplitremote() {
osascript -e 'tell application "iTerm" to activate' \
-e 'tell application "System Events" to tell process "iTerm" to keystroke "d" using {command down}' \
-e 'tell application "System Events" to tell process "iTerm" to keystroke "'"$*"'"' \
-e 'tell application "System Events" to tell process "iTerm" to key code 52'
sleep 0.5
}
function itermtabcmd() {
osascript -e 'tell application "iTerm" to activate' \
-e 'tell application "System Events" to tell process "iTerm" to keystroke "'"$*"'"' \
-e 'tell application "System Events" to tell process "iTerm" to key code 52'
sleep 0.5
}
function top10memory() {