-
Notifications
You must be signed in to change notification settings - Fork 1
/
.crcrc
285 lines (241 loc) · 8.58 KB
/
.crcrc
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
#!/bin/sh
###################################################
### Functions
###################################################
# append_path() and prepend_path() adapted from Fink's init.sh
# add to end of path
append_path() {
if eval test -z "\$$1"; then
eval "export $1=$2"
fi
if ! eval test -z "\"\${$1##*:$2:*}\"" -o -z "\"\${$1%%*:$2}\"" -o -z "\"\${$1##$2:*}\"" -o -z "\"\${$1##$2}\""; then
eval "export $1=\$$1:$2"
fi
}
# add to beginning of path
prepend_path() {
if eval test -z "\$$1"; then
eval "export $1=$2"
return
fi
if ! eval test -z "\"\${$1##*:$2:*}\"" -o -z "\"\${$1%%*:$2}\"" -o -z "\"\${$1##$2:*}\"" -o -z "\"\${$1##$2}\""; then
eval "export $1=$2:\$$1"
fi
}
prepend_path_if_exists() {
if [ -d "$2" ]; then
prepend_path $1 $2
fi
}
###################################################
### PATH-type list variables
###################################################
prepend_path_if_exists PATH $HOME/usr/bin
prepend_path_if_exists PATH $HOME/bin
###################################################
### Simple variables
###################################################
export EDITOR=vim
export VISUAL=vim
# XXX whatever my default visual editor happens to be, could be atom or subl too
# alias vi=${VISUAL}
# export LC_COLLATE="POSIX"
# export DOCKER_NAMESPACE=crccheck # my docker username
# Rancher Desktop
prepend_path_if_exists PATH $HOME/.rd/bin
# A quick way for me to change my terminal titles
function tit {
local title=$(basename $PWD)
echo -e "\033];$title\007"
}
# Open a Wireshark window to a Docker container
# Usage: dshark <container>
# function dshark {
# docker run --rm --net=container:$1 crccheck/tcpdump -i any tcp --immediate-mode -w - | wireshark -k -i -
# }
[ -f ~/.bash_aliases ] && source ~/.bash_aliases
if [ -n "ZSH_VERSION" ]; then
alias $=''
fi
[ -f ~/.work_aliases ] && source ~/.work_aliases
# Inspired by https://theptrk.com/2018/07/11/did-txt-file/
did () {
mkdir -p ~/Documents/did
if [ -n "$*" ]; then
# TODO figure out how to escape $*
touch ~/Documents/did/"$*"
fi
find ~/Documents/did -type f -printf "%TY-%Tm-%Td %TH:%TM\t%P\n" | sort -nr | head -n 10
}
if [ -d ~/.nvm ]; then
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
if [ -n "$BASH_VERSION" ]; then
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
fi
else
alias nvm='[ -f .nvmrc ] && n $(cat .nvmrc) || echo "MISSING: .nvmrc"'
fi
###################################################
### Platform- or system-specific modifications
###################################################
if [ "$(uname -s)" = "Darwin" ]; then
# Mac OS X stuff:
alias date=gdate
alias du='du -kh'
alias ls='ls -AFGq'
alias ,lsof="lsof -i -sTCP:LISTEN -P -n"
prepend_path_if_exists PATH /usr/local/bin
prepend_path_if_exists PATH /opt/homebrew/bin
prepend_path_if_exists PATH $(brew --prefix)/opt/curl/bin
prepend_path_if_exists PATH $(brew --prefix)/opt/findutils/libexec/gnubin
prepend_path_if_exists PATH $(brew --prefix)/opt/gnu-sed/libexec/gnubin
prepend_path_if_exists PATH $(brew --prefix)/opt/mysql@5.7/bin
prepend_path_if_exists PATH $(brew --prefix)/opt/openssl@1.1/bin
# http://postgresapp.com/documentation/cli-tools.html
prepend_path_if_exists PATH '/Applications/Postgres.app/Contents/Versions/latest/bin'
# Fix Backspace/Delete confusion:
# stty erase ^?
# Tell `n` not to use /usr/local
export N_PREFIX=~/.local
[ -d /opt/homebrew ] && eval "$(/opt/homebrew/bin/brew shellenv)"
else
# Fix aliases that don't work in OSX
alias df='df -kTh'
alias du='du -kh --max-depth=1'
alias ls='ls -F --color=auto'
alias pbcopy='xsel --clipboard --input'
alias pbpaste='xsel --clipboard --output'
# SSH Agent and keychain
if [ -f /usr/bin/keychain ] && [ ! -f /tmp/keychain ]; then
ssh-add ~/.ssh/*.pem
# /usr/bin/keychain $HOME/.ssh/*.pem
# source $HOME/.keychain/$HOSTNAME-sh
touch /tmp/keychain
fi
fi
# HACK to run stuff only in M1 mac with zsh
if [ -d /opt/homebrew ]; then
bindkey -e
fi
# After each command, save and reload history
# nevermind, this is too confusing
# export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"
export HISTCONTROL=ignoredups:erasedups
###################################################
### PYTHON
###################################################
export PIP_DISABLE_PIP_VERSION_CHECK=1
if [ -d ~/.pyenv ]; then
export PYENV_ROOT="$HOME/.pyenv"
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
if which pyenv-virtualenv-init > /dev/null; then eval "$(pyenv virtualenv-init -)"; fi
export PYENV_VIRTUALENV_DISABLE_PROMPT=1
export VIRTUAL_ENV_DISABLE_PROMPT=1
export POETRY_VIRTUALENVS_IN_PROJECT=true
function ,work {
if [ -f poetry.lock ]; then
echo "$(poetry env info --path)/bin/activate"
source $(poetry env info --path)/bin/activate
elif [ -f .venv/bin/activate ]; then
# Create a virtualenv with: python3 -m venv .venv
# The virtualenv PS1 will look like (.venv) but that just means it's local
echo source .venv/bin/activate
source .venv/bin/activate
else
local env_name=$(basename $PWD)
pyenv activate ${env_name}
if [ $? -ne 0 ]; then
echo "Shall I create '${env_name}' for you? [Yn]"
read -n 1 sure
if [ -z "$sure" ] || [ "$sure" = 'y' ] || [ "$sure" = 'Y' ]; then
# echo "Python version? [3.9]"
# read ver
pyenv virtualenv ${env_name}
pyenv activate ${env_name}
fi
fi
fi
}
fi
if [ -d ~/.poetry ]; then
prepend_path PATH $HOME/.poetry/bin
fi
# Workaround for Conda removing /usr/bin from my path
export CONDA_DEFAULT_ENV=dummy
# Command Completion
####################
# TODO move to ~/.zfunc
[ -f $HOME/Sync/dotfiles/completion/aws_zsh_completer.sh ] && source $HOME/Sync/dotfiles/completion/aws_zsh_completer.sh
if [ -n "$BASH_VERSION" ]; then
if [ -f $(brew --prefix)/etc/bash_completion ]; then
. $(brew --prefix)/etc/bash_completion
fi
GREEN="\[\033[1;32m\]"
BLUE="\[\033[1;34m\]"
CYAN="\[\033[1;36m\]"
UNCOLOR="\[\033[00m\]"
eCYAN="\x1b[1;36m"
eUNCOLOR="\x1b[0m"
###################################################
### git prompt hack
###################################################
# http://henrik.nyh.se/2008/12/git-dirty-prompt
# http://www.simplisticcomplexity.com/2008/03/13/show-your-git-branch-name-in-your-prompt/
# username@Machine ~/dev/dir[master]$ # clean working directory
# username@Machine ~/dev/dir[master*]$ # dirty working directory
#
# https://raw.github.com/git/git/master/contrib/completion/git-prompt.sh
function parse_git_dirty {
# 0 clean
# 1 dirty
# 129 not in git
git diff --no-ext-diff --quiet --exit-code 2> /dev/null
[[ $? -eq 1 ]] && echo "*"
}
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/ (Git branch: \1$(parse_git_dirty))/"
}
function active_virtualenv {
if [ -n "$VIRTUAL_ENV" ]
then
echo -en " (Virtualenv: $eCYAN$(basename "$VIRTUAL_ENV")$eUNCOLOR)"
# echo -n " (Virtualenv: $(basename "$VIRTUAL_ENV"))"
fi
}
# https://jakemccrary.com/blog/2015/05/03/put-the-last-commands-run-time-in-your-bash-prompt/
# https://stackoverflow.com/questions/1862510/how-can-the-last-commands-wall-time-be-put-in-the-bash-prompt/1862762#1862762
function timer_start {
timer=${timer:-$SECONDS}
}
function timer_stop {
timer_show=$(($SECONDS - $timer))
unset timer
}
trap 'timer_start' DEBUG
PROMPT_COMMAND="timer_stop"
# PS1="\n\[\033[01;32m\]\u@\h\[\033[01;34m\] \w\[\033[00m\]\$(active_virtualenv)\$(parse_git_branch)\n\$ "
PS1="\n⏳ $CYAN\D{%FT%T} ${UNCOLOR}last: \${timer_show}s exit: \$?\n$GREEN\u@\h$BLUE \w$UNCOLOR\$(active_virtualenv)\$(parse_git_branch)\n\$ "
export PS1
fi
# zsh prompt is in .zshrc
export STARSHIP_CONFIG=$HOME/Sync/dotfiles/.config/starship/starship.toml
# latest can be downloaded: http://standards.ieee.org/develop/regauth/oui/oui.txt
function mac {
OUI=$HOME/Sync/dotfiles/resources/oui.txt
if [ -z $1 ]; then
echo "Missing Parameter XX:XX:XX or xxxxxx"
else
grep -i $1 $OUI
fi
}
# MAC manipulators
# https://gist.github.com/jashkenas/5113149
alias random_mac='sudo ifconfig en0 ether `openssl rand -hex 6 | sed "s/\(..\)/\1:/g; s/.$//"`'
alias restore_mac='sudo ifconfig en0 ether YOUR_ORIGINAL_MAC_ADDRESS_GOES_HERE'
if [ -n "ZSH_VERSION" ]; then
eval "$(direnv hook zsh)"
fi
# Ubuntu 18.04 and pipx. This needs to take precedence over pyenv
prepend_path_if_exists PATH $HOME/.local/bin