-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbashrc
More file actions
634 lines (529 loc) · 16.7 KB
/
bashrc
File metadata and controls
634 lines (529 loc) · 16.7 KB
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
#!/usr/bin/env bash
# shellcheck disable=SC1090,SC1091
# If not running interactively, don't do anything
test -z "$PS1" && return
# Source global definitions
test -f /etc/bashrc && source /etc/bashrc
# COLORS
# -----------------------------------------------------------------------------
NO_COLOR=$(tput sgr0)
BOLD=$(tput bold)
BLACK=$(tput setaf 0)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
BLUE=$(tput setaf 4)
MAGENTA=$(tput setaf 5)
CYAN=$(tput setaf 6)
WHITE=$(tput setaf 7)
B_RED=$BOLD$RED
B_BLUE=$BOLD$BLUE
B_MAGENTA=$BOLD$MAGENTA
B_CYAN=$BOLD$CYAN
B_WHITE=$BOLD$WHITE
B_GREEN=$BOLD$GREEN
B_YELLOW=$BOLD$YELLOW
B_BLACK=$BOLD$BLACK
ON_BLACK=$(tput setab 0)
ON_RED=$(tput setab 1)
ON_GREEN=$(tput setab 2)
ON_YELLOW=$(tput setab 3)
ON_BLUE=$(tput setab 4)
ON_MAGENTA=$(tput setab 5)
ON_CYAN=$(tput setab 6)
ON_WHITE=$(tput setab 7)
export NO_COLOR BOLD BLACK RED GREEN YELLOW BLUE MAGENTA CYAN WHITE
export B_RED B_BLUE B_MAGENTA B_CYAN B_WHITE B_GREEN B_YELLOW B_BLACK
export ON_BLACK ON_RED ON_GREEN ON_YELLOW ON_BLUE ON_MAGENTA ON_CYAN ON_WHITE
export LS_COLORS='di=0;32'
# -----------------------------------------------------------------------------
# DEFAULTS
# -----------------------------------------------------------------------------
: "${PROMPT_COLOR:=$YELLOW}"
: "${ORG:=}"
: "${SYSTEM:=}"
: "${REPO:=$HOME/git}"
: "${USER_TAG:="($USER)"}"
# -----------------------------------------------------------------------------
# TERMINAL
# -----------------------------------------------------------------------------
set -o vi
export VISUAL=vim
export EDITOR=$VISUAL
export GIT_EDITOR=$VISUAL
# Add bin files to $PATH
_bashrc="${BASH_SOURCE[0]}"
if test -h "$_bashrc"; then
_cli_root=$(dirname "$(readlink "$_bashrc")")
export PATH=$PATH:$_cli_root/bin
export VI_CONFIG="$_cli_root/vi"
fi
export PATH=$PATH:/usr/local/bin:/usr/local/sbin
# cmd history
export HISTCONTROL=erasedups:ignoreboth
export HISTFILESIZE=500000
export HISTSIZE=100000
export HISTIGNORE="&:[ ]*:exit"
shopt -s histappend
shopt -s cmdhist
function reload {
source "$HOME/.bashrc"
}
function title {
echo -ne "\033]2;$1\007"
}
# -----------------------------------------------------------------------------
# PROMPT
# -----------------------------------------------------------------------------
function _gbranch {
local sym
sym=$(git symbolic-ref HEAD 2>/dev/null)
test -z "$sym" && sym=$(git describe --contains --all HEAD 2>/dev/null)
test -z "$sym" && sym=$(git rev-parse --short HEAD 2>/dev/null)
echo "${sym##refs/heads/}"
}
function _gprompt {
local branch w i u
git status >/dev/null 2>&1 || return;
if git log >/dev/null 2>&1; then
branch=$(_gbranch)
i=$(git diff-index --cached --quiet HEAD -- 2>/dev/null || echo "+")
else
branch='(init)'
i=$(git diff --cached --quiet --exit-code || echo "+")
fi
w=$(git diff --no-ext-diff --quiet --exit-code || echo "!")
u="$(test -z "$(git ls-files --others --exclude-standard -- ':/*' \
2>/dev/null)" || echo "?")"
echo "[$branch\[$CYAN\]$w$i$u\[$PROMPT_COLOR\]]"
}
function _prompt {
local depth b e
depth=$(pwd | sed 's/[^/]//g' | sed 's/^\///')
b="$NO_COLOR$PROMPT_COLOR\n"
e="$ON_BLACK$NO_COLOR"
PS1="\[${b}\]$(_gprompt)\[$PROMPT_COLOR\][$HOSTNAME$USER_TAG:$depth\W]:\[$e\] "
PS2=" \[$PROMPT_COLOR\]->\[$NO_COLOR\] "
}
PROMPT_COMMAND="_prompt"
# -----------------------------------------------------------------------------
# ALIASES
# -----------------------------------------------------------------------------
function _alias {
alias "$1" >/dev/null 2>&1
return
}
_alias .. || alias ..='cd ..'
_alias ..2 || alias ..2='cd ../..'
_alias ..3 || alias ..3='cd ../../..'
_alias ..4 || alias ..4='cd ../../../..'
_alias ..5 || alias ..5='cd ../../../../..'
_alias back || alias back='cd -'
_alias cp || alias cp='cp -i'
_alias grep || alias grep='grep --color=auto'
_alias ngrep || alias ngrep='grep --color=auto --exclude-dir node_modules'
_alias ll || alias ll='ls -hl'
_alias lla || alias lla='ls -ahl'
_alias lsd || alias lsd='ls -dl -- */'
_alias mv || alias mv='mv -i'
_alias rm || alias rm='rm -i'
_alias vi || alias vi=vim
_alias blint || alias blint='shellcheck -s bash'
_alias c || alias c='copilot --model claude-opus-4.7 --allow-all-tools --add-dir .'
# -----------------------------------------------------------------------------
# GIT
# -----------------------------------------------------------------------------
# system-provided git completions
if test "$(uname -s)" = Darwin; then
_git_comp="$(brew --prefix 2>/dev/null)/etc/bash_completion.d/git-completion.bash"
test -f "$_git_comp" && source "$_git_comp"
else
for _git_comp in \
/usr/share/bash-completion/completions/git \
/etc/bash_completion.d/git; do
test -f "$_git_comp" && source "$_git_comp" && break
done
fi
_hist="git log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short"
# shellcheck disable=SC2139
_alias hist || alias hist="$_hist"
_alias ga || alias ga='git add'
_alias gc || alias gc='git commit'
_alias gca || alias gca='git commit --amend'
_alias gco || alias gco='git checkout'
_alias gd || alias gd='git diff'
_alias gdr || alias gdr='git diff --cached -M --'
_alias gds || alias gds='git diff --staged'
_alias gpick || alias gpick='git cherry-pick -x'
_alias gpr || alias gpr='git merge --no-ff'
_alias gpt || alias gpt='git push --tags'
_alias gpv || alias gpv='git push --no-verify'
_alias gr || alias gr='git pull --rebase'
_alias grm || alias grm='git rm'
_alias groot || alias groot='cd $(git rev-parse --show-cdup) '
_alias gst || alias gst='git status'
_alias gt || alias gt='git tag -sam'
function grb {
git for-each-ref --sort=-committerdate refs/remotes/ \
--format='%(refname:short)' \
--count=20
}
function gp {
local branch remote
git status >/dev/null 2>&1 || return;
remote=origin
test -z "$1" || remote="$1"
branch=$(git symbolic-ref HEAD 2>/dev/null)
test -z "$branch" && echo "Detached state?" && return 1
echo "pushing to $remote/$branch"
git push "$remote" "$branch"
}
function gu {
local branch
git status >/dev/null 2>&1 || return;
branch=$(git symbolic-ref HEAD 2>/dev/null)
test -z "$branch" && echo "Detached state?" && return 1
echo "pulling from origin/$branch"
git pull origin "$branch"
}
function gshow {
git log --pretty=format:"%h" --grep "$1" | xargs git show
}
# -----------------------------------------------------------------------------
# ORG / SYSTEM / REPO
# -----------------------------------------------------------------------------
##
# org: switch organization context
#
# The org() function provides the switching mechanism. Actual org definitions
# are provided by the private repo via the _org_data() lookup function and
# ORG_LIST variable.
#
# Private repo must define:
# ORG_LIST - space-separated list of org aliases (e.g. "shb acme")
# _org_data - function that echoes "prompt_color:gh_org:aws_prefix" for a
# given alias, or returns 1 if unknown.
#
# Example:
# ORG_LIST="shb acme"
# function _org_data {
# case "$1" in
# shb) echo "B_CYAN:sbaxter:" ;;
# acme) echo "B_YELLOW:AcmeCorp:ACME" ;;
# *) return 1 ;;
# esac
# }
##
function org {
test -n "$1" || { echo "usage: org <name>" >&2; return 1; }
local name="$1"
local def
def=$(_org_data "$name" 2>/dev/null) || {
echo "unknown org: $name" >&2
echo "available: $ORG_LIST" >&2
return 1
}
local color gh_org aws_prefix
IFS=':' read -r color gh_org aws_prefix <<< "$def"
export ORG="$gh_org"
export GH_ORG="$gh_org"
export SYSTEM=""
export USER_TAG="(\[${!color}\]${name}\[${NO_COLOR}${PROMPT_COLOR}\])"
# AWS credential switching (if aws_prefix is set)
if test -n "$aws_prefix"; then
local id_var="${aws_prefix}_AWS_ACCESS_KEY_ID"
local secret_var="${aws_prefix}_AWS_SECRET_ACCESS_KEY"
export AWS_ACCESS_KEY_ID="${!id_var}"
export AWS_SECRET_ACCESS_KEY="${!secret_var}"
export AWS_DEFAULT_REGION=us-east-1
export AWS_REGION=$AWS_DEFAULT_REGION
unset AWS_SESSION_TOKEN
fi
}
# tab completion for org
function _org_complete {
local cur="${COMP_WORDS[COMP_CWORD]}"
# shellcheck disable=SC2207
COMPREPLY=( $(compgen -W "$ORG_LIST" -- "$cur") )
return 0
}
complete -F _org_complete org
##
##
# sys: set or clear the system (repo grouping) context
function sys {
if test -z "$1"; then
export SYSTEM=""
return
fi
local target="$REPO/$GH_ORG/$1"
if test -d "$target"; then
export SYSTEM="$1"
else
echo "system not found: $target" >&2
return 1
fi
}
# tab completion for sys
function _sys_complete {
local cur="${COMP_WORDS[COMP_CWORD]}"
local base="$REPO/$GH_ORG"
test -d "$base" || return 0
# shellcheck disable=SC2207
COMPREPLY=( $(compgen -W "$(find "$base" -mindepth 1 -maxdepth 1 -type d \
-exec basename {} \;)" -- "$cur") )
return 0
}
complete -F _sys_complete sys
##
##
# repo: navigate to a repository
# repo cd to $REPO/$GH_ORG/[$SYSTEM/]
# repo <name> cd to $REPO/$GH_ORG/[$SYSTEM/]<name>
# repo <system>/<name> cd to $REPO/$GH_ORG/<system>/<name> (inline)
function repo {
local target="$REPO/$GH_ORG"
test -n "$SYSTEM" && target="$target/$SYSTEM"
if test -n "$1"; then
if test "${1#*/}" != "$1"; then
# slash detected — inline system/repo
target="$REPO/$GH_ORG/$1"
else
target="$target/$1"
fi
fi
cd "$target" || return
}
# tab completion for repo
function _repo_complete {
local cur="${COMP_WORDS[COMP_CWORD]}"
local base="$REPO/$GH_ORG"
if test -n "$SYSTEM"; then
base="$base/$SYSTEM"
fi
# support inline system/repo completion
if test "${cur#*/}" != "$cur"; then
local sys="${cur%%/*}"
local partial="${cur#*/}"
local sysdir="$REPO/$GH_ORG/$sys"
test -d "$sysdir" || return 0
# shellcheck disable=SC2207
COMPREPLY=( $(compgen -P "$sys/" -W "$(find "$sysdir" -mindepth 1 -maxdepth 1 \
-type d -exec basename {} \;)" -- "$partial") )
else
test -d "$base" || return 0
# shellcheck disable=SC2207
COMPREPLY=( $(compgen -W "$(find "$base" -mindepth 1 -maxdepth 1 -type d \
-exec basename {} \;)" -- "$cur") )
fi
return 0
}
complete -F _repo_complete repo
##
##
# gclone: clone a repo into the org/system directory structure
# gclone <remote> clone as remote name
# gclone <remote> <local-name> clone as local-name
function gclone {
test -n "$1" || { echo "usage: gclone <remote> [local-name]" >&2; return 1; }
test -n "$GH_ORG" || { echo "GH_ORG not set. run org first." >&2; return 1; }
local remote="$1"
local localName="${2:-$remote}"
local dest="$REPO/$GH_ORG"
test -n "$SYSTEM" && dest="$dest/$SYSTEM"
mkdir -p "$dest"
git clone "git@github.com:$GH_ORG/$remote" "$dest/$localName" \
&& cd "$dest/$localName" || return
}
# -----------------------------------------------------------------------------
# GPG
# -----------------------------------------------------------------------------
function encryptfilefor {
gpg --armor --encrypt --recipient "$1" < "$2" > "$2.gpg"
}
function sign {
gpg --armor --detach-sign "$1"
}
# -----------------------------------------------------------------------------
# PROCESS
# -----------------------------------------------------------------------------
function isrunning {
echo "USE ISR OLD MAN!"
}
function wpid {
#shellcheck disable=SC2009
ps -ef | grep -i "$1" | grep -v grep | awk '{print $2}'
}
function listenports {
lsof | grep LISTEN
}
# -----------------------------------------------------------------------------
# TEXT PROCESSING
# -----------------------------------------------------------------------------
function dedup {
sed -i '$!N; /^\(.*\)\n\1$/!P; D' "$1"
}
function upcase {
sed -i 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' "$1"
}
function downcase {
sed -i 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/' "$1"
}
function unDOS {
sed -i 's/.$//' "$1"
}
function noextraspaces {
sed -i 's/^[ \t]*//;s/[ \t]*$//' "$1"
}
function trailingspaces {
sed -i '' -e's/[ \t]*$//' "$1"
}
function trails {
local file="$1"
test -f "$file" || return 1
sed -i '' 's/[[:space:]]*$//' "$file"
}
function trailsall {
for file in $(git ls-files --exclude-standard); do
trails "$file"
done
}
# -----------------------------------------------------------------------------
# NETWORK
# -----------------------------------------------------------------------------
function internalip {
if test "$(uname -s)" = Darwin; then
ipconfig getifaddr en0 2>/dev/null || ipconfig getifaddr en1 2>/dev/null
else
hostname -I 2>/dev/null | awk '{print $1}'
fi
}
function externalip {
curl --silent checkip.amazonaws.com
}
# -----------------------------------------------------------------------------
# URL ENCODE/DECODE
# -----------------------------------------------------------------------------
function urlencode {
local old_lc_collate=$LC_COLLATE
LC_COLLATE=C
local length="${#1}"
for (( i = 0; i < length; i++ )); do
local c="${1:i:1}"
case $c in
[a-zA-Z0-9.~_-]) printf "%c" "$c" ;;
*) printf '%%%02X' "'$c" ;;
esac
done
LC_COLLATE=$old_lc_collate
}
function urldecode {
local url_encoded="${1//+/ }"
printf '%b' "${url_encoded//%/\\x}"
}
# -----------------------------------------------------------------------------
# FILE/DIRECTORY
# -----------------------------------------------------------------------------
function lookat {
vim -R "$1"
}
function follow {
cat "$1" && tail -f -n0 "$1"
}
function linecount {
grep -c $ "$1"
}
function howbigis {
test -z "$1" && echo "no file specified" && return;
test -f "$1" || test -d "$1" || { echo "$1 not found" && return; }
du -sh "$1" | awk '{print $1}'
}
if test "$(uname -s)" = Darwin; then
function pb {
pbcopy < "$1"
}
function pbclear {
pbcopy < /dev/null
}
_alias pc || alias pc="tr -d '\n' | pbcopy"
fi
function l80 {
local pattern=.
local recursive=--recursive
if test -n "$1"; then
pattern="$1"
recursive=
fi
grep --exclude-dir .git --line-number $recursive '.\{80\}' "$pattern"
}
# -----------------------------------------------------------------------------
# FILE COMPRESSION
# -----------------------------------------------------------------------------
if hash gnutar 2>/dev/null; then
_alias tar || alias tar=gnutar
fi
# -----------------------------------------------------------------------------
# MARKDOWN
# -----------------------------------------------------------------------------
function mdfix {
local file="$1"
test -f "$file" || return 1
awk '
function is_list(line) {
return (line ~ /^[[:space:]]*[-*][[:space:]]/ || line ~ /^[[:space:]]*[0-9]+\.[[:space:]]/)
}
function is_blank(line) {
return (line ~ /^[[:space:]]*$/)
}
NR == 1 { prev = $0; next }
{
if (is_list($0) && !is_blank(prev) && !is_list(prev)) {
print prev
print ""
} else {
print prev
}
prev = $0
}
END { print prev }
' "$file" > "${file}.tmp" && mv -f "${file}.tmp" "$file"
}
function fixmd {
for file in $(git ls-files --exclude-standard --modified --others '*.md'); do
mdfix "$file"
trails "$file"
done
}
function mdfixall {
for file in $(git ls-files --exclude-standard '*.md'); do
mdfix "$file"
done
}
# -----------------------------------------------------------------------------
# PACKAGE MANAGER
# -----------------------------------------------------------------------------
if test "$(uname -s)" = Darwin; then
brew=/opt/homebrew/bin/brew
test -x $brew || brew=/usr/local/bin/brew
if test -x $brew; then
eval "$($brew shellenv)"
alias brup="brew upgrade && brew cleanup"
fi
elif type apt-get >/dev/null 2>&1; then
alias brup="sudo apt-get update && sudo apt-get upgrade"
elif type yum >/dev/null 2>&1; then
alias brup="sudo yum update"
elif type pacman >/dev/null 2>&1; then
alias brup="sudo pacman -Syu"
fi
# -----------------------------------------------------------------------------
# JAVA
# -----------------------------------------------------------------------------
test -f /usr/libexec/java_home || true
test -f /usr/libexec/java_home && JAVA_HOME=$(/usr/libexec/java_home 2>/dev/null)
export JAVA_HOME
# -----------------------------------------------------------------------------
# COMPLETIONS
# -----------------------------------------------------------------------------
! type aws_completer >/dev/null 2>&1 || complete -C aws_completer aws
# -----------------------------------------------------------------------------