Skip to content

Instantly share code, notes, and snippets.

@mdeous
Last active August 29, 2015 13:57
Show Gist options
  • Save mdeous/9650009 to your computer and use it in GitHub Desktop.
Save mdeous/9650009 to your computer and use it in GitHub Desktop.
(21:09:37)[mdeous@DELL-P159BIS:~/Dotfiles(master)↑+2⚡]0↩
$ CURSEUR ICI
# RED="\033[0;31m"
# YELLOW="\033[0;33m"
# GREEN="\033[0;32m"
# BLUE="\033[0;34m"
# LIGHT_RED="\033[1;31m"
# LIGHT_GREEN="\033[1;32m"
# WHITE="\033[1;37m"
# LIGHT_GRAY="\033[0;37m"
# COLOR_NONE="\033[0;0m"
RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
BLUE="\[\033[0;34m\]"
LIGHT_RED="\[\033[1;31m\]"
LIGHT_GREEN="\[\033[1;32m\]"
WHITE="\[\033[1;37m\]"
LIGHT_GRAY="\[\033[0;37m\]"
COLOR_NONE="\[\033[0;0m\]"
GIT_BRANCH_RE="^# On branch ([^${IFS}]*)"
GIT_REMOTE_RE="# Your branch is (.*) '"
GIT_GAP_RE="# Your branch is .* ([0-9]+) commits?\."
GIT_DIVERGE_RE="# Your branch and (.*) have diverged"
function gen_prompt {
if [ ! -d "$(pwd)/.git" ]; then
git_infos=""
else
git_status="$(git status 2> /dev/null)"
# Handle uncommited changes
if [[ ! ${git_status} =~ "working directory clean" ]]; then
state="${RED}⚡"
else
state=""
fi
# Handle commits gap with remote
if [[ ${git_status} =~ ${GIT_REMOTE_RE} ]]; then
if [[ ${BASH_REMATCH[1]} == "ahead of" ]]; then
remote="${YELLOW}↑"
remote_sign="+"
else
remote="${YELLOW}↓"
remote_sign="-"
fi
if [[ ${git_status} =~ ${GIT_GAP_RE} ]]; then
remote="${remote}${remote_sign}${BASH_REMATCH[1]}"
fi
else
remote=""
fi
# Handle diverged branches
if [[ ${git_status} =~ ${GIT_DIVERGE_RE} ]]; then
remote="${YELLOW}↕"
fi
# Generate git informations summary
if [[ ${git_status} =~ ${GIT_BRANCH_RE} ]]; then
branch=${BASH_REMATCH[1]}
git_infos="(${branch})${remote}${state}"
else
git_infos=""
fi
fi
# Handle last command's return value
if [ $? -eq 0 ]; then
last_ret="${GREEN}$?↩${COLOR_NONE}"
else
last_ret="${RED}$?↩${COLOR_NONE}"
fi
date_prefix="(${YELLOW}$(date +'%H:%M:%S')${COLOR_NONE})"
current_path=`pwd | sed "s|${HOME}|~|"`
main_part="${YELLOW}$(whoami)@$(hostname -s)${LIGHT_GRAY}:${BLUE}${current_path}"
PS1L="${date_prefix}${LIGHT_GRAY}[${main_part}${GREEN}${git_infos}${LIGHT_GRAY}]${COLOR_NONE}"
# PS1L_NOCOLOR=$(echo "${PS1L}" | sed -r 's|\\033\[[0-9];[0-9]+m||g')
PS1R="${last_ret} \n\$ "
# echo $PS1L_NOCOLOR
PS1=$(printf "%s%$((${COLUMNS}-${#PS1L}))s" "${PS1L}" "${PS1R}")
}
PROMPT_COMMAND="gen_prompt"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment