Skip to content

Instantly share code, notes, and snippets.

@nch3ng
Created May 28, 2018 21:18
Show Gist options
  • Save nch3ng/b84b735e53ac3849916013c613a49551 to your computer and use it in GitHub Desktop.
Save nch3ng/b84b735e53ac3849916013c613a49551 to your computer and use it in GitHub Desktop.
color prompt of the git status and also detect what the project is
ruby_version=`~/.rvm/bin/rvm-prompt v`
node_version=`/usr/local/bin/node -v`
NONE="\[\033[0m\]" # unsets color to term's fg color
# regular colors
K="\[\033[0;30m\]" # black
R="\[\033[0;31m\]" # red
G="\[\033[0;32m\]" # green
Y="\[\033[0;33m\]" # yellow
B="\[\033[0;34m\]" # blue
M="\[\033[0;35m\]" # magenta
C="\[\033[0;36m\]" # cyan
W="\[\033[0;37m\]" # white
# empahsized (bolded) colors
EMK="\[\033[1;30m\]"
EMR="\[\033[1;31m\]"
EMG="\[\033[1;32m\]"
EMY="\[\033[1;33m\]"
EMB="\[\033[1;34m\]"
EMM="\[\033[1;35m\]"
EMC="\[\033[1;36m\]"
EMW="\[\033[1;37m\]"
# background colors
BGK="\[\033[40m\]"
BGR="\[\033[41m\]"
BGG="\[\033[42m\]"
BGY="\[\033[43m\]"
BGB="\[\033[44m\]"
BGM="\[\033[45m\]"
BGC="\[\033[46m\]"
BGW="\[\033[47m\]"
function parse_git_deleted {
[[ $(git status 2> /dev/null | grep deleted:) != "" ]] && echo "-"
}
function parse_git_added {
[[ $(git status 2> /dev/null | grep "Untracked files:") != "" ]] && echo '+'
}
function parse_git_modified {
[[ $(git status 2> /dev/null | grep modified:) != "" ]] && echo "*"
}
function parse_git_dirty {
# [[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] && echo "☠"
echo "$(parse_git_added)$(parse_git_modified)$(parse_git_deleted)"
}
function parse_git_branch {
#echo $(parse_git_dirty)
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/\1 $(parse_git_dirty)/"
}
function git_name {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/\1/"
}
function ruby_ver {
rvm-prompt
#ruby -e 'print RUBY_VERSION'
}
function fn_dev_version {
if [ -e Gemfile ]
then
echo "$R ruby `~/.rvm/bin/rvm-prompt v`"
elif [ -e package.json ]
then
echo "$G node `/usr/local/bin/node -v`"
else
echo ""
fi
}
function color_git {
if [[ $(git_name) != "" ]]
then
if [[ $(git_name) == "master" ]]
then
COLOR=$EMC
else
COLOR=$EMR
fi
if [[ $(parse_git_dirty) == "" ]]
then
echo "($COLOR$(git_name)$NONE)"
else
echo "($COLOR$(git_name) $EMG$(parse_git_added)$EMY$(parse_git_modified)$EMR$(parse_git_deleted)$NONE)"
fi
fi
}
function prompt {
if [[ $? = "0" ]]; then RET=$G; else RET=$R; fi;
SYM=$'\xE2\x98\x85'
BEGIN=$'\xE2\x86\x92'
PS1="$EMR$BEGIN \[\033[01;32m\]${C9_USER}\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\[\033[1;36m\] $NONE$(color_git)$NONE using$(fn_dev_version)\[\033[38;5;15m\] $EMY$SYM $NONE "
}
# PS1='\[\033[01;32m\]${C9_USER}\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$(__git_ps1 " (%s)") using \[\033[38;5;6m\]$ruby_version\[\033[38;5;15m\] $ '
PROMPT_COMMAND=prompt
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
;;
*)
;;
esac
export rvm_silence_path_mismatch_check_flag=1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment