-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbash_functions
More file actions
263 lines (194 loc) · 7.75 KB
/
Copy pathbash_functions
File metadata and controls
263 lines (194 loc) · 7.75 KB
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
#!/bin/bash
# ----------------------------------------------------------------------
# | File System |
# ----------------------------------------------------------------------
# Create data URI from a file
datauri() {
local mimeType=""
if [ -f "$1" ]; then
mimeType=$(file -b --mime-type "$1")
# └─ do not prepend the filename to the output
if [[ $mimeType == text/* ]]; then
mimeType="$mimeType;charset=utf-8"
fi
printf "data:%s;base64,%s" \
"$mimeType" \
"$(openssl base64 -in "$1" | tr -d "\n")"
else
printf "'%s' is not a file.\n" "$1"
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Delete files that match a certain pattern from the current directory
delete-files() {
local q="${1:-*.DS_Store}"
find . -type f -name "$q" -ls -delete
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Get gzip information (gzipped file size + reduction size)
gz() {
declare -i gzippedSize=0
declare -i originalSize=0
if [ -f "$1" ]; then
if [ -s "$1" ]; then
originalSize=$( wc -c < "$1" )
printf "\n original size: %12s\n" "$(hrfs $originalSize)"
gzippedSize=$( gzip -c "$1" | wc -c )
printf " gzipped size: %12s\n" "$(hrfs $gzippedSize)"
printf " ─────────────────────────────\n"
printf " reduction: %12s [%s%%]\n\n" \
"$( hrfs $(($originalSize-$gzippedSize)) )" \
"$( printf "%s %s" "$originalSize $gzippedSize" | \
awk '{ printf "%.1f", 100 - $2 * 100 / $1 }' | \
sed -e "s/0*$//;s/\.$//" )"
# └─ remove tailing zeros
else
printf "'%s' is empty.\n" "$1"
fi
else
printf "'%s' is not a file.\n" "$1"
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Create new directories and enter the first one
mkd() {
if [ -n "$*" ]; then
mkdir -p "$@" && cd "$@"
# └─ make parent directories if needed
fi
}
# ----------------------------------------------------------------------
# | Miscellaneous |
# ----------------------------------------------------------------------
# Simple Calculator
? () {
local result=""
# ┌─ default (when --mathlib is used) is 20
result="$( printf "scale=10;%s\n" "$*" | bc --mathlib | tr -d "\\\n" )"
# remove the tailing "\" and "\n" ─┘
# (large numbers are printed on multiple lines)
if [[ "$result" == *.* ]]; then
# improve the output for decimal numbers
printf "%s" "$result" |
sed -e "s/^\./0./" `# add "0" for cases like ".5"` \
-e "s/^-\./-0./" `# add "0" for cases like "-.5"`\
-e "s/0*$//;s/\.$//" # remove tailing zeros
else
printf "%s" "$result"
fi
printf "\n"
}
# ----------------------------------------------------------------------
# | Network |
# ----------------------------------------------------------------------
# Start an HTTP server from a directory, optionally specifying the port
server() {
declare -r MAX_NUMBER_OF_TRIES=10
local i=0
local port="${1:-8000}"
# Wait for the server to be available, and once
# it is, open its address in the default browser
while [ $i -lt $MAX_NUMBER_OF_TRIES ]; do
if [ "$(lsof -i -nP | grep "$port" | grep -i "python")" != "" ]; then
o "http://localhost:${port}/"
break;
fi
i=$(( i + 1 ))
sleep 1
done &
# Start server
python -c "
import sys
try:
import SimpleHTTPServer as server
import SocketServer as socketserver
except ImportError:
# In Python 3, the 'SimpleHTTPServer'
# module has been merged into 'http.server'
import http.server as server
import socketserver
handler = server.SimpleHTTPRequestHandler
map = handler.extensions_map
port = int(sys.argv[1])
# Set default Content-Type to 'text/plain'
map[''] = 'text/plain'
# Serve everything as UTF-8 (although not technically
# correct, this doesn't break anything for binary files)
for key, value in map.items():
map[key] = value + '; charset=utf-8'
# Create, but don't automatically bind socket
# (the 'allow_reuse_address' option needs to be set first)
httpd = socketserver.ThreadingTCPServer(('localhost', port), handler, False)
# Prevent 'cannot bind to address' errors on restart
# http://brokenbad.com/address-reuse-in-pythons-socketserver/
httpd.allow_reuse_address = True
# Manually bind socket and start the server
httpd.server_bind()
httpd.server_activate()
print('Serving content on port:', port)
httpd.serve_forever()
" "$port"
}
# ----------------------------------------------------------------------
# | Search |
# ----------------------------------------------------------------------
# Search history
qh() {
# ┌─ enable colors for pipe
# │ ("--color=auto" enables colors only if
# │ the output is in the terminal)
grep --color=always "$*" "$HISTFILE" | less -RX
# display ANSI color escape sequences in raw form ─┘│
# don't clear the screen after quitting less ─┘
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Search for text within the current directory
qt() {
grep -ir --color=always "$*" . | less -RX
# │└─ search all files under each directory, recursively
# └─ ignore case
}
# ----------------------------------------------------------------------
# | Text Processing |
# ----------------------------------------------------------------------
# Human redable file size
# (because `du -h` doesn't cut it for me)
hrfs() {
printf "%s" "$1" |
awk '{
i = 1;
split("B KB MB GB TB PB EB ZB YB WTFB", v);
value = $1;
# confirm that the input is a number
if ( value + .0 == value ) {
while ( value >= 1024 ) {
value/=1024;
i++;
}
if ( value == int(value) ) {
printf "%d %s", value, v[i]
} else {
printf "%.1f %s", value, v[i]
}
}
}' |
sed -e ":l; s/\([0-9]\)\([0-9]\{3\}\)/\1,\2/; t l"
# └─ add commas to the numbers
# (changes "1023.2 KB" to "1,023.2 KB")
}
# ----------------------------------------------------------------------
# | Search web |
# ----------------------------------------------------------------------
# Just hit google and a string to search and you're good to go
# This will open google search in prefered browser
google() {
open "http://www.google.com/search?q= $1"
}
# ----------------------------------------------------------------------
# | Update ruby-build & install determined ruby version |
# ----------------------------------------------------------------------
# Example of usage: "install-ruby 2.2.3"
install-ruby() {
brew update && brew upgrade ruby-build
rbenv install $1 && rbenv global $1 && gem install bundler rails codeland-starter git-up
}