-
Notifications
You must be signed in to change notification settings - Fork 5
/
bashrc
183 lines (150 loc) · 4.32 KB
/
bashrc
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
#!/bin/bash
# useful .bashrc functions... each one is explained in the comments
# usage: ama
# toggles between the QWERTY and dvorak keymaps in X. called "ama" because those
# are the only two keys that are in the same place on both keymaps. also sets
# the compose key
ama() {
# check to make sure X is running
[[ $DISPLAY ]] || return
# if the current keymap is dvorak, set to qwerty
if setxkbmap -print | grep -q 'dvorak'; then
setxkbmap us -option compose:ralt
# otherwise, set to dvorak
else
setxkbmap us -variant dvorak -option compose:ralt
fi
}
# usage: dd_progress DD_OPTIONS
# a very primitive progress checking version of 'dd' using USR1
# TODO: create a more complex version that gives an actual progress bar
dd_progress() {
local dd_pid loop_pid
# fork dd
dd "$@" & dd_pid=$!
# every minute, send USR1 to the dd process to get an update
while sleep 1m; do
kill -USR1 "$dd_pid"
done & loop_pid=$! # fork the loop as well, so that it ends when dd does
# wait for dd to finish. when it does, kill the loop as well
wait "$dd_pid"
kill "$loop_pid"
}
# usage: is_dir_in_path [DIR]
# basic function to check if a directory is in PATH. if DIR is not provided,
# uses the current working directory
is_dir_in_path() {
local path d t=${1:-$PWD}
# trim trailing '/' from DIR, if it exists
t=${1%/}
# create an array from PATH, split on ':'s
IFS=: read -rd '' -a path <<< "$PATH"
# loop over each entry, and check it against DIR (after trimming any
# trailing '/'s)
for d in "${path[@]}"; do
# if it matches, return true
[[ ${d%/} = "$t" ]] && return
done
# no match
return 1
}
# usage: read_paste URL CURL_OPTIONS
# downloads URL with curl and opens with VISUAL, EDITOR, or vi, in that order.
# CURL_OPTIONS, if provided, are passed as additional arguments to curl, before
# URL
read_paste() {
local tmp url=$1
shift
# create temp file and remove it when the function returns
tmp=$(mktemp) || return
trap 'rm -f "$tmp"' RETURN
curl -# -o "$tmp" "$@" "$url" || return
${VISUAL:-${EDITOR:-vi}} "$tmp"
}
# usage: sprunge [OPTIONS] [FILE ...]
#
# Upload FILEs to sprunge.us. If FILE is not provided, or is '-', reads the
# standard input
#
# Options:
# -n, --nonames Do not ever print the filenames in the output
# -h, --help Display this help and exit
sprunge() {
local f err=0 optstring=nh options=() c names=1
# break the options up into a more easily parsable format
while (($#)); do
case $1 in
-[!-]?*)
for ((i=1; i<${#1}; i++)); do
c=${1:i:1} options+=("-$c")
if [[ $optstring = *"$c:"* && ${1:i+1} ]]; then
options+=("${1:i+1}")
break
fi
done
;;
--?*=*) options+=("${1%%=*}" "${1#*=}");;
# end of options, stop breaking them up
--)
options+=(--endopts)
shift
options+=("$@")
break
;;
*) options+=("$1");;
esac
shift
done
# set new positional parameters to altered options
set -- "${options[@]}"
# read in options
while [[ $1 = -?* ]]; do
case $1 in
-n|--nonames) names=0;;
-h|--help)
cat <<'EOF'
usage: sprunge [OPTIONS] [FILE ...]
Upload FILEs to sprunge.us. If FILE is not provided, or is '-', reads the
standard input
Options:
-n, --nonames Do not ever print the filenames in the output
-h, --help Display this help and exit
EOF
return 0
;;
--) shift; break;;
*)
printf 'invalid option: %s\n' "$1" >&2
return 1
;;
esac
shift
done
# args provided, treat as files
if (($#)); then
# iterate over each FILE
for f; do
# stdin
if [[ $f = - || $f = /dev/stdin ]]; then
if (($# > 1 && names)); then
printf 'stdin: '
fi
curl -F 'sprunge=<-' http://sprunge.us || err=1
# make sure it's a file or fifo and is readable
elif [[ ( -f $f || -p $f ) && -r $f ]]; then
if (($# > 1 && names)); then
printf '%s: ' "$f"
fi
curl -F 'sprunge=<-' http://sprunge.us < "$f" || err=1
# unreadable or nonexistent file
else
printf '%s: premission denied\n' "$f" >&2
err=1
fi
done
# no args, read from stdin
else
curl -F 'sprunge=<-' http://sprunge.us || err=1
fi
return "$err"
}