Last active
August 29, 2015 14:15
-
-
Save willmendesneto/b6bc8ab4f868a9ec459c to your computer and use it in GitHub Desktop.
.functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Simple calculator | |
function calc() { | |
local result=""; | |
result="$(printf "scale=10;$*\n" | bc --mathlib | tr -d '\\\n')"; | |
# └─ default (when `--mathlib` is used) is 20 | |
# | |
if [[ "$result" == *.* ]]; then | |
# improve the output for decimal numbers | |
printf "$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 trailing zeros | |
else | |
printf "$result"; | |
fi; | |
printf "\n"; | |
} | |
# Create a new directory and enter it | |
function mkd() { | |
mkdir -p "$@" && cd "$_"; | |
} | |
# Create a .tar.gz archive, using `zopfli`, `pigz` or `gzip` for compression | |
function targz() { | |
local tmpFile="${@%/}.tar"; | |
tar -cvf "${tmpFile}" --exclude=".DS_Store" "${@}" || return 1; | |
size=$( | |
stat -f"%z" "${tmpFile}" 2> /dev/null; # OS X `stat` | |
stat -c"%s" "${tmpFile}" 2> /dev/null # GNU `stat` | |
); | |
local cmd=""; | |
if (( size < 52428800 )) && hash zopfli 2> /dev/null; then | |
# the .tar file is smaller than 50 MB and Zopfli is available; use it | |
cmd="zopfli"; | |
else | |
if hash pigz 2> /dev/null; then | |
cmd="pigz"; | |
else | |
cmd="gzip"; | |
fi; | |
fi; | |
echo "Compressing .tar using \`${cmd}\`…"; | |
"${cmd}" -v "${tmpFile}" || return 1; | |
[ -f "${tmpFile}" ] && rm "${tmpFile}"; | |
echo "${tmpFile}.gz created successfully."; | |
} | |
# Create a new directory and enter it | |
function md() { | |
mkdir -p "$@" && cd "$@" | |
} | |
# find shorthand | |
function f() { | |
find . -name "$1" | |
} | |
# Start an HTTP server from a directory, optionally specifying the port | |
function server() { | |
local port="${1:-8000}" | |
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" | |
} | |
# git log with per-commit cmd-clickable GitHub URLs (iTerm) | |
function gf() { | |
local remote="$(git remote -v | awk '/^origin.*\(push\)$/ {print $2}')" | |
[[ "$remote" ]] || return | |
local user_repo="$(echo "$remote" | perl -pe 's/.*://;s/\.git$//')" | |
git log $* --name-status --color | awk "$(cat <<AWK | |
/^.*commit [0-9a-f]{40}/ {sha=substr(\$2,1,7)} | |
/^[MA]\t/ {printf "%s\thttps://github.com/$user_repo/blob/%s/%s\n", \$1, sha, \$2; next} | |
/.*/ {print \$0} | |
AWK | |
)" | less -F | |
} | |
# Copy w/ progress | |
cp_p () { | |
rsync -WavP --human-readable --progress $1 $2 | |
} | |
# 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 | |
function json() { | |
if [ -p /dev/stdin ]; then | |
# piping, e.g. `echo '{"foo":42}' | json` | |
python -mjson.tool | pygmentize -l javascript | |
else | |
# e.g. `json '{"foo":42}'` | |
python -mjson.tool <<< "$*" | pygmentize -l javascript | |
fi | |
} | |
# take this repo and copy it to somewhere else minus the .git stuff. | |
function gitexport(){ | |
mkdir -p "$1" | |
git archive master | tar -x -C "$1" | |
} | |
# get gzipped size | |
function gz() { | |
echo "orig size (bytes): " | |
cat "$1" | wc -c | |
echo "gzipped size (bytes): " | |
gzip -c "$1" | wc -c | |
} | |
# Extract archives - use: extract <file> | |
# Based on http://dotfiles.org/~pseup/.bashrc | |
function extract() { | |
if [ -f "$1" ] ; then | |
local filename=$(basename "$1") | |
local foldername="${filename%%.*}" | |
local fullpath=`perl -e 'use Cwd "abs_path";print abs_path(shift)' "$1"` | |
local didfolderexist=false | |
if [ -d "$foldername" ]; then | |
didfolderexist=true | |
read -p "$foldername already exists, do you want to overwrite it? (y/n) " -n 1 | |
echo | |
if [[ $REPLY =~ ^[Nn]$ ]]; then | |
return | |
fi | |
fi | |
mkdir -p "$foldername" && cd "$foldername" | |
case $1 in | |
*.tar.bz2) tar xjf "$fullpath" ;; | |
*.tar.gz) tar xzf "$fullpath" ;; | |
*.tar.xz) tar Jxvf "$fullpath" ;; | |
*.tar.Z) tar xzf "$fullpath" ;; | |
*.tar) tar xf "$fullpath" ;; | |
*.taz) tar xzf "$fullpath" ;; | |
*.tb2) tar xjf "$fullpath" ;; | |
*.tbz) tar xjf "$fullpath" ;; | |
*.tbz2) tar xjf "$fullpath" ;; | |
*.tgz) tar xzf "$fullpath" ;; | |
*.txz) tar Jxvf "$fullpath" ;; | |
*.zip) unzip "$fullpath" ;; | |
*) echo "'$1' cannot be extracted via extract()" && cd .. && ! $didfolderexist && rm -r "$foldername" ;; | |
esac | |
else | |
echo "'$1' is not a valid file" | |
fi | |
} | |
# animated gifs from any video | |
# from alex sexton gist.github.com/SlexAxton/4989674 | |
gifify() { | |
if [[ -n "$1" ]]; then | |
if [[ $2 == '--good' ]]; then | |
ffmpeg -i $1 -r 10 -vcodec png out-static-%05d.png | |
time convert -verbose +dither -layers Optimize -resize 900x900\> out-static*.png GIF:- | gifsicle --colors 128 --delay=5 --loop --optimize=3 --multifile - > $1.gif | |
rm out-static*.png | |
else | |
ffmpeg -i $1 -s 600x400 -pix_fmt rgb24 -r 10 -f gif - | gifsicle --optimize=3 --delay=3 > $1.gif | |
fi | |
else | |
echo "proper usage: gifify <input_movie.mov>. You DO need to include extension." | |
fi | |
} | |
# turn that video into webm. | |
# brew reinstall ffmpeg --with-libvpx | |
webmify(){ | |
ffmpeg -i $1 -vcodec libvpx -acodec libvorbis -isync -copyts -aq 80 -threads 3 -qmax 30 -y $2 $1.webm | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment