/
.functions
310 lines (297 loc) · 6.06 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
#!/usr/bin/env bash
less()
{
typeset param parsed
typeset -a params
if (( $# == 1 )) && [[ -f "$1" ]] && (( $(wc -l < "$1") < ${LINES:-20} ))
then
cat $1
return 0
fi
for param in "$@"
do
case $param in
(*:*)
if [[ -f "$param" ]]
then
params+=( "$param" )
else
parsed=( ${param//:/ } )
params+=( "-N" "+j${parsed[1]}" "${parsed[0]}" )
fi
;;
(*)
params+=( "$param" )
;;
esac
done
command less "${params[@]}"
}
get()
{
typeset count url file
count=$1
url=$2
file=$3
if [[ -z "$count" ]]
then
echo "Usage: $0 [count] url [target]"
return 1
fi
if [[ -z "$url" ]]
then
url="$count"
count=2
fi
if [[ -z "$file" ]]
then
file="$(basename "${url%%\?*}")";
fi
echo "Save $url to $file in $count threads."
aria2c -c -x $count -j $count -s $count $url -o $file
}
if ! which wget > /dev/null 2>&1
then
wget()
{
curl $1 -o $(basename $1)
}
fi
if ! which ldd > /dev/null 2>&1 && which otool > /dev/null 2>&1
then
ldd()
{
otool -L "$@"
}
fi
d()
{
[[ -d $1 ]] || mkdir -p $1
cd $1
}
gwmpush()
{
git pull $@ && git pull bitbucket master $@ && git push && git push bitbucket master
}
gpp()
{
git pull $@ && git push
}
gcaa()
{
git add .
git commit --amend
}
gcamp()
{
gcam "$@"
gpp
}
gcam()
{
git add .
git commit -m "$*"
}
gcm()
{
git commit -m "$*"
}
gf()
{
typeset command subcommand version
command=$1
subcommand=$2
version=${3:-}
case $command in
(r*) command=release ;;
(h*) command=hotfix ;;
esac
case $subcommand in
(s*) subcommand=start ;;
(f*) subcommand=finish ;;
esac
if [[ -z "$version" ]]
then
case "$command-$subcommand" in
(release-start) version=$(awk -F "[.-]" '{ if ($4=="") print $1"."$2+1".0"; else print $1"."$2"."$3;}' < VERSION ) ;; #'
(hotfix-start) version=$(awk -F "." '{ print $1"."$2"."$3+1}' < VERSION ) ;;
(*-finish) version=$(cat VERSION) ;;
esac
fi
git flow $command $subcommand $version
if [[ "$subcommand" == "start" ]]
then
echo $version > VERSION
git add VERSION
git commit -m $version
fi
}
show_git() {
typeset limit path top
limit=$(git config dir.limit 2>/dev/null)
path="$PWD"
top=$(git rev-parse --show-toplevel 2>/dev/null)
[[ -z "$limit" ]] && return 0
while (( limit >= 0 ))
do
case "$path" in
($top) return 0 ;;
(/|$HOME) return 1 ;;
esac
path="$( dirname "$path" )"
: $(( --limit ))
done
return 1
}
function pgd {
show_git && git status --porcelain 2>/dev/null |
sed 's/^\(.\)\(.\).*$/\1 \2/' | # ?? ... => ? ?
awk -v out="$1" '
BEGIN { x=0;y=0; }
$1!=" " && $1!="?"{ x++; }
$2!=" "{ y++; }
END{
if(out==""){
if(x>0)print "uncomitted"; else if (y>0) print "dirty";
}
else{
if(x>0||y>0)print out;
}
}
'
}
function pgb {
show_git && __git_ps1 "${1:-%s}" 2>/dev/null
}
function pgr {
show_git && git status 2>/dev/null | awk -v out="$1" '/# Your branch is / { if(out=="") printf $5; else printf out }' #'
}
function _rvm_curr {
typeset __rvm_curr
__rvm_curr=$(__rvm_env_string)
[[ -n "${__rvm_curr}" ]] && printf "${__rvm_curr##ruby-} "
}
function show_function() {
typeset -f $@
}
function edit_function() {
typeset body name
name="$*"
name="${name// /-}"
if
(( $# > 0 )) &&
show_function $@ >/dev/null
then
show_function $@ > ${TMPDIR:-/tmp}/__function_${name}_1.sh
cp -f ${TMPDIR:-/tmp}/__function_${name}_1.sh ${TMPDIR:-/tmp}/__function_${name}_2.sh
${EDITOR:-vim} ${TMPDIR:-/tmp}/__function_${name}_1.sh
diff -qwB ${TMPDIR:-/tmp}/__function_${name}_1.sh ${TMPDIR:-/tmp}/__function_${name}_2.sh >/dev/null ||
source ${TMPDIR:-/tmp}/__function_${name}_1.sh
rm -f ${TMPDIR:-/tmp}/__function_${name}_{1,2}.sh
else
echo "there is no function $name"
fi
}
function edit_variable() {
typeset body name
name="$*"
name="${name// /-}"
if
(( $# > 0 )) &&
eval "[[ -n \${$1} ]]"
then
set | grep -a "^$1=" > ${TMPDIR:-/tmp}/__function_${name}_1.sh
cp -f ${TMPDIR:-/tmp}/__function_${name}_1.sh ${TMPDIR:-/tmp}/__function_${name}_2.sh
${EDITOR:-vim} ${TMPDIR:-/tmp}/__function_${name}_1.sh
diff -qwB ${TMPDIR:-/tmp}/__function_${name}_1.sh ${TMPDIR:-/tmp}/__function_${name}_2.sh >/dev/null ||
source ${TMPDIR:-/tmp}/__function_${name}_1.sh
rm -f ${TMPDIR:-/tmp}/__function_${name}_{1,2}.sh
else
echo "there is no function $name"
fi
}
__setup_ssh_agent()
{
# skip if already loaded
if [[ -n "$SSH_AGENT_PID" ]] && ps -p $SSH_AGENT_PID | grep ssh-agent >/dev/null
then return 0
fi
typeset agent_file
agent_file=~/.ssh-agent-config
if [[ -f $agent_file ]]
then source $agent_file >/dev/null
fi
if [[ -n "$SSH_AGENT_PID" ]] && ps -p $SSH_AGENT_PID | grep ssh-agent >/dev/null
then return 0
fi
ssh-agent > $agent_file
chmod 600 $agent_file
source $agent_file >/dev/null
if [[ -t 0 ]] && ! ssh-add -l | grep $HOME/.ssh/id_rsa >/dev/null
then
ssh-add
fi
}
function print_part()
{
(( $1 )) || return 0
typeset ret
ret="$1 $2"
(( $1 == 1 )) || ret="${ret}s"
printf "$ret "
}
function time_offset()
{
typeset years months days hours minutes seconds
minutes="$(( $1 / 60 % 60 ))"
hours="$(( $1 / 3600 % 24 ))"
days="$(( $1 / 86400 % 31 ))"
months="$(( $1 / 2678400 % 12 ))"
years="$(( $1 / 31536000 ))"
seconds="$(( $1 % 60 ))"
for part in year month day hour minute second
do eval "print_part \${${part}s} ${part}"
done
}
function n()
{
typeset return_status
typeset __n_time=$( date +%s )
if "$@"
then return_status=0
else return_status=$?
fi
__n_time=$(( $( date +%s ) - __n_time ))
if
(( __n_time > ${n_time:-10} ))
then
notify-send --app-name="N $1" "
Finished: $*
Took: $(time_offset __n_time)
Status: $return_status
"
fi;
return $return_status
}
__backtrace()
{
typeset counter
counter=0
while caller $counter
do : $(( counter+=1 ))
done
}
if ! which mcedit >/dev/null
then
mcedit()
{
vim "$@"
}
fi
if ! command -v rvm >/dev/null 2>&1
then
rvm()
{
echo "no rvm"
}
fi