Skip to content

Instantly share code, notes, and snippets.

@erumoico
Created October 16, 2015 19:56
Show Gist options
  • Save erumoico/f5cee7e29af7d413bcdd to your computer and use it in GitHub Desktop.
Save erumoico/f5cee7e29af7d413bcdd to your computer and use it in GitHub Desktop.
Bash prompt with error code and git status.
# ~/.bashrc
set_prompt () {
local Last_Command=$? # Must come first!
local Blue='\[\e[00;34m\]'
local White='\[\e[00;37m\]'
local Red='\[\e[00;31m\]'
local BRed='\[\e[01;31m\]'
local Green='\[\e[00;32m\]'
local Yellow='\[\e[00;33m\]'
local Reset='\[\e[00m\]'
local FancyX='\342\234\227'
local Checkmark='\342\234\223'
local git_commit_id git_status min_color=1
# ERR_CODE
PS1=""
if (( Last_Command != 0 )); then
PS1+="${BRed}${Last_Command}${Reset}:"
fi
if (( ! min_color )); then
if (( Last_Command == 0 )); then
PS1+="${Green}"
else
PS1+="${Red}"
fi
fi
PS1+="\u@\h${Reset}"
if (( min_color )); then
PS1+=":\w\\$ "
else
PS1+=":${Blue}\w${Reset}\\$ "
fi
# GIT
git_commit_id="$(git rev-parse --short HEAD 2>/dev/null)"
if (( $? == 0 )); then
git_status="$(git status)"
if [ "$(grep -E '(not staged)|(Changed but not updated:)' <<< "${git_status}")" ]; then
PS1+="${Red}"
elif [ "$(grep -E '(Changes to be committed:)' <<< "${git_status}")" ]; then
PS1+="${Yellow}"
else
PS1+="${Green}"
fi
# PS1+="($(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'))${Reset} "
PS1+="($(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')|${git_commit_id})${Reset} "
# echo -n "$(__git_ps1)"
fi
}
PROMPT_COMMAND='set_prompt'
# ~/.local/share/mc/bashrc
if [ -f $HOME/.bashrc ]; then
. $HOME/.bashrc
else
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
fi
set_prompt_err_code() {
local ret=$?
(( ret )) && echo -n "${ret}:"
}
set_prompt_git() {
git_commit_id="$(git rev-parse --short HEAD 2>/dev/null)"
if (( $? == 0 )); then
echo -n '('
echo -n "$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')"
echo -n "|${git_commit_id}"
if [ "$(git status | grep -E '(not staged)|(Changed but not updated:)|(Changes to be committed:)')" ]; then
echo -n ':!'
fi
echo -n ") "
# echo -n "$(__git_ps1)"
fi
}
set_prompt_reset() {
PROMPT_COMMAND="$(sed -r 's/^(set_prompt;)?/set_prompt;/' <<< "${PROMPT_COMMAND}")"
}
export PROMPT_COMMAND=""
export PS1="\$(set_prompt_err_code)\u@\h:\w\\$ \$(set_prompt_git)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment