-
Notifications
You must be signed in to change notification settings - Fork 3
/
.functions
126 lines (109 loc) · 3.57 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
#!/usr/bin/env bash
# Create a new directory and enter it
function mkd() {
mkdir -p "$@" && cd "$@" || return;
}
# Change working directory to the top-most Finder window location
function cdf() {
cd "$(osascript -e 'tell app "Finder" to POSIX path of (insertion location as alias)')" || return;
}
# 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 -- "$@" | gsort -h;
else
du $arg .[^.]* * | gsort -h;
fi;
}
# Create a Base64 data URL from a file
function dataurl() {
local mimeType
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')";
}
# Start an HTTP server from a directory, optionally specifying the port
function serve_http() {
local port="${1:-8000}";
sleep 1 && open "http://localhost:${port}/" &
# 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 $'import SimpleHTTPServer;\nmap = SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map;\nmap[""] = "text/plain";\nfor key, value in map.items():\n\tmap[key] = value + ";charset=UTF-8";\nSimpleHTTPServer.test();' "$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";
}
# Syntax-highlight JSON strings or files
# Usage: `json '{"foo":42}'` or `echo '{"foo":42}' | json`
function json() {
if [ -t 0 ]; then # argument
python -mjson.tool <<< "$*" | pygmentize -l javascript;
else # pipe
python -mjson.tool | pygmentize -l javascript;
fi;
}
# Opens given/current directory in vscode
function c() {
if [ $# -eq 0 ]; then
code .;
else
code "$@";
fi;
}
# `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' --dirsfirst "$@" | less -FRNX;
}
# Transfer.sh (https://transfer.sh)
function transfer() {
tmpfile=$( mktemp -t transferXXX )
basefile=$(basename "$1" | sed -e 's/[^a-zA-Z0-9]/-/g')
curl --progress-bar --upload-file "$1" "https://transfer.sh/$basefile" >> "$tmpfile";
cat "$tmpfile";
rm -f "$tmpfile";
}
# Pretty print a message, like echo but cuter
function print() {
printf "\n\033[1m\033[34m%s\033[0m\n\n" "${1}"
}
# Generate a random number inbetween 2 ranges
function rnd_num() {
jot -r 1 "$1" "$2" | cp_sanitized
}
# Find which service is listening on a given port
function listening() {
lsof -n -iTCP:"$1" | grep LISTEN
}
function find_by_mac_addr() {
nmap -sP 192.168.1.0/24 >/dev/null && arp -an | grep $1 | awk '{print $2}' | sed 's/[()]//g'
}
function flatten_dir() {
find "$1" -mindepth 2 -type f -exec mv -i '{}' "$1" ';'
}
function icns_2_png() {
sips -s format png "$1" --out icon.png
}
function zero_out_disk() {
if [ -z "$1" ]; then
print "No valid disk found. Usage:"
print " zero_out_disk disk3"
exit 1
fi
diskutil unmountDisk force "$1"
sudo dd if=/dev/zero of="/dev/$1" bs=1024 count=1024
}