-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions
executable file
·466 lines (406 loc) · 10.9 KB
/
functions
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
#! /usr/bin/env bash
# If tty is connected to stdin
if [ -t 0 ]; then
# Replace current editing line with evald echo, via C-t
_replace() { READLINE_LINE="$(eval echo "$READLINE_LINE")"; }
bind -m emacs -x '"\C-t": _replace'
fi
function copy {
if [[ "$(uname)" -eq "Linux" ]]; then
xsel -ib;
else
pbcopy;
fi;
}
# Truncated a file to its last 10K lines
#
# trunklog /var/log/syslog
function trunklog {
tail -n 10000 $1 | sponge $1
}
# Execute a command in each pane inside a tmux session
# tmuxrun $COMMAND
# tmuxrun "echo hi"
function tmuxrun {
local run_command="$@";
tmux list-panes -s -F '#{pane_id} #{pane_current_command}' |
awk '/bash/ { print $1; }' |
while read pane; do
tmux send-keys -t $pane "$run_command" Enter;
done
}
function paste {
if [[ "$(uname)" -eq "Linux" ]]; then
xclip -o -selection clipboard;
else
pbpaste;
fi;
}
# vimmake 'some unix command which generates a locationlist'
function vimmake {
vim -c "set makeprg=$(echo $1 | sed 's: :\\ :g') | make";
}
function pubkey {
copy < ~/.ssh/id_rsa.pub
}
# Try man, then help, then --help until some docs are found
function h {
man "$1" 2>&- || { { help "$1" 2>&- || $1 --help; } | less; }
}
# Create or use existing weekly file for t's task list
function weekly-task-file {
local task_dir;
if [ -d ~/.tasks ]; then
task_dir=~/.tasks
else
mkdir -p /tmp/tasks;
task_dir=/tmp/tasks;
fi;
local DAY="$(printf "%02d" $(($(date +%_d) - $(date +%_u) + 1)))";
local DATE="$(date +%Y-%m-)${DAY}"
local TASK_FILE="${task_dir}/tasks-${DATE}";
local TASK_DONE_FILE="${task_dir}/.tasks-${DATE}.done";
touch $TASK_FILE $TASK_DONE_FILE;
echo "Using $TASK_FILE as task list.";
ln -sf $TASK_FILE "${task_dir}/tasks";
ln -sf $TASK_DONE_FILE "${task_dir}/.tasks.done";
local PREV_DATE="$(date --date="${DATE} -7 day" +%F)"
local PREV_TASK_FILE="${task_dir}/tasks-${PREV_DATE}";
if [ -e $PREV_TASK_FILE ]; then
echo "Importing tasks from prior week.";
sort -u ${PREV_TASK_FILE} ${TASK_FILE} | sponge ${TASK_FILE};
fi;
}
# Print my jiras from the last week
function printjiras {
local jql api;
read -p "Username? " user;
read -s -p "Password? " pass;
echo;
jql="reporter = $user AND createdDate >= -1w"
api="https://pods.iplantcollaborative.org/jira/rest/api/2/search"
# Fetch the json
curl --fail --silent --show-error \
--data-urlencode "jql=$jql" \
-G \
-H "Content-Type: application/json" \
-X GET -H "Authorization: Basic $(echo -n $user:$pass | base64)" \
"$api" |
# Format json into a small bash program
jq -r '.issues[] | .key as $ISSUE | .fields | @sh "echo $(date -d" + .created + " +%D) " + $ISSUE + " \"" + .summary + "\""' |
# Evaluate it
bash;
}
# Get the path to a python package
function pipwhich {
pip show $1 | grep -Po '^Location: \K.*';
}
# Ask the process running on a port to just die already
# > killport 5000
function killport {
kill $(lsof -i :$1 -t);
}
# echo -e "a\nb\n" | take 2
# > b
function take {
awk "NR == $1 { print; exit 0; }";
}
# echo "abcd" | from 1 2
# > "bc"
function from() {
head -c $2 | tail -c $(($2 - $1));
}
function filter {
xargs -I{} bash -c "$@ &>/dev/null && echo {}";
}
function join {
tr -d "\n"
}
# l(eft)trim the input
# echo " a" | ltrim
# > "a"
function ltrim {
sed 's/^[ \t]*//'
}
function ju {
cd $(find . -type d | fzf)
}
function ? {
test $@ && echo true;
}
function debugger {
sed -i 's:// debugger:debugger:' $@
}
function nodebugger {
sed -i 's:^\( *\)debugger:\1// debugger:' $@
}
function get_ps1 {
tput -S <<< "
cup 0 0
el
cup 1 0
el
cup 0 0
"
host=$(hostname)
host=${host%%.*}
width=$(tput cols)
ls -pt -G | xargs > /tmp/blah
lines=$(wc -l < /tmp/blah )
echo $lines
tput -S <<< "
cup $(( $lines + 2 )) 0
el
cup 0 0
"
if [ $(whoami) == "root" ]; then
symb="■"
else
symb="●"
fi
cat /tmp/blah | gawk '
BEGIN {
"tput cols" | getline width
"date" | getline date
printf "┏"
w = width
dateLength = length(date);
while (--w > dateLength + 2) {
printf "━";
}
printf date
printf "━┓\n"
}
{
w = width - 4;
printf "┃ %-"w"s ┃\n", substr($0, 0, length($0))
}
END {
printf "┗"
w = width
while (--w > 1) {
printf "━";
}
printf "┛\n"
}
'
echo "$host..$(basename $(pwd))/$symb "
}
function 9man {
if [[ $# -eq 0 ]]; then
man
return
fi
cat $(/usr/bin/env man $@ -w) | 9 troff -N -mantimes | 9 sed '
${
/^$/p
}
//N
/^\n$/D
' | less
}
function args {
while [ $# -gt 0 ]; do
echo "'$1'";
shift;
done;
}
#
# ve NAME creates and activates venv
# ve wipe remove and deactivate current venv
# ve wipe NAME... remove multiple venvs
# ve print all venvs
# ve [-h|--help] print usage
function ve {
#
# Use $VE_DIR it it exists in env
#
local VE_DIR="${VE_DIR:-$HOME/.ve}"
case $1 in
--help|-h )
echo "ve List all virtual envs";
echo "ve wipe [NAME...] Erase virtual envs";
echo "ve NAME Create or activate virtual env";
echo "ve [-h|--help] Print usage";
;;
"" )
if [ ! -d "$VE_DIR" ]; then
mkdir "$VE_DIR";
fi;
ls -1 "$VE_DIR";
;;
"wipe" )
shift;
#
# ve wipe w/o arguments removes current venv
#
if [ "$#" -eq 0 -a -d "$VIRTUAL_ENV" ]; then
rm -rf "$VIRTUAL_ENV";
fi;
#
# ve wipe with arguments removes each argument
#
local VENVS="$(ve)"
for ENV in $@; do
local DIR="$VE_DIR/$ENV";
if [[ "$VENVS" =~ "$ENV" ]] && [ -d "$DIR" ]; then
rm -rf "$DIR";
fi;
done
#
# ve wipe deactivates an activated but deleted venv
#
if [ "$VIRTUAL_ENV" -a ! -d "$VIRTUAL_ENV" ]; then
deactivate;
fi;
;;
* )
#
# ve NAME deactivates, creates if necessary, then activates
#
if [ -d "$VIRTUAL_ENV" ]; then
deactivate;
fi;
if [ ! -d "$VE_DIR/$1" ]; then
"$(which python3)" -m venv "$VE_DIR/$1";
fi;
if [ -e "$VE_DIR/$1/bin/activate" ]; then
. "$VE_DIR/$1/bin/activate";
fi
;;
esac
}
pyenv() {
PYENV_PATH="${HOME}/.pyenv/bin:${HOME}/.pyenv/shims"
if [[ ! "${PATH}" =~ "${PYENV_PATH}" ]]; then
export PATH="${PYENV_PATH}:${PATH}"
fi
export PYENV_SHELL=bash
command pyenv rehash 2>/dev/null
local command
command="$1"
if [ "$#" -gt 0 ]; then
shift
fi
case "$command" in
rehash|shell)
eval "$(pyenv "sh-$command" "$@")";;
*)
command pyenv "$command" "$@";;
esac
}
# reload-browser - A cross-platform wrapper for reloading the current
# browser tab
# Eric Radman, 2014
# http://entrproject.org/
reload-browser() {
usage() {
case `uname` in
Darwin)
# applescript needs the exact title
echo "Usage: reload-browser Firefox [Safari \"Google Chrome\" ...]"
;;
*)
# xdotool uses regular expressions
echo "Usage: reload-browser Firefox [Chrome ...]"
;;
esac
return 1
}
[ $# -lt 1 ] && usage;
for app in "$@";
do
case `uname` in
Darwin)
/usr/bin/osascript <<< "
set prev to (path to frontmost application as text)
tell application \"$app\"
activate
end tell
tell application \"System Events\"
keystroke \"r\" using {command down}
delay 0.3
end tell
activate application prev
"
;;
*)
xdotool search --onlyvisible --class "$app" windowfocus key \
--window %@ 'ctrl+r' || {
1>&2 echo "unable to signal an application named \"$app\""
}
;;
esac
done
}
#
# trim file1 file2 ... - Trim whitespace from files
#
function trim {
case `uname` in
Darwin)
sed -i '' 's/ *$//' $*
;;
*)
sed -i 's/ *$//' $*
;;
esac
}
#
# For each line of stdin execute command, where the {} is expanded to the
# input line.
#
# { echo a; echo b; } | each echo - {}
#
function each {
xargs -I{} $@
}
#
# Re-export the auth sock when it gets trampled.
#
# export-auth-sock
#
function export-auth-sock {
local NUM_SOCKETS=$(echo /tmp/ssh*/* | wc -l)
if [ "$NUM_SOCKETS" -eq 1 ]; then
export SSH_AUTH_SOCK="$(echo /tmp/ssh*/*)";
return;
else
echo "SSH_AUTH_SOCK was not set" >&2;
false;
fi
}
function t {
local task_dir;
if [ -d ~/.tasks ]; then
task_dir=~/.tasks
else
mkdir -p /tmp/tasks;
task_dir=/tmp/tasks;
fi;
python ~/dotfiles/scripts/t.py --task-dir "$task_dir" --list tasks $@;
}
function randrwork {
xrandr --output VIRTUAL1 --off --output eDP1 --off --output DP1 --off --output HDMI2 --primary --mode 3840x2160 --pos 0x0 --rotate normal --output HDMI1 --off --output DP2 --off;
}
function randrreset {
xrandr --output HDMI2 --off --output eDP1 --auto;
}
function randroldhome {
xrandr --output HDMI2 --scale 1.5x1.5 --mode 1920x1080 --fb 2880x1620 --panning 2880x1620 --output eDP1 --off;
}
function randrhome {
local width=2560 height=1440 scaledWidth=3840 scaledHeight=2160
xrandr --output HDMI2 --scale 1.5x1.5 --mode "${width}x${height}" --fb "${scaledWidth}x${scaledHeight}" --panning "${scaledWidth}x${scaledHeight}" --output eDP1 --off;
}
function tmux-close-other-windows-with-same-name {
local CUR_WIN_INDEX CUR_WIN_NAME;
read CUR_WIN_INDEX CUR_WIN_NAME <<< "$(tmux display-message -p "#{window_index} #{window_name}")"
tmux list-windows -F '#{window_index} #{window_name}' | while read WINDOW_INDEX WINDOW_NAME; do
if [ "$CUR_WIN_INDEX" == "$WINDOW_INDEX" ]; then
continue
elif [ "$CUR_WIN_NAME" == "$WINDOW_NAME" ]; then
tmux kill-window -t "$WINDOW_INDEX";
fi
done
}