Skip to content

Instantly share code, notes, and snippets.

@akostadinov
Last active May 14, 2021 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save akostadinov/bc910869ec58ce2e6c884e78cb949b29 to your computer and use it in GitHub Desktop.
Save akostadinov/bc910869ec58ce2e6c884e78cb949b29 to your computer and use it in GitHub Desktop.
bash red hat prompt on Fedora 34
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\002\234\227"
local Checkmark="\001\342\002\234\223"
# such unicodes see https://stackoverflow.com/a/602924/520567
local lightning='\001\xe2\002\x9a\xa1'
local collision='\001\xf0\x9f\002\x92\xa5'
local shellchar='\001\xf0\x9f\002\x90\x9a'
printf "$RED%s${RESET}%s" "$(__git_ps1)" "$(_shell_char)"
}
new_line_ps1() {
local _EXIT_CODE=$? # must be called first
local LIGHT_YELLOW="\001\033[1;93m\002"
local RESET="\001\e[0m\002"
local _ y x _
# https://github.com/dylanaraps/pure-bash-bible#get-the-current-cursor-position
IFS='[;' read -p $'\e[6n' -d R -rs _ y x _
if [[ "$x" != 1 ]]; then
printf "\n${LIGHT_YELLOW}^^ no newline at end of output ^^\n${RESET}"
fi
return $_EXIT_CODE
}
# 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
# from git package
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\]"
# this is 🎩︎ hat in UTF, use `printf 🎩︎| hexdump -C` or `printf 🎩︎| od -t x1`
# local HAT=`printf '\001\xf0\x9f\x8e\xa9\002\xef\xb8\x8e\x0a'`
local HAT="\[🎩︎\]\e\e"
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="\$(new_line_ps1)$LIGHT_BLUE[$RED$HAT$BLUE\u $GREEN\W$LIGHT_BLUE]\$(_generated_ps1) "
PS2='> '
PS4='+ '
}
# Call the fancy prompt function
proml
@akostadinov
Copy link
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
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
Copy link
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.

Update: Added handling of commands without new line at end of output as another source of prompt corruption.

Let me show you a screenshot with my prompt:
prompt

Copy link

ghost commented Jan 25, 2019

Really funny!! And nice redhat logo!

@qinpingli
Copy link

Cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment