-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbashrc.sh
More file actions
486 lines (411 loc) · 13.1 KB
/
bashrc.sh
File metadata and controls
486 lines (411 loc) · 13.1 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
#!/bin/bash
# git mail and user
# Fab <fabriph@users.noreply.github.com>
# git filter-branch -f --env-filter "GIT_AUTHOR_NAME='Fab'; GIT_AUTHOR_EMAIL='fabriph@users.noreply.github.com'; GIT_COMMITTER_NAME='Fab'; GIT_COMMITTER_EMAIL='fabriph@users.noreply.github.com';" HEAD
# git push --force --tags origin master
# TO DO list:
# - Improve TAB completion:
# - http://stackoverflow.com/questions/10942919/customize-tab-completion-in-shell
# - http://superuser.com/questions/289539/custom-bash-tab-completion
# - Maybe taking a look at ~/.git-completion.bash helps
# - Compress paths of PS1 if it's too long or too many directories.
# - Save VSCode config
# - Save Cursor config
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
if [ -n "${EPOCHREALTIME-}" ]; then
echo "speedup available"
fph_now() { printf '%s' "${EPOCHREALTIME/./}"; }
fph_runtime_seconds() {
local end_us="$1"
local start_us="$2"
local delta_us=$((10#$end_us - 10#$start_us))
printf '%s' $(( (delta_us + 500000) / 1000000 ))
}
else
echo "speedup NOT available"
fph_now() { date +%s.%N; }
fph_runtime_seconds() { echo "$1 - $2" | bc -l; }
fi
bashrc_start=$(fph_now)
fph_cmd_start=$bashrc_start
# I got this line from AWS sudo bashrc
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
# Constants
ECHO_LIGHT_GREY='\033[0;37m'
ECHO_NO_COLOR='\033[0m'
missing=()
if [ -d "$HOME/bin" ]; then
export PATH=$HOME/bin:$PATH
fi
if [ -d "$HOME/.local/bin" ]; then
export PATH=$HOME/.local/bin:$PATH
fi
if [ -d "/usr/local/sbin" ]; then
export PATH=/usr/local/sbin:$PATH
fi
# Check window size after every command. If necessary, updates the values of LINES and COLUMNS.
shopt -s checkwinsize
# History size
HISTSIZE=10000
HISTFILESIZE=20000
# Ignore and delete duplicate bash history entries.
export HISTCONTROL=ignoredups:erasedups
# Append to the history file, don't overwrite it
shopt -s histappend
# Print timestamp when showing history
export HISTTIMEFORMAT="%F %T "
case "$OSTYPE" in
darwin*) # Mac
export CLICOLOR=1
export BASH_SILENCE_DEPRECATION_WARNING=1 # Disable the ZSH warninig.
;;
linux*)
alias ls="ls --color=auto"
alias open='nautilus . > /dev/null 2>&1 &'
;;
*)
missing+=("LS coloring")
;;
esac
alias grep='grep --color=always'
alias s='screen'
alias ll='ls -l'
alias la='ls -al'
alias l='ls -CF'
alias ..='cd ..'
alias ...='cd ...'
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias lst='python ~/dev/dotfiles/ls_tests.py'
# Grep Recursive
alias gr='grep -RnIf /dev/stdin . <<<'
# File Find: look for files or directories by name (wildcard).
# $1: name/patter(bash).
# $2: optional root path.
ffind () {
if [ "$2" ]; then
find "$2" -name "*$1*"
else
find . -name "*$1*"
fi
}
# Ignore case completition.
bind "set completion-ignore-case on"
# Displays all possibilities with only one TAB press.
bind "set show-all-if-ambiguous on"
# Use Sublime Text as the default editor.
export EDITOR="vim"
if [ -f /etc/bash_completion ]; then
source /etc/bash_completion
elif [ -f /opt/homebrew/etc/profile.d/bash_completion.sh ]; then
source /opt/homebrew/etc/profile.d/bash_completion.sh
else
missing+=("bash-completion")
fi
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# GIT
git_autocommit () {
if [ $# -eq 1 ]; then
git commit -m "$1"
else
git commit -m 'autocommit'
fi
}
git_add_part () {
git add --patch "$1"
}
# Commits all modified files.
git_commit_all () {
files_to_commit=`git status -s | awk '{if ($1 == "M") print $2}' | paste -s -d' ' -`
git add $files_to_commit
if [ $# -eq 1 ]; then
git commit -m "$1"
else
git commit -m "autocommit"
fi
}
alias gl="git log"
alias ga="git add"
alias gb="git --no-pager branch"
alias gc=git_autocommit
alias gd="git --no-pager diff"
alias gs="git status"
alias gst="git --no-pager stash"
alias gca=git_commit_all
#alias gap=git_add_part
#alias gcp=git_add_part
alias gco="git checkout"
# alias gsync="git fetch origin && git rebase origin/master"
if [ -f /usr/share/bash-completion/completions/git ]; then
source /usr/share/bash-completion/completions/git
elif [ -f /Library/Developer/CommandLineTools/usr/share/git-core/git-completion.bash ]; then
source /Library/Developer/CommandLineTools/usr/share/git-core/git-completion.bash
elif [ -f ~/.git-completion.bash ]; then
source ~/.git-completion.bash
else
missing+=("git-completion.bash")
# curl https://raw.github.com/git/git/master/contrib/completion/git-completion.bash -OL
fi
if [ -f ~/.git-prompt.sh ]; then
source ~/.git-prompt.sh
else
missing+=("git-prompt")
# curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh -OL
fi
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Graphite
alias gtms="gt m && gt s"
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Homebrew
brew_completion () {
if [ -f `brew --repository`/Library/Contributions/brew_bash_completion.sh ]; then
source `brew --repository`/Library/Contributions/brew_bash_completion.sh
else
missing+=("brew-bash")
fi
}
vagrant_completion () {
if [ -f `brew --prefix`/etc/bash_completion.d/vagrant ]; then
source `brew --prefix`/etc/bash_completion.d/vagrant
else
missing+=("vagrant")
fi
}
command -v brew >/dev/null 2>&1
if [[ "$?" -eq 0 ]]; then
export PATH=$HOME/homebrew/bin:$PATH
export LD_LIBRARY_PATH=$HOME/homebrew/lib:$LD_LIBRARY_PATH
brew_completion
vagrant_completion
fi
if [ -f /opt/homebrew/bin/brew ]; then
eval "$(/opt/homebrew/bin/brew shellenv)"
else
missing+=("homebrew/bin/brew")
fi
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Pinterest
# if [ -f ~/.bash_pinterest.sh ]; then
# source ~/.bash_pinterest.sh
# else
# missing+=("bash_pinterest")
# fi
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Prompt
# preexec can exist via Homebrew or via ~/.bash-preexec.sh
[ -f $HOMEBREW_PREFIX/etc/profile.d/bash-preexec.sh ] && . $HOMEBREW_PREFIX/etc/profile.d/bash-preexec.sh
function format_runtime() {
local runtime="$1"
local seconds
seconds=$(printf "%.0f" "$runtime")
if [ "$seconds" -lt 60 ]; then
echo "$seconds seconds"
elif [ "$seconds" -lt 3600 ]; then
local minutes=$((seconds / 60))
local remaining_seconds=$((seconds % 60))
if [ "$remaining_seconds" -eq 0 ]; then
echo "$minutes min"
else
echo "$minutes min $remaining_seconds sec"
fi
else
local hours=$((seconds / 3600))
local remaining_minutes=$(((seconds % 3600) / 60))
local remaining_seconds=$((seconds % 60))
local output="$hours hours"
if [ "$remaining_minutes" -gt 0 ]; then
output="$output $remaining_minutes min"
fi
if [ "$remaining_seconds" -gt 0 ]; then
output="$output $remaining_seconds sec"
fi
echo "$output"
fi
}
function preexec_prompt_stats() {
datetime_local=$(date "+%Y-%m-%d %H:%M:%S")
datetime_utc=$(date -u "+%Y-%m-%d %H:%M:%S")
datetime_est=$(TZ=":US/Eastern" date "+%Y-%m-%d %H:%M:%S")
datetime_pst=$(TZ=":US/Pacific" date "+%Y-%m-%d %H:%M:%S")
echo -e "${ECHO_LIGHT_GREY}[${datetime_local} local] [${datetime_utc} UTC] [${datetime_est} EST] [${datetime_pst} PST]${ECHO_NO_COLOR}"
fph_cmd_start=$(fph_now)
}
function precmd_prompt_stats() {
exit_code="$?"
fph_cmd_end=$(fph_now)
runtime=$(fph_runtime_seconds "$fph_cmd_end" "$fph_cmd_start")
precmd_output=""
if [ $exit_code -ne 0 ]; then
precmd_output="${precmd_output}${bold}${red}[\$?:$exit_code]${reset}"
fi
datetime_local=$(date "+%Y-%m-%d %H:%M:%S")
datetime_utc=$(date -u "+%Y-%m-%d %H:%M:%S")
datetime_est=$(TZ=":US/Eastern" date "+%Y-%m-%d %H:%M:%S")
datetime_pst=$(TZ=":US/Pacific" date "+%Y-%m-%d %H:%M:%S")
wall_times="${ECHO_LIGHT_GREY}[${datetime_local} local] [${datetime_utc} UTC] [${datetime_est} EST] [${datetime_pst} PST]"
formatted_runtime=$(format_runtime "$runtime")
precmd_output="${precmd_output}${wall_times}[$formatted_runtime]${ECHO_NO_COLOR}"
echo -e "${precmd_output}"
}
function configure_preexec() {
preexec_functions+=(preexec_prompt_stats)
precmd_functions+=(precmd_prompt_stats)
}
# Try 1 to find preexec.sh
if [ -f ~/.bash-preexec.sh ]; then
source ~/.bash-preexec.sh
configure_preexec
# Try 2 to find preexec.sh
elif [ -f $HOMEBREW_PREFIX/etc/profile.d/bash-preexec.sh ]; then
source /opt/homebrew/etc/profile.d/bash-preexec.sh
configure_preexec
else
missing+=("bash-preexec")
fi
function perforce_client() {
pwd | awk -F '/' '{
n = split($0,a,"/");
if (n < 6) {
exit;
}
if ( a[2] == "google" && a[3] == "src" && a[4] == "cloud" ) {
if ( a[5] == "fabriph" )
print a[6];
else
print a[5],"@",a[6];
}
}'
}
# TODO(fabriph):replace by a front matching, printing the rest of the array,
# until it's long.
function my_ps_dir() {
pwd | awk -F "/" '{
FS = OFS = "/"
sub(ENVIRON["HOME"], "~");
if (length($0) > 24 && NF > 5) {
if ( $4 == "cloud" ) {
if (NF == 6)
exit;
else if (NF == 7)
print "/"$7
else if (NF == 8)
print "/"$7,$8
else if (NF == 9)
print "/"$7,$8,$9
else
print "/"$7,"...",$(NF-1),$NF
} else {
print $1,$2,"...",$(NF-1),$NF
}
} else
print $0
}'
}
grey=$(tput setaf 0) # In fact is black
red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
blue=$(tput setaf 4)
pink=$(tput setaf 5)
cyan=$(tput setaf 6)
white=$(tput setaf 7)
bold=$(tput bold)
reset=$(tput sgr0)
ps1_user=$USER
ps1_user_color="$green"
if [ "$(uname)" == "Darwin" ]; then # Mac
# Switch on serial number
# serial="$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')"
# i=$((${#serial}-3))
# if [ "${serial:$i:3}" == "R53" ]; then # Mac personal
# ps1_user_color="$green"
# elif [ "${serial:$i:3}" == "VDL" ]; then # Mac G
# ps1_user="Mac"
# #ps1_user_color="$cyan"
# else # Unkown Mac
ps1_user="Mac"
ps1_user_color="$green"
# fi
else
# Linux & others
node=`uname --nodename | cut -f2 -d'.'`
if [ "$node" == "nyc" ] || [ "$node" == "c" ]; then
ps1_user="$node"
ps1_user_color="$pink"
elif [ "$node" == "penguin" ]; then
ps1_user="ChromeOS"
ps1_user_color="$green"
else # Uknown Linux
ps1_user="$node"
ps1_user_color="$pink"
fi
# ps1_user_color="$pink"
fi
# Custom PS1:
command -v __git_ps1 >/dev/null 2>&1
if [[ "$?" -eq 0 ]]; then
# Path compression is temporarely disabled (`$(my_ps_dir)` vs `\w`)
# PS1 Git
PS1='\[$ps1_user_color\]$ps1_user\[$reset\]:\[$blue$bold\]\w\[$grey\]$(__git_ps1 " %s")\[$reset\]\$ '
# PS1 Git + Perforce
#PS1='\[$ps1_user_color\]$ps1_user\[$reset\]:\[$cyan\]$(perforce_client)\[$blue$bold\]\w\[$grey\]$(__git_ps1 " %s")\[$reset\]\$ '
else
PS1='\[$ps1_user_color\]$ps1_user\[$reset\]:\[$blue$bold\]\w\[$reset\]\$ '
missing+=("__git_ps1")
fi
# Try colors:
#PS1='\[$grey\]grey\[$red\]red\[$green\]green\[$yellow\]yellow\[$blue\]blue\[$pink\]pink\[$cyan\]cyan\[$white\]white\[$bold\]\[$grey\]grey\[$red\]red\[$green\]green\[$yellow\]yellow\[$blue\]blue\[$pink\]pink\[$cyan\]cyan\[$white\]white\[$reset\]'
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# NVM
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# pyenv
# export PYENV_ROOT="$HOME/.pyenv"
# command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
# eval "$(pyenv init -)"
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# bazel
# https://github.com/bazelbuild/buildtools
BUILD_TOOLS_BAZEL_BIN="/Users/fabricioph/code/buildtools/bazel-bin"
export PATH=$BUILD_TOOLS_BAZEL_BIN/buildozer/buildozer_:$PATH
export PATH=$BUILD_TOOLS_BAZEL_BIN/buildifier/buildifier_:$PATH
export PATH=$BUILD_TOOLS_BAZEL_BIN/unused_deps/unused_deps_:$PATH
lint () {
lint_start=`date +%s.%N`
bazel lint --no-polite
lint_end=`date +%s.%N`
lint_runtime=$(fph_runtime_seconds "$lint_end" "$lint_start")
echo "lint took $lint_runtime seconds"
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# CODEX
codex2() {
command codex __CODEX_STARTUP_HANDSHAKE__ --add-dir /Users/fabricio/dev/dotfiles-private/ai-tools/runs "$@"
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Notion
# eval "$(rbenv init -)"
# eval "$(direnv hook bash)"
# eval "$(notion completion --install)"
# export PATH="/usr/local/opt/libpq/bin:$PATH"
# PATH="/opt/homebrew/opt/libpq/bin:$PATH"
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Show missing files
if [ ! ${#missing[@]} -eq 0 ]; then
output=$(printf ", %s" "${missing[@]}")
output=${output:1}
echo "bashrc.sh: missing ${output[*]}"
else
echo "bashrc.sh: nothing missing"
fi
bashrc_end=`date +%s.%N`
runtime=$(fph_runtime_seconds "$bashrc_end" "$bashrc_start")
echo "bashrc.sh: loaded in $runtime seconds"