-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.zshrc
More file actions
374 lines (301 loc) · 11 KB
/
Copy path.zshrc
File metadata and controls
374 lines (301 loc) · 11 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
# OhMyZsh configuation
export ZSH="$(echo ~)/.oh-my-zsh"
# ZSH_THEME="blinks-time"
ZSH_THEME="blinks"
HIST_STAMPS="dd.mm.yyyy"
COMPLETION_WAITING_DOTS="true"
# add plugins to ~/.oh-my-zsh/custom/plugins/
plugins=(git z)
source $ZSH/oh-my-zsh.sh
# initialize homebrew
eval "$(/opt/homebrew/bin/brew shellenv)"
# make sure keys are added to the agent
if ! ssh-add -l | grep -q "luke.abel@agilebits.com"; then
ssh-add --apple-use-keychain ~/.ssh/id_ed25519
fi
# if ! ssh-add -l | grep -q "luke.abel@gmail.com"; then
# ssh-add --apple-use-keychain ~/.ssh/id_ed25519_pers
# fi
# initialize fzf
[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh
# User configuration
setopt histignorealldups
unsetopt nomatch
## Autocomplete
autoload -Uz compinit
compinit
zstyle ':completion:*' auto-description 'specify: %d'
zstyle ':completion:*' completer _expand _complete _correct _approximate
## Paths 'n Flags
export USER_BIN=~/bin
export PATH=${USER_BIN}:$PATH
export PATH="/usr/local/sbin:$PATH"
export PATH="$(brew --prefix)/opt/libpq/bin:$PATH"
export PATH="$PATH:/Applications/Visual Studio Code.app/Contents/Resources/app/bin"
export CPPFLAGS="-I$(brew --prefix)/opt/libpq/include"
export LDFLAGS="-L$(brew --prefix)/opt/libpq/lib"
export PKG_CONFIG_PATH="$(brew --prefix)/opt/libpq/lib/pkgconfig"
# Force use of OpenSSL 3.5
export PATH="/opt/homebrew/opt/openssl@3.5/bin:$PATH"
export LDFLAGS="-L/opt/homebrew/opt/openssl@3/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl@3/include"
export PKG_CONFIG_PATH="/opt/homebrew/opt/openssl@3/lib/pkgconfig"
# For Ruby specifically
export RUBY_CONFIGURE_OPTS="--with-openssl-dir=/opt/homebrew/opt/openssl@3"
export HA_TOKEN="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiI4Zjg3ZjlhNjczMjA0Njg0YTU0ZDY4YmVjODEzNjRlNyIsImlhdCI6MTc4MTg2ODg0MSwiZXhwIjoyMDk3MjI4ODQxfQ.9zGReIIzkdzcODcVZQaX3NDMJjQ-W1s8RkSPGc9tZsc"
## Other Variables
export LANG=en_US.UTF-8
export RIPGREP_CONFIG_PATH=~/.ripgreprc
export NVM_DIR="$HOME/.nvm"
## Preferred editor for local and remote sessions
if [[ -n $SSH_CONNECTION ]]; then
export EDITOR='vim'
elif [[ -n $CODESPACES ]]; then
export EDITOR='vim.tiny'
else
export EDITOR='nvim'
fi
## Aliases
# alias glog='git log --graph --oneline --decorate'
alias be='bundle exec'
alias rake='noglob rake' # required for certain strap tasks
alias linecount='tee >(wc -l | xargs echo)' # for piping ripgrep
alias caff='caffdown 36000'
alias baudit='bundle exec bundle-audit update && bundle exec bundle-audit check'
alias claude-personal='CLAUDE_CONFIG_DIR=~/.claude-personal claude'
alias k2-agent='./bin/agent -- -a -p -u "https://app.kolide.test"'
# Start or resume a Claude session tied to the current git branch.
# Uses a deterministic UUID derived from the branch name so the same
# branch always maps to the same session.
function cb() {
local repo=$(basename `git rev-parse --show-toplevel`)
local branch=$(git symbolic-ref --short HEAD 2>/dev/null)
if [[ -z "$branch" ]]; then
echo "Not on a git branch. Starting claude normally."
claude "$@"
return
fi
# based on claude's slug derivation, but not load-bearing outside this command
local uuid=$(python3 -c "import uuid; print(uuid.uuid5(uuid.NAMESPACE_DNS, '$repo-$branch'))")
echo "Session for: $repo-$branch ($uuid)"
claude --resume "$uuid" "$@" 2>/dev/null
if [[ $? -ne 0 ]]; then
echo "No existing session found. Starting new session."
claude --session-id "$uuid" "$@"
fi
}
function n() {
nvim $1
}
function gcam() {
git commit -am $1
}
# Open a bash shell in the given Kubernetes pod
function kexec() {
kubectl exec -it "$1" -- /bin/bash
}
function kdebug() {
kubectl debug $(kubectl get pod -l app.kubernetes.io/component=workers -o name | head -n1) -it --copy-to=rails-console-lukeabel-1 --container=kolide2 -- bin/rails console
}
function kcleanup() {
kubectl delete pod rails-console-lukeabel-1
}
## Functions
# Start tmux with a standard layout:
# Window 0: tall left pane (~63%), two stacked right panes
# Window 1: two equal columns, right column split top/bottom
function tmux-dev() {
local session="${1:-dev}"
if [[ -n "$TMUX" ]]; then
echo "Already in a tmux session."
return 1
fi
if tmux has-session -t "$session" 2>/dev/null; then
tmux attach -t "$session"
return
fi
# Window 0: left pane + two stacked right panes
# Use explicit cols and lines so we get an accurate split
tmux new-session -d -s "$session" -x "$(tput cols)" -y "$(tput lines)"
tmux split-window -h -p 45 -t "$session"
tmux split-window -v -t "$session"
tmux select-pane -t "$session:.0"
# Window 1: 50/50 split, then split right pane top/bottom
tmux new-window -t "$session"
tmux split-window -h -p 50 -t "$session"
tmux split-window -v -t "$session"
tmux select-pane -t "$session:.0"
# Start on window 0
tmux select-window -t "$session:0"
tmux attach -t "$session"
}
# find filenames that look like arg
function rgg() {
rg --iglob "*$1*" -g '!/Library/' --files
}
# match literal string for arg
# useful for characters that bash wants to interpret
function rgl() {
rg -F $1 $2
}
# match a string in files AND filenames
function rga() {
( rg $1; rgg $1 )
}
# find and replace. leave second arg blank for find-and-remove.
# supports filenames with whitespace.
function rgr() {
rg $1 --files-with-matches -0 | xargs -0 sed -i '' "s/$1/$2/g"
}
# count occurrences of arg
function rgcount() {
rg $1 | tee >(wc -l | xargs echo)
}
# search for an element by attribute key and/or value
function rgelement() {
rg --multiline --multiline-dotall "<$1[^<]*$2[^<]*>"
}
# The following function runs the caffeinate command for a given number of seconds,
# and then displays a countdown for that amount of time.
# It hides the curser while the countdown is running, and then displays a message
# when the countdown is complete, and restores the cursor visibility.
function caffdown() {
caffeinate -dimsut $1 &
local caffeinate_pid=$!
tput civis
trap "local stop_script=1; kill $caffeinate_pid; tput cnorm; echo \"\n caffdown cancelled!\"" SIGINT SIGTERM EXIT
for i in $(seq $1 -1 1); do
local hours=$((i/3600))
local minutes=$(( (i%3600)/60 ))
local seconds=$(( (i%60) ))
printf "%02d:%02d:%02d\033[0K\r" $hours $minutes $seconds
if [[ $stop_script ]]; then
break
fi
sleep 1
done
tput cnorm
trap - SIGINT SIGTERM EXIT
}
function profanno() {
local filepath="$1"
local filename=$(basename "$filepath")
be stackprof tmp/stackprof.dump --file "$filepath" > "tmp/$filename"
}
# Function to check out latest release canddiate branch
function gcol() {
# fetch to make sure we have the latest branches
git fetch
# Get all branches matching the release candidate pattern
branches=$(git branch -a | grep 'remotes/origin/v[0-9]*\.[0-9]*\(\.[0-9]*\)\?-candidate' | sed 's/.*remotes\/origin\///')
# Initialize variables to store the latest version and branch
latest_version="0.0.0"
latest_branch=""
# Iterate through the branches
while IFS= read -r branch; do
# Extract the version number from the branch name
version=$(echo "$branch" | sed -E 's/.*v([0-9]+\.[0-9]+(\.[0-9]+)?)-candidate/\1/')
# Compare the version with the current latest version
if [[ "$version" > "$latest_version" ]]; then
latest_version="$version"
latest_branch="$branch"
fi
done <<< "$branches"
# Check out the latest release candidate branch
if [[ -n "$latest_branch" ]]; then
git checkout "$latest_branch"
# Check for updates and pull if needed
git fetch origin "$latest_branch"
if [[ $(git rev-parse HEAD) != $(git rev-parse origin/"$latest_branch") ]]; then
echo "Updates found. Pulling latest changes..."
git pull origin "$latest_branch"
fi
else
echo "No release candidate branches found."
fi
}
# Function to create and open a timestamped scratch file
function makescratch() {
# Check if a name was provided
if [[ -z "$1" ]]; then
echo "Error: Please provide a name for the scratch file."
echo "Usage: makescratch <name>"
return 1
fi
# Get the current timestamp in YYYYMMDD_HHMMSS format
local timestamp=$(date +"%Y%m%d_%H%M%S")
# Find the project root (assuming it's a git repository)
local project_root=$(git rev-parse --show-toplevel 2>/dev/null)
if [[ -z "$project_root" ]]; then
echo "Error: Not in a git repository."
return 1
fi
# Create the scratch directory if it doesn't exist
local scratch_dir="${project_root}/scratch"
mkdir -p "$scratch_dir"
# Create the scratch file with the given name and timestamp
local filename="${1}_${timestamp}.rb"
local filepath="${scratch_dir}/${filename}"
# Add a header comment to the file
echo "# Scratch file: ${filename}" > "$filepath"
echo "# Created: $(date)" >> "$filepath"
echo "" >> "$filepath"
# Open the file in Neovim
nvim "$filepath"
}
# Copy git patch from given file
function gcopy() {
git diff $1 | pbcopy
}
# Apply git patch from macOS clipboard (pbpaste)
git-apply-clipboard() {
local patch_file=$(mktemp -t clipboard_patch.XXXXXX.patch)
# Get clipboard, strip all CR characters (handles CRLF), save to temp file【7】【8】
if ! pbpaste | tr -d '\r' > "$patch_file"; then
echo "Error: Failed to read clipboard" >&2
rm -f "$patch_file"
return 1
fi
# Unconditionally ensure trailing newline (fixes "corrupt patch at line X")【9】
# Extra blank line at end is harmless to git apply
if [[ -s "$patch_file" ]]; then
printf '\n' >> "$patch_file"
fi
if git apply "$patch_file"; then
echo "✓ Patch applied successfully"
rm -f "$patch_file"
else
echo "✗ Error: Failed to apply patch" >&2
echo " Patch preserved at: $patch_file" >&2
echo "" >&2
echo "Debug - last 5 lines (od -c shows \\r as \\r, \\n as \\n):" >&2
tail -n 5 "$patch_file" | od -c >&2
echo "" >&2
echo "If you see \\r (carriage returns) above, that's the issue." >&2
return 1
fi
}
# Optional alias for quicker access
alias gapply='git-apply-clipboard'
# retrieve and set environment variables in the MacOS Keychain
function keychain-environment-variable () {
security find-generic-password -w -a ${USER} -D "environment variable" -s "${1}"
}
function set-keychain-environment-variable () {
[ -n "$1" ] || print "Missing environment variable name"
read "?Enter Value for ${1}: " secret
( [ -n "$1" ] && [ -n "$secret" ] ) || return 1
security add-generic-password -U -a ${USER} -D "environment variable" -s "${1}" -w "${secret}"
}
# Automatically set keychain-stored environment variables in the current shell
if command -v security >/dev/null 2>&1; then
# export GITHUB_USER=$(keychain-environment-variable GITHUB_USER);
# export GITHUB_TOKEN=$(keychain-environment-variable GITHUB_TOKEN);
fi
## Last Call
if [ "$(uname -s)" = "Darwin" ]; then
eval "$(rbenv init -)"
[ -s "$(brew --prefix)/opt/nvm/nvm.sh" ] && \. "$(brew --prefix)/opt/nvm/nvm.sh" # This loads nvm
[ -s "$(brew --prefix)/opt/nvm/etc/bash_completion.d/nvm" ] && \. "$(brew --prefix)/opt/nvm/etc/bash_completion.d/nvm" # This loads nvm bash_completion
fi
export PATH="$HOME/.local/bin:$PATH"