-
Notifications
You must be signed in to change notification settings - Fork 0
/
.bash_profile_helpers
650 lines (591 loc) · 13.8 KB
/
.bash_profile_helpers
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
#!/bin/bash
alias bfg="java -jar ~/bfg.jar" # clears stuff from Git history, https://rtyley.github.io/bfg-repo-cleaner/
alias chrome="open -a 'Google Chrome.app'"
alias dnsrefresh="sudo killall -HUP mDNSResponder"
alias hs="http-server"
alias jad="jade -Pw"
alias jek="jekyll serve -w"
alias json="jq"
alias kiosk="/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --kiosk --incognito --no-context-menu"
alias lando="~/lando-cli/bin/lando.js"
alias math="bc -l"
alias ports='lsof -i -P | grep -i "listen"'
alias reinstall='rm -rf package-lock.json node_modules && npm i'
alias reload='exec -l $SHELL'
alias sas="sass --style expanded --watch --sourcemap=none"
alias search="sudo grep -nrilS"
export GIT_REDIRECT_STDERR='2>&1'
export ORIGIN='LOCAL'
desk="$HOME/Desktop"
games='/usr/share/emacs/22.1/lisp/play/'
git_format="%C(yellow)%h%Creset %C(auto)%d%Creset %Cblue%ar%Creset %Cred%an%Creset %n%w(72,1,2)%s"
TAB=`echo -e "\t"`
useragent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36"
vscode="$(which code)"
w="$HOME/Programming/web"
if [[ $OSTYPE == "msys" ]]; then
function pbcopy() {
printf "$(</dev/stdin)" | cat > /dev/clipboard
}
alias fregp="grep"
alias open="start"
export w="$HOME/source/repos"
lx_user=$(echo "$(whoami)" | tr '[:upper:]' '[:lower:]')
export lx="//wsl\$/Ubuntu/home/$lx_user"
elif [[ $OSTYPE == "linux-gnu" ]]; then
export w="/mnt/c/Users/$(whoami)/source/repos"
fi
# If google-chrome command doesn't exist, set it
if ! command -v google-chrome &> /dev/null; then
# Opens specified website in Chrome
alias google-chrome="/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
fi
function b64() {
# Base 64 encode/decode
# Encoce: b64 "Hello world"
# Decode: b64 -d SGVsbG8gd29ybGQ=
if [[ -n $2 ]]; then
local flags=$1
shift
fi
local string=$1
c="echo -n \$(echo -n "$string" | base64 $flags) | pbcopy"
echo ">>> $c"
bash -c "$c"
echo $(pbpaste)
}
function cbrew() {
# Change brew permissions on corporate machine
sudo chown -R $(whoami) /usr/local/bin /usr/local/etc /usr/local/sbin /usr/local/share /usr/local/share/doc
chmod u+w /usr/local/bin /usr/local/etc /usr/local/sbin /usr/local/share /usr/local/share/doc
}
function code() {
command "$vscode" --user-data-dir ~/.vscode $@
}
function f() {
set -o noglob
local dowhat=$1
local substring=$(echo $2 | sed -E 's/\"/\\\"/g')
local include=$3
local is_git="$(is_git)"
local exclude_list=(node_modules jspm_packages .git tmp .venv logs staticfiles)
if [[ $is_git == "true" ]]; then
local git_root=$(git rev-parse --show-toplevel)
local gitignore="$git_root/$(git config --local --get --default .gitignore core.excludesFile)"
if [[ -f $gitignore ]]; then
gitignore=$(cat $gitignore | sed -E 's/[#\!].*//g' | sed 's/\B\///g')
exclude_list=("${exclude_list[@]}" $gitignore)
fi
fi
local exclude=$(wrap "':(exclude)%' " "${exclude_list[@]}")
local find_path=$include
if [[ -z $find_path ]]; then
local find_path="./*"
fi
local flags=""
case "$dowhat" in
# Look for substring in files, return file names
# e.g. `f tx "foo bar" ./some_directory`
tx) flags="-iIrl";;
# Look for substring in files, return matching lines
# e.g. `f ln "foo bar" ./some_directory`
ln) flags="-iIr";;
# Look for regex in files, return file names
# e.g. `f rx "foo b[ar]{2}" ./some_directory`
rx) flags="-iIrl -E";;
*)
echo "'$dowhat' isn't a thing. Did you want to use fd?"
return
esac
local c="git grep $flags --no-index --recurse-submodules \"$substring\" -- $find_path $exclude"
echo ">>> $c"
bash -c "$c"
set +o noglob
}
function figall(){
filename="figlet.txt"
figlist=$(figlist | tail -n +4)
nl=$'\n'
output=""
while read -r font; do
if [[ "$font" == "Figlet control files in this directory:"* ]]; then
break
fi
output="$output### $font${nl}"
output="$output$(figlet -f $font $1)${nl}${nl}"
done <<< "$figlist"
echo "$output" > $filename
}
function is_git() {
echo $((git rev-parse --is-inside-work-tree) 2>&1)
}
function git_branch(){
local git_status="$(git status 2> /dev/null)"
local on_branch="On branch ([^${IFS}]*)"
local on_commit="HEAD detached at ([^${IFS}]*)"
local has_unstaged="Changes not staged for commit"
local has_staged="Changes to be committed"
local unstaged_char=""
local staged_char=""
if [[ $git_status =~ $has_unstaged ]]; then
unstaged_char=" *"
fi
if [[ $git_status =~ $has_staged ]]; then
staged_char=" +"
fi
if [[ $git_status =~ $on_branch ]] || [[ $git_status =~ $on_commit ]]; then
echo " (${BASH_REMATCH[1]}$unstaged_char$staged_char)"
fi
}
function gh_url(){
local gh_remote=${1:-origin}
local gh_url=$(git remote get-url $gh_remote | sed -E 's/^.*git@//' | sed -E 's/\:[0-9]+//' | sed -E 's/github\.com\:/github.com\//' | sed -E 's/\.git$//')
local gh_url="https://$gh_url"
echo $gh_url
}
function g(){
local abbv=$1
shift
local author=$(git config user.name)
local git_dir=$(git rev-parse --git-dir 2> /dev/null)
local current_branch=''
if [[ $git_dir ]]; then
local current_branch=$(git rev-parse --abbrev-ref HEAD)
fi
local c=()
case "$abbv" in
a)
c=(add);;
b)
c=(branch);;
bDD) # Delete all local branches except the current one
git branch | grep -v \* | xargs git branch -D
c=(branch)
;;
c)
c=(checkout);;
ca) # Crappy WIP GUI for selecting branches
local branch
local branches=$(git branch -a --format "%(refname:lstrip=2)")
select_from "$branches" branch
c=(checkout $branch)
;;
cp)
c=(cherry-pick);;
cpb) # Cherry pick all commits on branch from point where the branch and HEAD diverged
local branch=$1
local sha=$(git merge-base HEAD $branch)
c=(cherry-pick $sha..$branch)
shift
;;
d)
c=(diff)
OPTIND=1
local opt
while getopts "gsw" flag; do
opt=
case "${flag}" in
g) opt=--staged;;
s) opt=--stat;;
w) opt=--ignore-all-space;;
esac
if [[ ! -z $opt ]]; then
shift $(($OPTIND - 1))
c+=($opt)
fi
done
;;
# db) # What changed between where this branch and other branch diverged
# if [[ $1 == "-s" ]]; then
# local flag="--stat"
# shift
# fi
# local target_branch=$1
# shift
# local base_commit=$(git merge-base --all HEAD $target_branch)
# c=(diff $flag $base_commit..$target_branch)
# ;;
dc) # What changed in the last n commits
if [[ $1 == "-s" ]]; then
local flag="--stat"
shift
fi
if [[ -z $1 ]]; then
local commit=HEAD
else
local commit=$1
fi
shift
if [[ -z $1 ]]; then
local num_commits=1
else
local num_commits=$1
shift
fi
c=(diff $flag $commit~$num_commits..$commit)
;;
dcs)
g dc -s $@
return
;;
dnew) # Diff from my last commit
local author=$(git config user.name)
local hash=$(git log --author=$author -1 --pretty=%h)
c=(diff $hash..HEAD)
;;
f)
local remote=$1
if [[ -z $remote ]]; then
remote="origin"
else
shift
fi
c=(fetch --prune $remote);;
fa)
c=(fetch --prune --all);;
fn)
git rev-list --all | xargs git grep -i "$@"
return
;;
h)
chrome $(gh_url $@)
return
;;
l)
git log --graph --pretty=format:"$git_format" $@
return
;;
lme)
g l --author=$author
return
;;
lame)
g l --author=$author --all
;;
lf)
g l --follow $@
return
;;
la)
g l --all $@
return
;;
laf)
g l --all --follow $2
return
;;
lc)
c=(diff --name-only --diff-filter=U);; # List conflicted files
m)
c=(commit);;
mm)
c=(commit -m);;
MM)
c=(commit --no-verify -m);;
mme)
c=(commit -m --allow-empty);;
ma)
c=(commit --amend);;
mA)
c=(commit --amend -m);;
mC)
c=(commit -C ORIG_HEAD);;
p)
c=(push);;
pD)
local branch=$1
shift
if [ -z $branch ]; then
local branch=$current_branch
fi
c=(push --delete origin $branch)
;;
pDD) # Delete all remote branches that don't match a local branch
local remote="${1:-origin}"
local branches_local=$(git branch | sed -E 's/\*//g')
local branch_local
local branches_remote=$(git branch -r | sed -E "s/$remote\///g" | sed -E "s/.* -> .*//g")
local branch_remote
local branches=()
local branch
for branch_remote in $branches_remote; do
if ! in_list "$branches_local" "$branch_remote"; then
branches+=($branch_remote)
fi
done
if [[ -z "$branches" ]]; then
echo "No branches to delete"
return
fi
local branch_str=$(join_by " " "${branches[@]}")
c=(push --delete $remote $branch_str)
;;
pu)
local remote=$1
shift
if [[ -z $remote ]]; then
remote=origin
fi
c=(push -u $remote $current_branch)
;;
puF)
local remote=$1
shift
if [ -z $remote ]; then
remote=origin
fi
c=(push -u -f $remote $current_branch)
;;
pl)
c=(pull --set-upstream origin $current_branch);;
pr)
chrome "$(gh_url)/pull/new/$current_branch"
return
;;
r)
c=(reset);;
rc)
local num_commits=$(git rev-list --count HEAD)
local message="$(git log -1 --pretty=%B)"
if [[ $num_commits == 1 ]]; then
git update-ref -d HEAD
else
git reset --soft HEAD~
fi
git add .
c=(commit -C ORIG_HEAD)
;;
rH)
c=(reset --hard "origin/$current_branch");;
rl)
git reflog --pretty=format:"$git_format"
;;
s)
c=(status);;
st)
if [[ -z $1 ]]; then
c=(stash)
else
c=(stash push $1)
fi
shift
;;
sta)
c=(stash apply);;
stc)
c=(stash clear);;
sh)
c=(show $1:./$2)
shift
shift
;;
u) # undo
git reset --soft head~$1
shift
c=(status)
;;
U)
git reset --soft head~$1
shift
git reset head .
c=(status)
;;
UH)
c=(reset --hard head~$1)
shift
;;
x)
c=(clean -fd);;
X) # Clean everything including ignored files
c=(clean -xfd);;
++) # Go to next commit
local target_branch=$1
shift
if [[ -z $target_branch ]]; then
echo "Specify a branch"
return
fi
local current_sha=$(git rev-parse HEAD)
local target_sha=$(git log $target_branch --reverse --pretty=%H | grep -A 1 $current_sha | tail -n1)
git checkout $target_sha
return
;;
--) # Go to previous commit
c=(checkout HEAD~);;
esac
if [[ -z $c ]]; then
c=$abbv
fi
echo ">>> git ${c[@]} $@"
git "${c[@]}" "$@"
}
function happ(){
heroku apps:destroy $1 --confirm $1
}
function hk() {
local app=$1
shift
local cmd=$1
shift
cmd="heroku $cmd --app $app $@"
echo $cmd
bash -c "$cmd"
}
function in_list() {
local list=$1
local target=$2
local item
for item in $list; do
if [[ "$item" == "$target" ]]; then
return 0
fi
done
return 1
}
function join_by() { # https://stackoverflow.com/a/17841619/2053389
local d=${1-} f=${2-}
if shift 2; then
printf %s "$f" "${@/#/$d}"
fi
}
function lipsum() {
local text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
echo -n $text | pbcopy
echo "Copied!"
}
function l(){
echo ""
echo "----------"
ls -AF1 $1
echo "----------"
echo ""
}
function ngg() {
local APP_ROUTE="src/app/" # TODO: Calculate path
local HAS_NPX_NG="[ -e node_modules/.bin/ng ]"
if ! eval "$HAS_NPX_NG"; then
echo "Not ng project root"
return 1
fi
local abbv=$1
shift
local path=$1
shift
local path_trim=${path/$APP_ROUTE/}
local c=()
case "$abbv" in
mc)
npx ng generate module $path_trim && npx ng generate component $path_trim --skip-tests --style=scss $@
;;
*)
echo "No :("
return 1
;;
esac
local HAS_NPX_PRETTIER="[ -e node_modules/.bin/prettier ]"
if eval "$HAS_NPX_PRETTIER"; then
echo "prettier"
npx prettier -w $path
fi
}
function pingt() {
local dest=$1
local pong
ping -t $dest | while read pong; do echo "$(date): $pong"; done
}
function sc() {
local arg=$1
shift
case "$arg" in
all)
byobu ls -F "#{session_name}"
;;
kill)
local sessions=($(sc all))
local sessions_length=${#sessions[@]}
if [[ $sessions_length == 0 ]]; then
return 1
fi
select_from "$(sc all)" session_name
byobu kill-session -t $session_name
;;
up)
local sessions=($(sc all))
local sessions_length=${#sessions[@]}
case $sessions_length in
0)
echo "Session name?"
read session_name
byobu new -s $session_name
;;
1)
local session_name=${sessions[0]}
byobu attach -t $session_name
;;
*)
select_from "$(sc all)" session_name
byobu attach -t $session_name
;;
esac
;;
*)
byobu $arg $@
;;
esac
}
function select_from(){
local options=($1)
local varname=$2
local index
local selection
for index in "${!options[@]}"; do
echo -e "$index\t${options[index]}"
done
read -n 2 index
if [[ $index == "q" ]]; then
return
fi
selection="${options[$index]}"
eval $varname="$selection"
}
function sizeof(){
if [[ $1 == "." ]] || [[ -z "$1" ]]; then
du -hc -d 0 ./.[!.]* ./* 2>/dev/null
elif [[ $1 == ".." ]]; then
du -hc -d 0 ../.[!.]* ../* 2>/dev/null
elif [[ -d $1 ]]; then
dir=${1%/}
du -hc -d 0 $dir/.[!.]* $dir/* 2>/dev/null
else
du -hc -d 0 $1 2>/dev/null
fi
}
function sizeofs(){
sizeof $1 | sort -h
}
function ssfmt() {
echo "Changing screenshot format to '$1'..."
defaults write com.apple.screencapture type $1
killall SystemUIServer
}
function targz(){
local dir=$(dirname "$1")
local base=$(basename "$1")
local fp="$base.tar.gz"
rm -rf $fp
tar -cvzf $fp $1
mv $fp $dir
}
function venv() {
python -m venv .venv
source .venv/bin/activate
}
function wrap() { # For each item, replace '%' with string
local wrapper=$1
shift
local output=""
for item in "${@}"; do
output="$output${wrapper//%/$item}"
done
echo $output
}