Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
bash red hat prompt on Fedora 28
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" "$(__git_ps1)" "$(_shell_char)"
}
# Fancy prompt function (from nhr)
function proml {
GIT_PS1_SHOWUPSTREAM="auto"
GIT_PS1_SHOWUNTRACKEDFILES=1
GIT_PS1_SHOWDIRTYSTATE=1
# GIT_PS1_STATESEPARATOR=""
GIT_PS1_HIDE_IF_PWD_IGNORED=1
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
fi
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
@akostadinov

This comment has been minimized.

Copy link
Owner Author

akostadinov commented Oct 11, 2018

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

@ekohl

This comment has been minimized.

Copy link

ekohl commented Oct 12, 2018

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
fi

The latter one is for a non-RH distro.

@akostadinov

This comment has been minimized.

Copy link
Owner Author

akostadinov commented Jan 25, 2019

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.

Let me show you a screenshot with my prompt:
prompt

@wjiangjay

This comment has been minimized.

Copy link

wjiangjay commented Jan 25, 2019

Really funny!! And nice redhat logo!

@qinpingli

This comment has been minimized.

Copy link

qinpingli commented Jan 29, 2019

Cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.