Skip to content

Instantly share code, notes, and snippets.

@vitorbritto
Created February 11, 2014 00:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vitorbritto/8926835 to your computer and use it in GitHub Desktop.
Save vitorbritto/8926835 to your computer and use it in GitHub Desktop.
dotfiles - bash functions
#!/bin/bash
#
# General
# ---------------------------------------------------
# Create a 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
print_error "'$1' is not a file."
fi
}
# Create a .tar.gz archive, using `zopfli`, `pigz` or `gzip` for compression
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."
}
# Delete all files that match a certain pattern from the current directory
deletefiles() {
local q="${1:-*.DS_Store}"
find . -type f -name "$q" -ls -delete
}
# Create new directories and enter the first one
mkd() {
if [ -n "$*" ]; then
mkdir -p "$@" && cd "$@"
# └─ make parent directories if needed
fi
}
# Remove directories and echo a message
rmd() {
rm -r "$@"
}
# Search History
qh() {
# ┌─ enable colors for pipe
# │ ("--color=auto" enables colors only if
# │ the output is in the terminal)
cat ~/.bash_history | grep --color=always "$*" | less -RX
# display the 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
}
copydir() {
pwd | tr -d "\r\n" | pbcopy
}
copyfile() {
[[ "$#" != 1 ]] && return 1
local file_to_copy=$1
cat $file_to_copy | pbcopy
}
dash() {
open dash://"$@"
}
#
# Development (based on my workflow)
# ---------------------------------------------------
# Projects
# -------------------
# Shortcut to clone Skeleton Repository
skeleton() {
git clone git://github.com/vitorbritto/node-skeleton.git "$@"
cd "$@" && npm i && node skeleton
printf "Done! ✔ \n"
}
# Shortcut to clone Boilerplates Repository
boilerplate() {
git clone git://github.com/vitorbritto/boilerplates.git "$@"
cd "$@" && npm i && node newproject
printf "Done! ✔ \n"
}
# Shortcut to clone Just task Runner Repository
just() {
git clone git://github.com/vitorbritto/just.git "$1"
cd "$1" && npm install && chmod u+x just.js
./just.js npm
./just.js build
./just.js "$2"
}
# Deployment
# -------------------
gone() {
git clone git://github.com/vitorbritto/gone.git "$1"
cd "$1" && npm install && chmod u+x gone.js
./gone.js npm
./gone.js build
./gone.js "$2"
}
# Server
# -------------------
# Apache
apache() {
sudo -v # Ask for the administrator password upfront
apachectl $@ # options: start, stop or restart
}
# Start an HTTP server from a directory, optionally specifying the port
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+.)
phpserver() {
local port="${1:-4000}"
local ip=$(ipconfig getifaddr en1)
sleep 1 && open "http://${ip}:${port}/" &
php -S "${ip}:${port}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment