Skip to content

Instantly share code, notes, and snippets.

@0x00dec0de
Created August 22, 2015 10:35
Show Gist options
  • Save 0x00dec0de/0f4801502276142778a3 to your computer and use it in GitHub Desktop.
Save 0x00dec0de/0f4801502276142778a3 to your computer and use it in GitHub Desktop.
PS1.sh
## File used for defining $PS1
##-ANSI-COLOR-CODES-##
# Reset
Color_Off='\e[0m' # Text Reset
# Regular Colors
Black='\e[0;30m' # Black
Red='\e[0;31m' # Red
Green='\e[0;32m' # Green
Yellow='\e[0;33m' # Yellow
Blue='\e[0;34m' # Blue
Purple='\e[0;35m' # Purple
Cyan='\e[0;36m' # Cyan
White='\e[0;37m' # White
Gray='\e[1;30m'
# Bold
BBlack='\e[1;30m' # Black
BRed='\e[1;31m' # Red
BGreen='\e[1;32m' # Green
BYellow='\e[1;33m' # Yellow
BBlue='\e[1;34m' # Blue
BPurple='\e[1;35m' # Purple
BCyan='\e[1;36m' # Cyan
BWhite='\e[1;37m' # White
# Underline
UBlack='\e[4;30m' # Black
URed='\e[4;31m' # Red
UGreen='\e[4;32m' # Green
UYellow='\e[4;33m' # Yellow
UBlue='\e[4;34m' # Blue
UPurple='\e[4;35m' # Purple
UCyan='\e[4;36m' # Cyan
UWhite='\e[4;37m' # White
# Background
On_Black='\e[40m' # Black
On_Red='\e[41m' # Red
On_Green='\e[42m' # Green
On_Yellow='\e[43m' # Yellow
On_Blue='\e[44m' # Blue
On_Purple='\e[45m' # Purple
On_Cyan='\e[46m' # Cyan
On_White='\e[47m' # White
# High Intensity
IBlack='\e[0;90m' # Black
IRed='\e[0;91m' # Red
IGreen='\e[0;92m' # Green
IYellow='\e[0;93m' # Yellow
IBlue='\e[0;94m' # Blue
IPurple='\e[0;95m' # Purple
ICyan='\e[0;96m' # Cyan
IWhite='\e[0;97m' # White
# Bold High Intensity
BIBlack='\e[1;90m' # Black
BIRed='\e[1;91m' # Red
BIGreen='\e[1;92m' # Green
BIYellow='\e[1;93m' # Yellow
BIBlue='\e[1;94m' # Blue
BIPurple='\e[1;95m' # Purple
BICyan='\e[1;96m' # Cyan
BIWhite='\e[1;97m' # White
# High Intensity backgrounds
On_IBlack='\e[0;100m' # Black
On_IRed='\e[0;101m' # Red
On_IGreen='\e[0;102m' # Green
On_IYellow='\e[0;103m' # Yellow
On_IBlue='\e[0;104m' # Blue
On_IPurple='\e[0;105m' # Purple
On_ICyan='\e[0;106m' # Cyan
On_IWhite='\e[0;107m' # White
# Status of last command (for prompt)
function __stat() {
if [ $? -eq 0 ]; then
echo -en "$Green[o]$Color_Off"
else
echo -en "$Red[x]$Color_Off"
fi
}
function __git_prompt() {
local git_status="`git status -unormal 2>&1`"
if ! [[ "$git_status" =~ Not\ a\ git\ repo ]]; then
if [[ "$git_status" =~ nothing\ to\ commit ]]; then
local Color_On=$Green
elif [[ "$git_status" =~ nothing\ added\ to\ commit\ but\ untracked\ files\ present ]]; then
local Color_On=$Purple
else
local Color_On=$Red
fi
if [[ "$git_status" =~ On\ branch\ ([^[:space:]]+) ]]; then
branch=${BASH_REMATCH[1]}
else
# Detached HEAD. (branch=HEAD is a faster alternative.)
branch="(`git describe --all --contains --abbrev=4 HEAD 2> /dev/null || echo HEAD`)"
fi
echo -ne "$Color_On[$branch]$Color_Off "
fi
}
function __color() {
PS1+="\[\033[38;5;$1m\]"
}
# set up command prompt
function __prompt_command()
{
# capture the exit status of the last command
EXIT="$?"
PS1="\n"
# save history
history -a
#local MemFree="$(($(sed -nu "s/MemFree:[\t ]\+\([0-9]\+\) kB/\1/p" /proc/meminfo)/1024))"
local MemFree=$(free -m | grep cache:|awk '{print $4}')
local MemTotal="$(($(sed -nu "s/MemTotal:[\t ]\+\([0-9]\+\) kB/\1/Ip" /proc/meminfo)/1024))"
local load="$(sed -nu "s/^\([0-9.]\+\) \([0-9.]\+\) \([0-9.]\+\).*/\1 \2 \3/p" /proc/loadavg)"
local uptime="$(( $(sed 's/^\([0-9]*\).*/\1/' /proc/uptime) / (60*60*24) ))"
local date="\D{%Y/%m/%d %T}"
__color 246
PS1+="["
__color 84
PS1+="$MemFree"
__color 237
PS1+="/"
__color 22
PS1+="$MemTotal"
__color 237
PS1+="Mb"
__color 246
PS1+="] ["
__color 66
PS1+="$load"
__color 246
PS1+="] ["
__color 103
PS1+="$uptime days"
__color 246
PS1+="] ["
__color 106
PS1+="$date"
__color 246
PS1+="]"
if [ $EXIT -eq 0 ]; then
__color 29
PS1+=" [o]"
else
__color 196
PS1+=" [x]"
fi
local git_status="`git status -unormal 2>&1`"
if ! [[ "$git_status" =~ Not\ a\ git\ repo ]]; then
if [[ "$git_status" =~ nothing\ to\ commit ]]; then
local Color_On=$Green
elif [[ "$git_status" =~ nothing\ added\ to\ commit\ but\ untracked\ files\ present ]]; then
local Color_On=$Purple
else
local Color_On=$Red
fi
if [[ "$git_status" =~ On\ branch\ ([^[:space:]]+) ]]; then
branch=${BASH_REMATCH[1]}
else
# Detached HEAD. (branch=HEAD is a faster alternative.)
branch="(`git describe --all --contains --abbrev=4 HEAD 2> /dev/null || echo HEAD`)"
fi
PS1+=" $Color_On[git $branch]$Color_Off "
fi
__color 246
PS1+="\n["
__color 215
PS1+="\u"
__color 246
PS1+="@"
__color 240
PS1+="\h"
__color 246
PS1+=":"
__color 203
PS1+="\w"
__color 246
PS1+=']\$ '
PS1+="\[$Color_Off\]"
return
}
PROMPT_COMMAND=__prompt_command
export PATH="$PATH:/home/vim/.composer/vendor/bin"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment