207 lines
5.7 KiB
Plaintext
207 lines
5.7 KiB
Plaintext
get_cols () {
|
||
# Usage: get_cols [file_name] [column_num]
|
||
column_list=""
|
||
for column_num in $@; do
|
||
[ -r $column_num ] && continue
|
||
|
||
[ $column_num -lt '0' ] && column_num="(NF + 1 $column_num )" #Negative indices like python's array[-1]
|
||
|
||
[ -z $column_list ] && column_num="\$$column_num" || column_num=",\$$column_num" # Place commas appropriately.
|
||
|
||
column_list="$column_list$column_num"
|
||
done
|
||
unset column_num
|
||
|
||
# Is the first parameter a file?
|
||
if [ -r $1 ]; then
|
||
awk "{print $column_list}" $1
|
||
else
|
||
awk "{print $column_list}"
|
||
fi
|
||
unset column_list
|
||
}
|
||
|
||
note() {
|
||
if [ $# -eq 0 ]; then
|
||
(ym && exec $EDITOR $HOME/notes/$(git rev-parse --abbrev-ref HEAD)) ;
|
||
else
|
||
$EDITOR $HOME/notes/"$*" ;
|
||
fi
|
||
}
|
||
|
||
list_notes() {
|
||
ls -c $HOME/notes/ | grep "$*"
|
||
}
|
||
|
||
function get_shas_that_touched() {
|
||
git log --oneline --reverse "$@" | get_col 1
|
||
}
|
||
|
||
function echo_hash_if_exp_found_in_filename_diff() {
|
||
[ ! -z "$(git diff $1~1 $1 -- $2 | cat | grep $3)" ] && echo $1
|
||
}
|
||
|
||
function get_shas_that_touched_with_grep() {
|
||
get_shas_that_touched $1 | xargs -I the_hash sh -c 'echo_hash_if_exp_found_in_filename_diff the_hash $1 $2'
|
||
}
|
||
|
||
function show_interdiffs_matching_grep() {
|
||
get_shas_that_touched_with_grep $1 $2 | xargs -I hash sh -c 'git diff hash~1 hash -- $1'
|
||
}
|
||
|
||
function find_all_ssh_agent_sockets() {
|
||
find /tmp -type s -name agent.\* 2> /dev/null | grep '/tmp/ssh-.*/agent.*'
|
||
}
|
||
|
||
function set_ssh_agent_socket() {
|
||
export SSH_AUTH_SOCK=$(find_all_ssh_agent_sockets | tail -n 1 | awk -F: '{print $1}')
|
||
}
|
||
|
||
# Create a new directory and enter it
|
||
function mkd() {
|
||
mkdir -p "$@" && cd "$@"
|
||
}
|
||
|
||
# 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')"
|
||
}
|
||
|
||
# Start an HTTP server from a directory, optionally specifying the port
|
||
function server() {
|
||
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"
|
||
}
|
||
|
||
# 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"
|
||
}
|
||
|
||
# Test if HTTP compression (RFC 2616 + SDCH) is enabled for a given URL.
|
||
# Send a fake UA string for sites that sniff it instead of using the Accept-Encoding header. (Looking at you, ajax.googleapis.com!)
|
||
function httpcompression() {
|
||
encoding="$(curl -LIs -H 'User-Agent: Mozilla/5 Gecko' -H 'Accept-Encoding: gzip,deflate,compress,sdch' "$1" | grep '^Content-Encoding:')" && echo "$1 is encoded using ${encoding#* }" || echo "$1 is not using any encoding"
|
||
}
|
||
|
||
# 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
|
||
}
|
||
|
||
# All the dig info
|
||
function digga() {
|
||
dig +nocmd "$1" any +multiline +noall +answer
|
||
}
|
||
|
||
# Escape UTF-8 characters into their 3-byte format
|
||
function escape() {
|
||
printf "\\\x%s" $(printf "$@" | xxd -p -c1 -u)
|
||
echo # newline
|
||
}
|
||
|
||
# Decode \x{ABCD}-style Unicode escape sequences
|
||
function unidecode() {
|
||
perl -e "binmode(STDOUT, ':utf8'); print \"$@\""
|
||
echo # newline
|
||
}
|
||
|
||
# Get a character’s Unicode code point
|
||
function codepoint() {
|
||
perl -e "use utf8; print sprintf('U+%04X', ord(\"$@\"))"
|
||
echo # newline
|
||
}
|
||
|
||
# Add note to Notes.app (OS X 10.8)
|
||
# Usage: `note 'foo'` or `echo 'foo' | note`
|
||
function note() {
|
||
local text
|
||
if [ -t 0 ]; then # argument
|
||
text="$1"
|
||
else # pipe
|
||
text=$(cat)
|
||
fi
|
||
body=$(echo "$text" | sed -E 's|$|<br>|g')
|
||
osascript >/dev/null <<EOF
|
||
tell application "Notes"
|
||
tell account "iCloud"
|
||
tell folder "Notes"
|
||
make new note with properties {name:"$text", body:"$body"}
|
||
end tell
|
||
end tell
|
||
end tell
|
||
EOF
|
||
}
|
||
|
||
# Add reminder to Reminders.app (OS X 10.8)
|
||
# Usage: `remind 'foo'` or `echo 'foo' | remind`
|
||
function remind() {
|
||
local text
|
||
if [ -t 0 ]; then
|
||
text="$1" # argument
|
||
else
|
||
text=$(cat) # pipe
|
||
fi
|
||
osascript >/dev/null <<EOF
|
||
tell application "Reminders"
|
||
tell the default list
|
||
make new reminder with properties {name:"$text"}
|
||
end tell
|
||
end tell
|
||
EOF
|
||
}
|
||
|
||
# Manually remove a downloaded app or file from the quarantine
|
||
function unquarantine() {
|
||
for attribute in com.apple.metadata:kMDItemDownloadedDate com.apple.metadata:kMDItemWhereFroms com.apple.quarantine; do
|
||
xattr -r -d "$attribute" "$@"
|
||
done
|
||
}
|