Skip to content

Instantly share code, notes, and snippets.

@minacle
Last active August 29, 2015 14:00
Show Gist options
  • Save minacle/11213322 to your computer and use it in GitHub Desktop.
Save minacle/11213322 to your computer and use it in GitHub Desktop.
[ -z "$PS1" ] && return
# Colours
Black='\033[0;30m'
Red='\033[0;31m'
Green='\033[0;32m'
Yellow='\033[0;33m'
Blue='\033[0;34m'
Purple='\033[0;35m'
Cyan='\033[0;36m'
White='\033[0;37m'
Origin='\033[m'
# Bolds
sBlack='\033[1;30m'
sRed='\033[1;31m'
sGreen='\033[1;32m'
sYellow='\033[1;33m'
sBlue='\033[1;34m'
sPurple='\033[1;35m'
sCyan='\033[1;36m'
sWhite='\033[1;37m'
# Background
bBlack='\033[30m'
bRed='\033[31m'
bGreen='\033[32m'
bYellow='\033[33m'
bBlue='\033[34m'
bPurple='\033[35m'
bCyan='\033[36m'
bWhite='\033[37m'
ulimit -S -c 0
case "$OSTYPE" in
darwin*)
CPU_COUNT=$(sysctl hw.ncpu | awk '{print $2}')
;;
linux*)
CPU_COUNT=$(grep -c 'processor' /proc/cpuinfo)
;;
*)
CPU_COUNT=0
esac
LOAD1X=$(( 100*${CPU_COUNT} ))
LOAD2X=$(( 200*${CPU_COUNT} ))
LOAD4X=$(( 400*${CPU_COUNT} ))
function load() {
local LOAD
case "$OSTYPE" in
darwin*)
LOAD=$(sysctl -n vm.loadavg | cut -d ' ' -f2 | tr -d '.')
;;
linux*)
LOAD=$(cut -d ' ' -f1 /proc/loadavg | tr -d '.')
;;
*)
LOAD=0
esac
echo $(( 10#$LOAD ))
}
function usercolour() {
if [ ${UID} -eq 0 ]; then
printf ${Red}
elif [[ ${USER} != $(logname) ]]; then
printf ${Purple}
else
printf ${Cyan}
fi
}
function hostcolour() {
if [ -n "${SSH_CONNECTION}" ]; then
printf ${Purple}
else
printf ${Cyan}
fi
}
function loadcolour() {
local LOAD=$(load)
if [ ${LOAD} -gt ${LOAD2X} ]; then
printf ${Red}
elif [ ${LOAD} -gt ${LOAD1X} ]; then
printf ${Yellow}
else
printf ${Green}
fi
}
function diskcolour() {
local USED=$(command df -P "${PWD}" | tail -1 | awk 'END {print $5}' | tr -d '%')
if [ -s "${PWD}" ]; then
if [ ${USED} -gt 95 ]; then
printf ${Red}
elif [ ${USED} -gt 90 ]; then
printf ${Yellow}
else
printf ${Green}
fi
else
printf ${Cyan}
fi
}
function permcolour() {
if [ -w "${PWD}" ]; then
printf ${Green}
elif [ -r "${PWD}" ]; then
printf ${Yellow}
else
printf ${Red}
fi
}
function jobcolour() {
if [ $(jobs -s | wc -l) -gt 0 ]; then
printf ${sRed}
elif [ $(jobs -r | wc -l) -gt 0 ]; then
printf ${sPurple}
else
printf ${sWhite}
fi
}
function exitcolour() {
if [ $RETVAR -ne 0 ]; then
printf ${Yellow}
else
printf ${Green}
fi
}
function getgitdir() {
git rev-parse --git-dir 2>/dev/null
}
function getgitbranch() {
git rev-parse --abbrev-ref HEAD 2>/dev/null
}
function getgitlocal() {
git rev-parse @ 2>/dev/null
}
function getgitremote() {
git rev-parse @{u} 2>/dev/null
}
function getgitbase() {
git merge-base @ @{u} 2>/dev/null
}
function getgitps1string() {
if [ ! -z $(getgitdir) ]; then
local BRANCH=$(getgitbranch)
local REPOSTAT=0
local LOCAL=$(getgitlocal)
local REMOTE=$(getgitremote)
local BASE=$(getgitbase)
local FILES=`git status --porcelain 2>/dev/null`
local STAGEDADD=0
local STAGEDMOD=0
local STAGEDDEL=0
local STAGED=''
local UNTRACKEDADD=0
local UNTRACKEDMOD=0
local UNTRACKEDDEL=0
local UNTRACKED=''
local IFS=$'\n'
for line in $FILES; do
if [[ $line =~ ^A\ .* ]]; then
((STAGEDADD+=1))
elif [[ $line =~ ^M\ .* ]]; then
((STAGEDMOD+=1))
elif [[ $line =~ ^D\ .* ]]; then
((STAGEDDEL+=1))
elif [[ $line =~ ^\?\?.* ]]; then
((UNTRACKEDADD+=1))
elif [[ $line =~ ^\ M.* ]]; then
((UNTRACKEDMOD+=1))
elif [[ $line =~ ^\ D.* ]]; then
((UNTRACKEDDEL+=1))
fi
done
if (( STAGEDADD > 0 || STAGEDMOD > 0 || STAGEDDEL > 0 || UNTRACKEDADD > 0 || UNTRACKEDMOD > 0 || UNTRACKEDDEL > 0 )); then
REPOSTAT=1
elif [ ! -z $REMOTE ]; then
if [ $LOCAL = $REMOTE ]; then
REPOSTAT=0
elif [ $LOCAL = $BASE ]; then
REPOSTAT=3
elif [ $REMOTE = $BASE ]; then
REPOSTAT=2
fi
else
REPOSTAT=0
fi
if [ $BRANCH = HEAD ]; then
BRANCH=master
fi
STAGED="+$STAGEDADD *$STAGEDMOD -$STAGEDDEL"
UNTRACKED="+$UNTRACKEDADD *$UNTRACKEDMOD -$UNTRACKEDDEL"
printf "${sYellow}:"
case $REPOSTAT in
0) printf "${sGreen}" ;;
1) printf "${sRed}" ;;
2) printf "${sCyan}" ;;
3) printf "${sPurple}" ;;
esac
printf "${BRANCH}"
if (( STAGEDADD > 0 || STAGEDMOD > 0 || STAGEDDEL > 0 )); then
printf " ${Green}${STAGED}"
STAGED=""
fi
if (( UNTRACKEDADD > 0 || UNTRACKEDMOD > 0 || UNTRACKEDDEL > 0 )); then
printf " ${Red}${UNTRACKED}"
UNTRACKED=""
fi
printf "${Origin}"
fi
}
PROMPT_COMMAND='RETVAR=$?'
case ${TERM} in
linux|*term*)
PS1="\[\$(loadcolour)\]\t\[${Origin}\] \[$(usercolour)\]\u\[\$(exitcolour)\]@\[$(hostcolour)\]\h\[${Origin}\] \[\$(permcolour)\]:\[\$(diskcolour)\]\W\[${Origin}\]\[\$(getgitps1string)\] \[\$(jobcolour)\]\\$\[${Origin}\] "
PS2='> '
;;
esac
if [ $LOCALBIN ]
then
if [ -d "$HOME/bin" ]
then
PATH="$HOME/bin:$PATH"
fi
for BIN in `find $HOME -maxdepth 1 -type d -name ".*" -exec find {} -type d -name "bin" \;`
do
PATH="$BIN:$PATH"
done
unset BIN
fi
alias ls='ls -Gh'
alias l='ls -F'
alias ll='ls -lF'
alias la='ls -Al'
alias lm='ls -al | more'
alias ..='cd ..'
alias cp='cp -i'
alias mv='mv -i'
alias du='du -kh'
alias df='df -kTh'
export EDITOR=vim
export HISTCONTROL=ignoreboth
# Local Variables:
# mode:shell-script
# sh-shell:bash
# End:
# vim: set ts=4 sw=4:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment