-
Notifications
You must be signed in to change notification settings - Fork 1
/
rc_func.sh
executable file
·347 lines (311 loc) · 9.05 KB
/
rc_func.sh
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
#!/usr/bin/env bash
bashdoc en <<'DOC-HERE'
## rc_func
* `o [dir]`
* Open director
* Support cmd,cygwin,Centos(nautilus),Mint(nemo),OS X
* mkd
* mkdir && cd
* colorname
* Echo manual for color and name
* 256colors
* for 256 color
* 16colors
* for 16 color
* server [port:-8000]
* stary a simple http server
* will try python, npm server, php
DOC-HERE
bashdoc zh <<'DOC-HERE'
## rc_func
* `o [dir]`
* 在资源管理器中打开指定目录,如果不指定目录则为当前目录
* 支持 cmd,cygwin,Centos(nautilus),Mint(nemo),OS X
* mkd
* mkdir && cd
* colorname
* 输出 bash 颜色名字和相应数字
* 256colors
* 输出 256 色
* 16colors
* 输出 16 色
* server [port:-8000]
* 在当前目录启动一个 http 服务器
* 会尝试使用 python, npm server, php
DOC-HERE
# `o` 打开目录
function o()
{
if [ $# -eq 0 ]; then
local opath=.
else
local opath="$@"
fi
# Mac
command -v open > /dev/null && open "$opath" && return
# Windows
command -v cygstart > /dev/null && cygstart "$opath" && return
command -v cygpath > /dev/null && start "$(cygpath -d $opath)" && return
# Windows
command -v cmd > /dev/null && cmd /c start "$opath" && return
# Centos
command -v nautilus > /dev/null && nautilus --browser "$opath" && return
# Linux Mint Cinnamon
command -v nemo > /dev/null && nemo "$opath" && return
}
# print the header (the first line of input)
# and then run the specified command on the body (the rest of the input)
# use it in a pipeline, e.g. ps | body grep somepattern
# see http://unix.stackexchange.com/a/11859/47774
body() {
IFS= read -r header
printf '%s\n' "$header"
"$@"
}
# 显示exe文件包含的dll
# objdump a.exe -p|grep DLL\ [^:]\*|sed 's/^\s*//g'
# objdump rex_pcre.dll -p|sed 's/^\s*DLL[^::]*[::]\s*\(.*\)/\t\1/p' -n
incdll()
{
for i in "$@"
do
if [ -f "$i" ]
then
echo $i: included
# objdump $i -p|grep DLL\ [^:]:|sed 's/^.*:/\t/g'
objdump $i -p|sed 's/^\s*DLL[^::]*[::]\s*\(.*\)/\t\1/p' -n
echo
else
echo incdll:\'$i\' file not exists or permission deny
fi
done
}
# Create a new directory and enter it
function mkd()
{
mkdir -p "$@" && cd "$@"
}
# 输出颜色
colorname(){
# https://wiki.archlinux.org/index.php/Bash/Prompt_customization
# http://misc.flogisoft.com/bash/tip_colors_and_formatting
echo -e "
Format:
<Esc>[\e[31mFormatCode\e[0mm | \\\\e[\e[31mFormatCode\e[0mm
\\\\e[\e[31mBold\e[0m;\e[31mUnderlined\e[0mm
\\\\e[\e[31mBold\e[0m;\e[31mForground\e[0m;\e[31mBackground\e[0mm
Set Reset
1 \e[1mBold/Bright\e[0m 21 Reset bold/bright
2 \e[2mDim\e[0m 22 Reset dim
4 \e[4mUnderlined\e[0m 24 Reset underlined
5 \e[5mBlink\e[0m 25 Reset blink
7 \e[7mReverse\e[0m 27 Reset reverse
8 \e[8mHidden\e[0m 28 Reset hidden
0 Reset all attributes
8 Colors
Foreground Background
39 \e[39mDefault\e[0m 49 \e[30m\e[49mDefault\e[0m
30 \e[107m\e[30mBlack\e[0m 40 \e[97m\e[40mBlack\e[0m
31 \e[31mRed\e[0m 41 \e[30m\e[41mRed\e[0m
32 \e[32mGreen\e[0m 42 \e[30m\e[42mGreen\e[0m
33 \e[33mYellow\e[0m 43 \e[30m\e[43mYellow\e[0m
34 \e[34mBlue\e[0m 44 \e[30m\e[44mBlue\e[0m
35 \e[35mMagenta\e[0m 45 \e[30m\e[45mMagenta\e[0m
36 \e[36mCyan\e[0m 46 \e[30m\e[46mCyan\e[0m
37 \e[37mLight gray\e[0m 47 \e[30m\e[47mLight gray\e[0m
90 \e[90mDark gray\e[0m 100 \e[30m\e[100mDark gray\e[0m
91 \e[91mLight red\e[0m 101 \e[30m\e[101mLight red\e[0m
92 \e[92mLight green\e[0m 102 \e[30m\e[102mLight green\e[0m
93 \e[93mLight yellow\e[0m 103 \e[30m\e[103mLight yellow\e[0m
94 \e[94mLight blue\e[0m 104 \e[30m\e[104mLight blue\e[0m
95 \e[95mLight magenta\e[0m 105 \e[30m\e[105mLight magenta\e[0m
96 \e[96mLight cyan\e[0m 106 \e[30m\e[106mLight cyan\e[0m
97 \e[97mWhite\e[0m 107 \e[30m\e[107mWhite\e[0m
256 Colors
Foreground: <Esc>[38;5;\e[31mColorNumber\e[0mm | \\\\e[38;5;\e[31mColorNumber\e[0mm
Background: <Esc>[48;5;\e[31mColorNumber\e[0mm | \\\\e[48;5;\e[31mColorNumber\e[0mm
ColorNumber: 0-255
Current term
Term: $TERM
Colors: `tput colors`
Column: `tput cols`
"
}
16colors(){
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
#Background
for clbg in {40..47} {100..107} 49 ; do
#Foreground
for clfg in {30..37} {90..97} 39 ; do
#Formatting
for attr in 0 1 2 4 5 7 ; do
#Print the result
echo -en "\e[${attr};${clbg};${clfg}m ^[${attr};${clbg};${clfg}m \e[0m"
done
echo #Newline
done
done
}
256colors(){
for fgbg in 38 48 ; do #Foreground/Background
for color in {0..15} ; do
echo -en "\e[${fgbg};5;${color}m `printf "%3s" ${color}` \e[0m"
if [ $((($color + 1) % ${1:-6})) == 0 ] ; then
echo
fi
done
echo
# Better layout
for color in {16..255} ; do
echo -en "\e[${fgbg};5;${color}m `printf "%3s" ${color}` \e[0m"
if [ $((($color - 16 + 1) % ${1:-6})) == 0 ] ; then
echo
fi
done
echo
done
}
# Determine size of a file or total size of a directory
function fs()
{
if du -b /dev/null > /dev/null 2>&1; then
local arg=-sbh
else
local arg=-sh
fi
if [[ -n "$@" ]]; then
du $arg -- "$@"
else
du $arg .[^.]* *
fi
}
# Use Git’s colored diff when available
hash git &>/dev/null
if [ $? -eq 0 ]; then
function diff() {
git diff --no-index --color-words "$@"
}
fi
# Create a data URL from a file
function dataurl()
{
local mimeType=$(file -b --mime-type "$1")
if [[ $mimeType == text/* ]]; then
mimeType="${mimeType};charset=utf-8"
fi
echo "data:${mimeType};base64,$(openssl base64 -in "$1" | tr -d '\n')"
}
# Create a git.io short URL
function gitio()
{
if [ -z "${1}" -o -z "${2}" ]; then
echo "Usage: \`gitio slug url\`"
return 1
fi
curl -i http://git.io/ -F "url=${2}" -F "code=${1}"
}
# Start a PHP server from a directory, optionally specifying the port
# (Requires PHP 5.4.0+.)
function phpserver()
{
local port="${1:-4000}"
local ip=$(ipconfig getifaddr en1)
sleep 1 && open "http://${ip}:${port}/" &
php -S "${ip}:${port}"
}
# Compare original and gzipped file size
function gz()
{
local origsize=$(wc -c < "$1")
local gzipsize=$(gzip -c "$1" | wc -c)
local ratio=$(echo "$gzipsize * 100/ $origsize" | bc -l)
printf "orig: %d bytes\n" "$origsize"
printf "gzip: %d bytes (%2.2f%%)\n" "$gzipsize" "$ratio"
}
# Escape UTF-8 characters into their 3-byte format
function escape()
{
printf "\\\x%s" $(printf "$@" | xxd -p -c1 -u)
# print a newline unless we’re piping the output to another program
if [ -t 1 ]; then
echo # newline
fi
}
# Decode \x{ABCD}-style Unicode escape sequences
function unidecode()
{
perl -e "binmode(STDOUT, ':utf8'); print \"$@\""
# print a newline unless we’re piping the output to another program
if [ -t 1 ]; then
echo # newline
fi
}
# Get a character’s Unicode code point
function codepoint()
{
perl -e "use utf8; print sprintf('U+%04X', ord(\"$@\"))"
# print a newline unless we’re piping the output to another program
if [ -t 1 ]; then
echo # newline
fi
}
# `v` with no arguments opens the current directory in Vim, otherwise opens the
# given location
function v()
{
if [ $# -eq 0 ]; then
vim .
else
vim "$@"
fi
}
# `np` with an optional argument `patch`/`minor`/`major`/`<version>`
# defaults to `patch`
# function np()
# {
# git pull --rebase && \
# npm install && \
# npm test && \
# npm version ${1:=patch} && \
# npm publish && \
# git push origin master && \
# git push origin master --tags
# }
# `tre` is a shorthand for `tree` with hidden files and color enabled, ignoring
# the `.git` directory, listing directories first. The output gets piped into
# `less` with options to preserve color and line numbers, unless the output is
# small enough for one screen.
function tre()
{
tree -aC -I '.git|node_modules|bower_components' --dirsfirst "$@" | less -FRNX
}
# Instant Server for Current Directory
# https://gist.github.com/JeffreyWay/1525217
function server()
{
local port=${1:-8000}
iscmd python && {
(sleep 1 && o "http://localhost:${port}/")&
python -m SimpleHTTPServer ${port}
return
}
iscmd npm && (npm -g ls --depth=0 | grep server@) >/dev/null && {
# Use npm server
(sleep 1 && o "http://localhost:${port}/")&
server ${port}
return
}
iscmd php && {
(sleep 1 && o "http://localhost:${port}/")&
php -S localhost:${port}
return
}
}
function sshtrc(){
sshrc "$*" tmuxrc new -A -s main
}
# vim: set foldmarker={{,}} foldlevel=0 foldmethod=marker: