-
-
Save akostadinov/bc910869ec58ce2e6c884e78cb949b29 to your computer and use it in GitHub Desktop.
| # Helper function for our fancy prompt. | |
| function _parse_git_branch { | |
| local branch=$(git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/') | |
| if [[ "$branch" == "" ]]; then | |
| printf "" | |
| elif [[ "$(git status --porcelain)" == "" ]]; then | |
| printf " (%s) " "$branch" | |
| else | |
| # printf " $lightning%s$lightning " "$branch" | |
| printf ' *%s* ' "$branch" | |
| fi | |
| } | |
| function _shell_char { | |
| if [ "$_EXIT_CODE" -eq 0 ]; then | |
| #printf "%s" "$" | |
| printf "$RED$shellchar$RESET" | |
| else | |
| # printf "$RED$FancyX$RESET" | |
| printf "$RED$collision$RESET" | |
| fi | |
| } | |
| function _generated_ps1 { | |
| local _EXIT_CODE=$? # must be called first | |
| # using \001 and \002 instead of \[ \] here | |
| # https://unix.stackexchange.com/a/447520/14907 | |
| local RED="\001\033[0;31m\002" | |
| local RESET="\001\e[0m\002" | |
| # see https://unix.stackexchange.com/a/468983/14907 | |
| local FancyX="\001\342\234\002\227" | |
| local Checkmark="\001\342\234\223\002" | |
| # such unicodes see https://stackoverflow.com/a/602924/520567 | |
| local collision='\001\xf0\x9f\002\x92\xa5' | |
| local lightning='\001\xe2\002\x9a\xa1' | |
| local shellchar='\001\xf0\x9f\002\x90\x9a' | |
| printf "$RED%s${RESET}%s" "$(_parse_git_branch)" "$(_shell_char)" | |
| } | |
| # Fancy prompt function (from nhr) | |
| function proml { | |
| local BLUE="\[\033[0;34m\]" | |
| local LIGHT_BLUE="\[\033[0;36m\]" | |
| local RED="\[\033[0;31m\]" | |
| local LIGHT_RED="\[\033[1;31m\]" | |
| local GREEN="\[\033[0;32m\]" | |
| local LIGHT_GREEN="\[\033[1;32m\]" | |
| local WHITE="\[\033[1;37m\]" | |
| local LIGHT_GRAY="\[\033[0;37m\]" | |
| local BLACK="\[\033[0;30m\]" | |
| local RESET="\[\e[0m\]" | |
| case $TERM in | |
| xterm*) | |
| TITLEBAR='\[\033]0;\u@\h:\w\007\]' | |
| ;; | |
| *) | |
| TITLEBAR="" | |
| ;; | |
| esac | |
| # Standard Prompt | |
| #PS1="$LIGHT_BLUE[$BLUE\u $GREEN\W$LIGHT_BLUE] $RED\$(parse_git_branch)${RESET} $ " | |
| # Red Hat Prompt | |
| PS1="$LIGHT_BLUE[$RED🎩︎$BLUE\u $GREEN\W$LIGHT_BLUE]\$(_generated_ps1) " | |
| PS2='> ' | |
| PS4='+ ' | |
| } | |
| # Call the fancy prompt function | |
| proml |
There's also __git_ps1 but you need to enable it. In my ~/.bashrc I have:
if [ -f /usr/share/git-core/contrib/completion/git-prompt.sh ] ; then
. /usr/share/git-core/contrib/completion/git-prompt.sh
elif [ -f /usr/share/git/git-prompt.sh ] ; then
. /usr/share/git/git-prompt.sh
fiThe latter one is for a non-RH distro.
I have updated the gist to use the shell (see below) unicode character instead of $ as well report non-zero command exit status by changing that character to collision (see below). They take up 2 character spaces but with them I remove needless empty space after git info. These characters can be changed in _shell_char function if desired.
Also I implemented the suggestion of @ekohl to use git-prompt.sh. I'm not sure it is faster than my implementation of _parse_git_branch in revision 2 but it presents rich options.
Update: Added handling of commands without new line at end of output as another source of prompt corruption.
Really funny!! And nice redhat logo!
Cool!

Credits to @thrasr who provided original version. I only made it omit empty spaces before
$in case we are outside a git tree.