-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpers.bash
325 lines (287 loc) · 7.49 KB
/
helpers.bash
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
# Collection of useful helpers for day-to-day bash shell'ing.
# Make sure to `source` your helpers rather than executing the bash file.
# Substitute for `cd`
c() {
cd *${*}*
pwd
ls
}
# Find file named like $1 in the cwd
# Example:
# $ f help
# ./helpers.bash
f() {
find . -name "*$**"
}
# Find directory named like $1 under cwd and cd into it
# Example:
# dotfiles $ fcd bin
# dotfiles/local/bin $
fcd() {
local target="$(find . -name "*${*}*" -type d | head -n1)"
if [[ "$target" ]]; then
cd "$target"
else
echo "Directory not found: $*"; return
fi
}
# Find a source dir using findsrc and cd into it
scd() {
local target="$(findsrc "${*}" | head -n1)"
if [[ "$target" ]]; then
cd "$target"
else
echo "Directory not found: $*"; return
fi
}
# Find a running process named like $1
# Example:
# $ p iterm
# shazow 540 5.7 2.1 ... 23:03.17 /opt/homebrew-cask/Caskroom/iterm2-beta/1.0.0.20140629/iTerm.app/Contents/MacOS/iTerm
p() {
ps aux | grep "$*"
}
# Grep in cwd
# Example:
# $ g "cwd and cd"
# ./helpers.bash:# Find directory named like $1 under cwd and cd into it
g() {
grep --exclude-dir='*/\.*' -Ir "$(echo $*)" .
}
# Double-grep: grep with files resulting of the first grep
# Example: Find all source-ing in files that mention helpers.bash
# $ gg "helpers.bash" "source"
# ./.bashrc: source $DOTFILES_PATH/.bash_profile
# ...
gg() {
grep --exclude-dir='*/\.*' -Irl "${1}" . | xargs grep -I "${2}"
}
# Grep in cwd and replace $1 with $2 in-line. (Uses regexp, remember to escape when necessary.)
# Example:
# $ greplace "fcd" "findcd"
# Replacing: ./helpers.bash
greplace() {
grep --exclude-dir='*/\.*' -Irl "$1" . | while read i; do
echo "Replacing: $i"
perl -p -i -e "s/$1/$2/g" "$i"
done
}
# Grep in files named like $1 for subpattern $2
# Example:
# $ ingrep bash randomline
# ./.bash_aliases:alias cdrandom='cd "$(lsdir | randomline $(lsdir | wc -l))"'
# ./helpers.bash:randomline() {
ingrep() {
find . -name "*${1}*" -exec grep -I "${2}" {} +
}
# Print a random number between two input values. (Default 1,n or 0,1)
random() {
declare a="$1" b="$2"
if [[ ! "$a" ]]; then
a="0";
b="1";
elif [[ ! "$b" ]]; then
b="$a";
a="1";
fi
local range="$[ $b - $a + 1]";
echo "$[ ( $RANDOM % $range ) + $a ]";
}
# Given a number of lines, read from stdin and echo a random one.
randomline() {
if [[ ! "$1" ]]; then
echo "Must specify how many lines to consider."
return 1
fi
local count="$(random 1 $1)";
while read line; do
if [[ "$count" == "1" ]]; then
echo "$line";
break;
fi
count="$[ $count - 1 ]"
done
}
# Send file to a given email address as attachment
mailfile() {
# uuenview can be replaced with uuencode (bin depends on the distro)
uuenview "$1" | mail -s "$(basename $1)" $2
}
# Move target $1 to $1.bak
# Example:
# $ bak helpers.bash
# helpers.bash -> helpers.bash.bak
bak() {
declare target=$1;
if [[ "${target:0-1}" = "/" ]]; then
target=${target%%/}; # Strip trailing / of directories
fi
mv -v $target{,.bak}
}
# Revert previously bak'd $1 target
# Example:
# $ unbak *.bak
# helpers.bash.bak -> helpers.bash
unbak() {
declare target=$1;
if [[ "${target:0-1}" = "/" ]]; then
# Strip trailing / of directories
target="${target%%/}"
fi
if [[ "${target:0-4}" = ".bak" ]]; then
mv -v "$target" "${target%%.bak}"
else
echo "No .bak extension, ignoring: $target"
fi
}
if ! (which say &> /dev/null) then
function say() { echo "$*" | festival --tts; }
fi
# Open modified git files usin `v`
vmod() {
v $(git status | grep 'modified:' | cut -d ' ' -f4 | xargs)
}
# Watch a command for diffs every second
w() {
watch -dn1 $*
}
# Workspace navigation functions
if [[ -z "$PROJECTS_DIR" ]]; then
PROJECTS_DIR="$HOME/projects"
fi
if [[ -z "$SOURCE_DIRS" ]]; then
SOURCE_DIRS="$PROJECTS_DIR"
fi
BIN_GO="$(which go 2> /dev/null)"
# Jump to a project (and activate environment)
go() {
declare to=$1
if [[ ! "$to" ]]; then
# Go to the last go'ne destination
to=$(grep "^go " ~/.bash_history | tail -n1 | cut -d ' ' -f2-)
fi
target=$PROJECTS_DIR/$to
if [[ -d $target ]]; then
cd "$target"
# Load project profile (e.g. virtualenv)
[[ -e .profile ]] && . .profile
elif [[ "$BIN_GO" ]]; then
$(which go) "$@"
return
fi
}
# Autocomplete function for go
_complete_go() {
COMPREPLY=( $(compgen -W "$(ls $PROJECTS_DIR/)" -- "${COMP_WORDS[$COMP_CWORD]}") )
}
complete -F _complete_go go
exitenv() {
export PS1="${PS1##(*) }"
}
# Add $1 to $PS1
enterenv() {
exitenv
export PS1="($1) $PS1"
}
# Make a fresh virtualenv [for some existing directory [with a give environment name]]
create_virtualenv() {
if [[ "$VIRTUAL_ENV" ]]; then
deactivate
fi
if [[ "$1" ]]; then
name="$(basename $1)"
path=$1
else
name="$(basename $PWD)"
path="$PWD"
fi
if [[ "$2" ]]; then
name="$2"
fi
if [[ "$1" == "." ]]; then
env_path=".env"
name="$(basename $PWD)"
else
env_path="$path/.env"
fi
if [[ -d "$env_path" ]]; then
echo "$env_path already exists. Activating and aborting."
source "$env_path/bin/activate"
return 1
fi
virtualenv "$env_path" -p "$(which python)" --prompt="($name)"
ln -s "$env_path/bin/activate" "$path/.profile"
source "$env_path/bin/activate"
}
# Traverse shallow depth of SOURCE_DIRS and find the first match
findsrc() {
declare query="$1" srcpaths=""
if [[ -z "$SOURCE_DIRS" ]]; then
echo "\$SOURCE_DIRS is empty."
return 1
fi
IFS=":" srcpaths=( $SOURCE_DIRS )
if [[ ! "$srcpaths" ]]; then
# Single element
srcpaths=( "$SOURCE_DIRS" )
fi
# TODO: Backport to `find`?
fd -d3 -td "$query" ${srcpaths[@]}
}
# cd to root of the current repository
# Example:
# dotfiles/local/bin $ up
# Found .git at .../dotfiles
# dotfiles $
up() {
local old_pwd="$PWD"
while [[ 1 ]]; do
cd ..
if [[ "$PWD" == "/" ]]; then
cd "$old_pwd"
echo "No repository found, returned to $PWD"
return 1
fi
for repo in ".git" ".hg"; do
if [[ -d "$repo" ]]; then
echo "Found $repo at $PWD"
return 0
fi
done
done
}
# find the nearest parent $1*.nix file and nix-shell into it, without changing
# directories.
upshell() {
declare pattern="${1}*.nix"
local cwd=${PWD%/}
while [[ -d $cwd ]]; do
local candidate="$(compgen -G "${cwd}/${pattern}" | head -n1)"
if [[ -f "${candidate}" ]]; then
echo "Found nix-shell, entering: ${candidate}"
nix-shell "${candidate}"
return 0
fi
cwd=${cwd%/*}
done
echo "No nix-shell: ${1}*.nix"
return 1
}
# http://www.foo.com/bar -> foo.com
domain() {
local parts=(${1//\// });
local domain="${parts[1]}"
if [[ ! "$domain" ]] || [[ "$domain" != *.* ]]; then
domain="${parts[0]}"
fi
echo "${domain/www./}"
}
# whois, but a bit smarter (parse domains out of urls)
whois() {
$(which whois) "$(domain $1)"
return $?
}
# dig wrapper for returning all records
dns() {
dig +nocmd "$(domain $1)" any +multiline +noall +answer
}