-
Notifications
You must be signed in to change notification settings - Fork 1
/
.functions
executable file
·53 lines (48 loc) · 1.91 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
#!/usr/bin/env bash
# create a directory and enters
# usage: mkcd <directory>
mkcd() {
mkdir -p -- "$1" && cd -P -- "$1"
}
# extracts archived files
# usage: extract <file>
# credit: http://nparikh.org/notes/zshrc.txt
extract() {
if [ -f "$1" ]; then
case "$1" in
*.tar.bz2) tar -jxvf "$1" ;;
*.tar.gz) tar -zxvf "$1" ;;
*.bz2) bunzip2 "$1" ;;
*.gz) gunzip "$1" ;;
*.tar) tar -xvf "$1" ;;
*.tbz2) tar -jxvf "$1" ;;
*.tgz) tar -zxvf "$1" ;;
*.zip) unzip "$1" ;;
*.ZIP) unzip "$1" ;;
*.pax) cat "$1" | pax -r ;;
*.pax.Z) uncompress "$1" --stdout | pax -r ;;
*.Z) uncompress "$1" ;;
*) echo "'$1' cannot be extracted/mounted via extract()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
# generate a random password
# usage: genpwd OR genpwd <length>
genpwd() {
[[ -z "$1" ]] && pwdLen=32 || pwdLen="$1"
cat /dev/urandom | tr -dc 'a-zA-Z0-9#+.-_!$%&/()[]{}=' | fold -w "$pwdLen" | head -n 1
}
# start an HTTP server from current directory
# usage: server OR server <port>
server() {
local port="${1:-8080}"
# set the default Content-Type to `text/plain` instead of `application/octet-stream`
# And serve everything as UTF-8 (although not technically correct, this doesn’t break anything for binary files)
python -c $"from http.server import test, SimpleHTTPRequestHandler as RH; RH.extensions_map={k:v+';charset=UTF-8' for k,v in RH.extensions_map.items()}; test(RH);" "$port" "--bind 127.0.0.1"
# open "http://127.0.0.1:${port}/"
}
if [ -f ~/.functions.local ]; then
. ~/.functions.local
fi