-
Notifications
You must be signed in to change notification settings - Fork 3
/
.my_mac_zshrc
151 lines (132 loc) · 4.73 KB
/
.my_mac_zshrc
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
# This file contains code that is specific to OSX
# Homebrew path setup
export HOMEBREW_BREWFILE=~/homedir/Brewfile
if [ -f /opt/homebrew/bin/brew ]; then
eval "$(/opt/homebrew/bin/brew shellenv)"
fi
# Add a virtualenv to the path for each directory in ~/src
src_contents=$(find ~/src -mindepth 1 -maxdepth 1 -type d 2>/dev/null)
if [[ -n "$src_contents" ]]; then
echo "$src_contents" | while read -r env; do
env_name=$(basename "$env")
has_venv="$HOME/.virtualenvs/$env_name"
if [[ -d $has_venv ]]; then
alias $env_name="cd ~/src/$env_name; source $has_venv/bin/activate"
fi
done
fi
#### Aliases
alias ip='dig +short myip.opendns.com @resolver1.opendns.com'
alias localip="ipconfig getifaddr en0"
alias randpick='jot -r 1 1'
alias agp="ag -G '.py'"
alias family_guy="find ~/Dropbox/Watch/Family\ Guy/ -type f -print0 | sort -zR | head -n 1 | xargs -0 vlc -f --play-and-exit"
alias office="find ~/Dropbox/Watch/The\ Office/ -type f -print0 | sort -zR | head -n 1 | xargs -0 vlc -f --play-and-exit"
alias interpreter=$HOME/.virtualenvs/interpreter/bin/interpreter
#### Functions
function commit_link() {
## Grab the latest commit from the current repo and open it on github.com
# hash
hash=$(git log -n 1 --format="%H")
if [ $? != 0 ]; then
echo "Oops! Not a Git repository 🚫"
return
fi
# repo owner/name
repo=$(git remote -v | grep github.com | head -1 | awk -F ':' '{print $2}' | perl -p -e 's/\.git.+//')
url="https://github.com/$repo/commit/$hash"
echo "Opening latest commit on GitHub... 🔗"
open $url
}
function wifi_redirect() {
local check_interval=1
local test_url
if [[ -n "$1" ]]; then
test_url="$1"
else
test_url="http://example.com"
fi
while true; do
status_code=$(curl -s -o /dev/null -w "%{http_code}" "$test_url")
if [[ $status_code == "000" ]]; then
echo "🌐 Internet connection is down. Retrying in $check_interval second... ⏳"
sleep $check_interval
elif [[ $status_code -ge 300 && $status_code -lt 400 ]]; then
redirect_url=$(curl -s -I "$test_url" | grep -i "Location" | awk -F' ' '{print $2}' | tr -d '\r')
echo "🔄 Detected a redirect, opening $redirect_url 🌐"
open "$redirect_url"
break
elif [[ $status_code -eq 200 ]]; then
echo "✅ Internet is working. Opening https://ismyinternetworking.com 🌐"
open "https://ismyinternetworking.com"
break
else
echo "❓ Unexpected response from curl: $status_code. Exiting..."
return
fi
done
}
### Handle env files
# Function to source an env file and export variables
function load_env_file() {
local env_file=$1
if [[ ! -f $env_file ]]; then
echo "File not found: $env_file"
return 1
fi
# Source the file
source "$env_file"
# Export each variable
while IFS='=' read -r key value; do
# Skip blank lines and comments
[[ -z "$key" || "$key" =~ ^# ]] && continue
# Remove surrounding quotes from the value if present
value=$(echo "$value" | sed -e 's/^"//' -e 's/"$//')
# Export the key-value pair
export "$key=$value"
done <"$env_file"
}
# List of env file locations (files or directories)
env_locations=(
"$HOME/dot_env_shared"
"$HOME/dot_env_local"
"$HOME/env"
"$HOME/src/env"
"$HOME/.env"
)
# Loop through each location
for location in "${env_locations[@]}"; do
if [[ -d $location ]]; then
# If it's a directory, read all files in the directory
for env_file in "$location"/*; do
if [[ -f $env_file ]]; then
load_env_file "$env_file"
fi
done
elif [[ -f $location ]]; then
# If it's a file, read the file
load_env_file "$location"
fi
done
# New function to show environment variables loaded from each env file
function show_env_vars() {
for location in "${env_locations[@]}"; do
if [[ -d $location ]]; then
for env_file in "$location"/*; do
if [[ -f $env_file ]]; then
echo "\nVariables from $env_file:"
grep -v '^#' "$env_file" | while IFS='=' read -r key value; do
[[ -z "$key" ]] && continue
echo "$key=$(printenv $key)"
done
fi
done
elif [[ -f $location ]]; then
echo "\nVariables from $location:"
grep -v '^#' "$location" | while IFS='=' read -r key value; do
[[ -z "$key" ]] && continue
echo "$key=$(printenv $key)"
done
fi
done
}