-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_functions
299 lines (261 loc) · 7.51 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
#!/usr/bin/env bash
### .functions - more complicated aliases, basically
# colors
_RED="$(tty -s && tput setaf 1)"
_YELLOW="$(tty -s && tput setaf 3)"
_WHITE="$(tty -s && tput setaf 7)"
_GRAY="$(tty -s && tput setaf 8)"
_BOLD="$(tty -s && tput bold)"
_RESET="$(tty -s && tput sgr0)"
# list: display all functions in custom dotfiles
function _funclist {
if hash ag 2>/dev/null; then
ag '(function )([A-Za-z0-9_-]+)' "$HOME"/.functions* --nonumbers
else
echo 'Error: Missing ag (silver searcher). Cannot display functions.'
fi
}
# ansicolors: display all fg and bg ansi color escape codes
function ansicolors {
for attr in $(seq 0 1); do
for fg in $(seq 30 37); do
for bg in $(seq 40 47); do
printf "\033[%i;%i;%im%i;$i;%i\033[m " "$attr" "$bg" "$fg" "$attr" "$fg" "$bg"
done
echo
done
done
}
# b64: base64 encode string
function b64 {
echo -n "$1" | openssl base64
}
# dummy: create dummy binary file
function dummy() {
dd if=/dev/urandom bs=1024 count="$1" of=dummy
}
# print out list of generic variables
function foobarlist() {
list=('foo' 'bar' 'baz' 'qux' 'quux' 'quuz' 'corge' 'grault' 'garply' 'waldo' 'fred' 'plugh' 'xyzzy' 'thud')
if [ $# -eq 0 ]; then
for w in "${list[@]}"; do
echo "$w"
done
elif [ $# -ge 1 ]; then
echo "${list[$1]}"
fi
}
# get_rand_port: get random available local port for local webservers
function get_rand_port {
netstat -aln | awk '
$6 == "LISTEN" {
if ($4 ~ "[.:][0-9]+$") {
split($4, a, /[:.]/);
port = a[length(a)];
p[port] = 1
}
}
END {
for (i = 3000; i < 65000 && p[i]; i++){};
if (i == 65000) {exit 1};
print i
}
'
}
# list http ports and procs
function httports {
# shellcheck disable=SC2196
lsof -i -n -P | grep "$HTTP_SERVICES" | grep LISTEN | egrep -o ":[0-9]{2,5}" | cut -f2- -d: | sort -n | uniq
}
function httportss {
# shellcheck disable=SC2196
sudo lsof -i -n -P | grep "$HTTP_SERVICES" | grep LISTEN | egrep -o ":[0-9]{2,5}" | cut -f2- -d: | sort -n | uniq
}
function httprocs() {
lsof -i -n -P | grep 'httpd\|vpnkit\|java\|nc\|node\|ng' | grep LISTEN | awk '{print $1}' | sort -n
}
function httprocss {
sudo lsof -i -n -P | grep 'httpd\|vpnkit\|java\|nc\|node\|ng' | grep LISTEN | awk '{print $1}' | sort -n | uniq
}
# simple http servers for local directory in various flavors
## php
function httphp() {
rport=$(get_rand_port);
if [ $# -eq 1 ]; then
open http://localhost:"$rport" "$1";
else
open http://localhost:"$rport";
fi
php -S 127.0.0.1:"$rport";
}
## python 2.x
function httpy2 {
rport=$(get_rand_port);
open http://localhost:"$rport";
python -m SimpleHTTPServer "$rport";
}
## python 3.x
function httpy3 {
rport=$(get_rand_port);
open http://localhost:"$rport";
python3 -m http.server "$rport";
}
## python 3.x (or 2.x if 3.x not available)
function httpy {
if hash python3 2>/dev/null; then
httpy3
else
httpy2
fi
}
## nodejs (http-server)
function httnode {
rport=$(get_rand_port);
http-server -o -p "$rport";
}
## ruby 1.9.2+
function httruby {
rport=$(get_rand_port);
open http://localhost:"$rport";
ruby -run -e httpd . -p "$rport";
}
## get the Notes field from a Logic Pro X project (e.g. lyrics)
function lpxnotes_dump() {
# Check if a filename is provided as an argument
if [ "$#" -ne 1 ]; then
echo "Usage: lpxnotes_dump <filename>"
else
# assuming only one Alternative
filename=$1/Alternatives/000/ProjectData
# Check if the file exists
if [ ! -e "$filename" ]; then
echo "File not found: $filename"
else
# regex pattern for code where Notes start and end (in LPX 10.7, at least)
pattern='\\\\f0\\\\fs30'
end_pattern='}'
# find first, and only first, instance of this to capture Project Notes
str=$(strings "$filename" | awk -v pattern="$pattern" -v end_pattern="$end_pattern" '
$0 ~ pattern {
flag=1
}
flag {
print
}
$0 ~ end_pattern && flag {
flag=0
exit
}
')
# clean up output
notes=$(
echo "$str" \
| awk '{gsub(/pard.+/,""); print}' \
| awk '{gsub(/\\f0\\fs30/,""); print}' \
| awk '{gsub(/\\cf2 /,""); print}' \
| awk '{gsub(/^ /,""); print}' \
| awk '{gsub(/\\/,""); print}' \
| awk '{gsub(/\}/,""); print}' \
| awk '{gsub(/^[\s]+$/,""); print}' \
| awk '{gsub(/qSxT/,""); print}' \
| awk '{gsub(/\04792/,"\047"); print}' \
)
echo "$notes"
fi
fi
}
# display public IP using cloudflare/google
# myip 4 -> IPv4, myip 6 -> IPv6, myip -> IPv4 and IPv6
function myip() {
if [ $# -eq 0 ]; then
myip4=$(dig +short txt ch whoami.cloudflare @1.0.0.1)
myip6=$(dig -6 TXT +short o-o.myaddr.l.google.com @ns1.google.com)
echo "$myip4" | xargs
echo "$myip6" | xargs
elif [ $# -ge 1 ]; then
if [ "$1" -eq '4' ]; then
myip=$(dig +short txt ch whoami.cloudflare @1.0.0.1)
echo "$myip" | xargs
elif [ "$1" -eq '6' ]; then
myip=$(dig -6 TXT +short o-o.myaddr.l.google.com @ns1.google.com)
echo "$myip" | xargs
else
echo "only IPv4 and IPv6 supported"
fi
fi
}
# speedtest
function speedtest() {
if hash speedtest-cli 2>/dev/null; then
logfile="$HOME"/speedtest_log.txt
echo "Checking internet speed and logging..."
{
date
echo "============================"
hostname
speedtest-cli --simple
echo "----------------------------"
echo ""
} >> "$logfile"
echo "Done and logged to $logfile"
else
echo "speedtest-cli not installed; try brew install speedtest-cli"
fi
}
# mov to gif
function vid2gif() {
RUN_CMD=true
if ! [ -x "$(command -v ffmpeg)" ]; then
echo 'Error: ffmpeg is not installed. Please install with (brew install ffmpeg)' >&2
RUN_CMD=false
fi
if ! [ -x "$(command -v gifsicle)" ]; then
echo 'Error: gifsicle is not installed. Please install with (brew install gifsicle)' >&2
RUN_CMD=false
fi
if [ "$RUN_CMD" = true ]; then
ffmpeg -i "$1" -pix_fmt rgb24 -r 10 -f gif - | gifsicle --optimize=3 --delay=8 > ~/Downloads/"$1".gif
fi
}
alias mp42gif=vid2gif
alias mov2gif=vid2gif
# wav to flac
function wav2flac() {
if ! [ -x "$(command -v ffmpeg)" ]; then
echo 'Error: ffmpeg is not installed. please install with (brew install ffmpeg)' >&2
exit 1
fi
for i in *.wav; do ffmpeg -i "$i" "${i%.*}.flac"; done
}
# wav to mp3
function wav2mp3() {
if ! [ -x "$(command -v ffmpeg)" ]; then
echo 'Error: ffmpeg is not installed. please install with (brew install ffmpeg)' >&2
exit 1
fi
for i in *.wav; do ffmpeg -i "$i" "${i%.*}.mp3"; done
}
# * to *
function mediaconv() {
if ! [ -x "$(command -v ffmpeg)" ]; then
echo 'Error: ffmpeg is not installed. please install with (brew install ffmpeg)' >&2
exit 1
fi
if [ "$#" -lt 2 ]; then
echo "Usage: mediaconv <ext1> <ext2>"
fi
for i in *."$1"; do ffmpeg -i "$i" "${i%.*}.$2"; done
}
function whoruns() {
# shellcheck disable=SC2009
ps -oe user,comm | grep "$1"
}
##############################################################
## load additional functions broken out into own files
# shellcheck disable=SC1091
if [ -f "$HOME"/.functions_about ]; then source "$HOME"/.functions_about; fi
# shellcheck disable=SC1091
if [ -f "$HOME"/.functions_extras ]; then source "$HOME"/.functions_extras; fi
## load private, non-tracked, functions
# shellcheck source=/dev/null
if [ -f "$HOME"/.functions_priv ]; then source "$HOME"/.functions_priv; fi