Last active
June 13, 2016 17:29
-
-
Save 0x9900/0bf4f3ffb36679b49396 to your computer and use it in GitHub Desktop.
.bash_profile misc 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
# | |
# Nice prompt with nice colors for git | |
# Colors: master (red), dev (grey), other branches (green) | |
# | |
__set_prompt() { | |
local _RESET="\033[0m\017" # reset color | |
local _MASTER="\033[31m" # red | |
local _DEV="\033[90m" # grey | |
local _BRANCH="\033[32m" # green | |
local ref="$(git symbolic-ref --short HEAD 2>/dev/null)" | |
PS1="\u[\!]" | |
if [[ ! -z $ref ]]; then | |
case $ref in | |
"master") PS1+="\[${_MASTER}\](${ref})\[${_RESET}\]" ;; | |
"dev") PS1+="\[${_DEV}\](${ref})\[${_RESET}\]" ;; | |
*) PS1+="\[${_BRANCH}\](${ref})\[${_RESET}\]" ;; | |
esac | |
fi | |
PS1+="\\$ " | |
} | |
PROMPT_COMMAND='__set_prompt' | |
edit() { | |
/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -nq "$@" || mg "$@" | |
} | |
wpaste() { | |
local url=$(curl -sF 'sprunge=<-' http://sprunge.us) | |
[[ $(uname -s) == 'Darwin' ]] && echo -n $url | pbcopy | |
echo $url | |
} | |
diff-html() { | |
local filediff=$(mktemp ~/tmp/$(logname)-diff.XXXXXX) || exit 1 | |
# Styles I like "monokai, paraiso-dark, native, murphy" | |
git diff $1 > ${filediff} | |
if [ -s ${filediff} ]; then | |
pygmentize -l diff -f html -O full,style="paraiso-dark" -o ${filediff}.html ${filediff} | |
rm -f ${filediff} | |
open ${filediff}.html | |
else | |
echo "Diff file \"${filediff}\" empty." | |
fi | |
} | |
colours() { | |
for i in {0..255} ; do | |
printf "\x1b[38;5;${i}m%-10s" "colour${i}" | |
if [[ $(( i % 7 )) == 0 ]]; then | |
echo | |
fi | |
done | |
echo | |
} | |
genselfsigned () { | |
if [ -z "$1" ]; then | |
echo "Usage: genselfsigned [name] [key_len] [expire]" >&2 | |
else | |
keylen=${2:-2048} | |
expire=${3:-3650} # roughly 10 years | |
openssl genrsa -out $1.key 2048 | |
openssl req -new -key $1.key -out $1.csr | |
openssl x509 -req -days 365 -in $1.csr -signkey $1.key -out $1.crt | |
fi | |
} | |
genpass () | |
{ | |
local pass="" | |
local len=${1:-16} | |
local chars="A-Za-z0-9?\!+$#" | |
if [[ $((len % 4)) == 0 ]]; then | |
pass=$(LC_ALL=C tr -dc $chars < /dev/urandom | hexdump -n $len -e '"%.4s-"') | |
pass=${pass::$((len + (len / 4 - 1)))} | |
echo $pass | |
[[ $(uname -s) == 'Darwin' ]] && echo -n $pass | pbcopy | |
else | |
echo "Error: The password length must be a multiple of '4'" 1>&2 | |
fi | |
} | |
reverseip() { | |
local ip=${1} | |
local reverse=$(IFS=.; set -- $ip; echo "${4}.${3}.${2}.${1}") | |
echo $reverse | |
} | |
mailfrom_ip() { | |
local file=${1} | |
sed -n '/^Received: from.*$/{N;N;s/^Received.*\[\(.*\)\].*by dos\.kas.*/\1/p;}' $file | sort -nu | |
} | |
alias work="tmux attach -t Work || tmux new-session -s Work -d '/Applications/Emacs.app/Contents/MacOS/Emacs -nw' \; split-window -h \; split-window -d \; select-pane -t +1 \; attach" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment