forked from colonelpanic8/dotfiles_old
-
Notifications
You must be signed in to change notification settings - Fork 3
/
.functions
381 lines (318 loc) · 9.17 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
# vim:ft=sh
ios_runtime() {
xcrun simctl list -j runtimes ios | jq -r '.[] | sort_by(.identifier)[0].identifier'
}
ios_devices() {
xcrun simctl list -j devices ios | jq ".devices.\"$(ios_runtime)\"" | jq 'sort_by(.deviceTypeIdentifier)'
}
ios_devices_booted() {
ios_devices | jq '[.[] | select(.state == "Booted")]'
}
ios_devices_shutdown() {
ios_devices | jq '[.[] | select(.state == "Shutdown")]'
}
ios_devices_get_id() {
booted=$(ios_devices_booted)
if [ "$booted" != "[]" ]; then
echo $booted | jq -r '.[0].udid'
else
device_id=$(ios_devices_shutdown | jq -r '.[0].udid')
xcrun simctl boot $device_id
echo $device_id
fi
}
journal_pass() {
pass edit Journal/$(date +%Y/%m/%d)/entry.txt
}
fix_bluetooth() {
pids="$( ps -A | grep -iE '(blued|coreaudiod|bluetoothaudiod)' | grep -v grep | cut -c 1-90 | cut -c 1-6 )"
echo sudo renice -10 $pids
sudo renice -10 $pids
}
update-rust-analyzer() {
pushd .
cd ~/Downloads/
gh release -R rust-lang/rust-analyzer download -p '*-aarch64-apple-darwin.gz'
gunzip rust-analyzer-aarch64-apple-darwin.gz
chmod +x rust-analyzer-aarch64-apple-darwin
./rust-analyzer-aarch64-apple-darwin --version
mv ./rust-analyzer-aarch64-apple-darwin ~/.cargo/bin/rust-analyzer
popd
}
random_words() {
WORDFILE="/usr/share/dict/words"
NUMWORDS=$1
#Number of lines in $WORDFILE
tL=`awk 'NF!=0 {++c} END {print c}' $WORDFILE`
for i in `seq $NUMWORDS`
do
rnum=$((RANDOM%$tL+1))
echo -n $(sed -n "$rnum p" $WORDFILE)' '
done
}
ssh_tar() {
tar -pcz $1 | ssh $2 'tar -xz'
}
ssh_untar() {
ssh $1 "tar -pcz $2" | tar -pxz
}
youtube_play() {
local_video_file=/tmp/video.flv
fifo_loc=/tmp/fifo
rm $local_video_file
if [[ ! -p $fifo_loc ]]; then
mkfifo $fifo_loc
fi
youtube-dl --no-part --max-quality 5 -o $local_video_file $1 > $fifo_loc & disown
counter=0
while ! grep '\[download\]' $fifo_loc; do
sleep 1
counter=$(($counter + 1))
if [ $counter -ge 10 ]; then
exit 1
fi
done
mplayer -vo null $local_video_file
rm $fifo_loc
}
histogram() {
if [ $# -lt 1 ]
then
echo "Usage: histogram [field] [precision]"
return 0
fi
field=1
if [ $# -gt 1 ]
then
field=$2
fi
if [ $# -gt 2 ]
then
precision="."
for i in `seq 1 1 $3`
do
precision=$precision.
done
cat $1 | gsort -k $field -n -t ',' | cut -d ',' -f $field | gsed -e "s/\($precision\).*/\1/" | uniq -c
else
cat $1 | sort -k $field -n -t ',' | cut -d ',' -f $field | uniq -c
fi
}
cdn() {
num_up=$(echo $1 | grep -o "\." | wc -l)
updot="./"
for ((i=0;i<$((${num_up}-1));i++))
do
updot="$updot../"
done
cd $updot
}
get_cols () {
# Usage: get_cols [-F$separator] [file_name] [column_num]
# separator
separator=' '
column_list=""
for column_num in $@; do
[ -r $column_num ] && continue
if echo $column_num | grep '\-F' ; then
separator=$(echo $column_num | sed 's/-F//g' )
continue
fi
[ $column_num -lt '0' ] && column_num="(NF + 1 $column_num )" #Negative indices like python's array[-1]
[ -z $column_list ] && column_num="\$$column_num" || column_num=",\$$column_num" # Place commas appropriately.
column_list="$column_list$column_num"
done
unset column_num
# Is the first parameter a file?
awk_string="BEGIN { FS = \"$separator\" } ; {print $column_list}"
if [ -r $1 ]; then
awk $awk_string $1
else
awk "$awk_string"
fi
unset column_list
unset awk_string
}
aggregate () {
awk "{sum += \$1} END { printf \"%d\n\", sum }"
}
git_modified () {
git status | grep modified | get_cols -1
}
get_col () {
for item in `awk "{print \\$$@}"`; do
printf $item
printf '\n'
done
}
get_line() {
if [ -t 0 ]; then
head -n $1 | tail -n 1
else
head -n $1 $2 | tail -n 1
fi
}
du_sorted () {
du "$@" | sort -n | get_col 2 | xargs du -hs
}
function git_checkout_last() {
git checkout @{-1}
}
function average () {
if [ '0' -eq $# ]
then
awk '{print $1}' | awk '{s+=$1} END{print "Sum: "s, "\nNumber of lines : "NR, "\nAverage: "s/(NR)}'
else
awk '{print $1}' | awk '{s+=$1} END{print "Sum: "s, "\nNumber of lines : "NR, "\nAverage: "s/(NR)}' $1
fi
}
function get_shas_that_touched() {
git log --oneline --reverse "$@" | get_col 1
}
function echo_hash_if_exp_found_in_filename_diff() {
[ ! -z "$(git diff $1~1 $1 -- $2 | cat | grep $3)" ] && echo $1
}
function get_shas_that_touched_with_grep() {
get_shas_that_touched $1 | xargs -I the_hash sh -c 'echo_hash_if_exp_found_in_filename_diff the_hash $1 $2'
}
function show_interdiffs_matching_grep() {
get_shas_that_touched_with_grep $1 $2 | xargs -I hash sh -c 'git diff hash~1 hash -- $1'
}
function find_all_ssh_agent_sockets() {
find /tmp -type s -name agent.\* 2> /dev/null | grep '/tmp/ssh-.*/agent.*'
}
function set_ssh_agent_socket() {
export SSH_AUTH_SOCK=$(find_all_ssh_agent_sockets | tail -n 1 | awk -F: '{print $1}')
}
# Create a new directory and enter it
function mkd() {
mkdir -p "$@" && cd "$@"
}
# 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
}
# 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')"
}
# Start an HTTP server from a directory, optionally specifying the port
function server() {
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)
python3 -m http.server "$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=$(ipconfig getifaddr en1)
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"
}
# Test if HTTP compression (RFC 2616 + SDCH) is enabled for a given URL.
# Send a fake UA string for sites that sniff it instead of using the Accept-Encoding header. (Looking at you, ajax.googleapis.com!)
function httpcompression() {
encoding="$(curl -LIs -H 'User-Agent: Mozilla/5 Gecko' -H 'Accept-Encoding: gzip,deflate,compress,sdch' "$1" | grep '^Content-Encoding:')" && echo "$1 is encoded using ${encoding#* }" || echo "$1 is not using any encoding"
}
# Syntax-highlight JSON strings or files
# Usage: `json '{"foo":42}'` or `echo '{"foo":42}' | json`
function json() {
if [ -t 0 ]; then # argument
python -mjson.tool <<< "$*" | pygmentize -l javascript
else # pipe
python -mjson.tool | pygmentize -l javascript
fi
}
# All the dig info
function digga() {
dig +nocmd "$1" any +multiline +noall +answer
}
# Escape UTF-8 characters into their 3-byte format
function escape() {
printf "\\\x%s" $(printf "$@" | xxd -p -c1 -u)
echo # newline
}
# Decode \x{ABCD}-style Unicode escape sequences
function unidecode() {
perl -e "binmode(STDOUT, ':utf8'); print \"$@\""
echo # newline
}
# Get a character’s Unicode code point
function codepoint() {
perl -e "use utf8; print sprintf('U+%04X', ord(\"$@\"))"
echo # newline
}
# Add note to Notes.app (OS X 10.8)
# Usage: `note 'foo'` or `echo 'foo' | note`
function note() {
local text
if [ -t 0 ]; then # argument
text="$1"
else # pipe
text=$(cat)
fi
body=$(echo "$text" | sed -E 's|$|<br>|g')
osascript >/dev/null <<EOF
tell application "Notes"
tell account "iCloud"
tell folder "Notes"
make new note with properties {name:"$text", body:"$body"}
end tell
end tell
end tell
EOF
}
# 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
}
# Manually remove a downloaded app or file from the quarantine
function unquarantine() {
for attribute in com.apple.metadata:kMDItemDownloadedDate com.apple.metadata:kMDItemWhereFroms com.apple.quarantine; do
xattr -r -d "$attribute" "$@"
done
}