Skip to content

Instantly share code, notes, and snippets.

@scriptum
Created August 31, 2018 06:59
Show Gist options
  • Save scriptum/28df9f3a68a73ea43cf068142b0b1f03 to your computer and use it in GitHub Desktop.
Save scriptum/28df9f3a68a73ea43cf068142b0b1f03 to your computer and use it in GitHub Desktop.
Prompt command
function __prompt_command_generator() {
PS1=""
local TXTRED='\[\e[0;31m\]' # red
local TXTGRN='\[\e[0;32m\]' # green
local TXTYLW='\[\e[0;33m\]' # yellow
local TXTBLU='\[\e[0;34m\]' # blue
local TXTPUR='\[\e[0;35m\]' # purple
local TXTCYN='\[\e[0;36m\]' # cyan
local TXTWHT='\[\e[0;37m\]' # white
local BLDRED='\[\e[1;31m\]' # red - Bold
local BLDGRN='\[\e[1;32m\]' # green
local BLDYLW='\[\e[1;33m\]' # yellow
local BLDBLU='\[\e[1;34m\]' # blue
local BLDPUR='\[\e[1;35m\]' # purple
local BLDCYN='\[\e[1;36m\]' # cyan
local BLDWHT='\[\e[1;37m\]' # white
local TXTRST='\[\e[0m\]' # Text reset
# if [[ $TERM == screen* ]]; then
# PS1='\[\033k\033\\\]'"$BLDPUR$WINDOW$TXTRST|" # для screen
# fi
PS1+='$(__LAST_EXIT="$?";' # This needs to be first
#mark root as red
PS1+='[[ $UID -eq 0 ]] && echo -n "'$TXTRED'" || echo -n "'$TXTCYN'";'
PS1+='echo -n "\u'$TXTRST'";'
#hide host name if we are on a local machine
if [[ -n "$SSH_CLIENT" || -n "$SSH_TTY" ]]; then
PS1+='echo -n "@'$TXTYLW'\h'$TXTRST'";'
fi
#append current path
PS1+='echo -n " '$BLDBLU'\W";'
#append git branch
if hash git 2>/dev/null; then
PS1+='__GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2> /dev/null);'
PS1+='[[ -n "$__GIT_BRANCH" ]] && echo -n "'$TXTBLU'(${__GIT_BRANCH})";'
PS1+='unset __GIT_BRANCH;'
fi
#show exit code
PS1+='[[ $__LAST_EXIT -eq 0 ]] && echo -n "'$BLDGRN'" || echo -n "'$BLDRED'";';
PS1+='unset __LAST_EXIT)'
PS1+="\n\\\$ $TXTRST"
}
# we do not need use this in non-interactive terminals e.g. scp
if [[ $- == *i* ]]; then
# unset if defined system-wide
unset PROMPT_COMMAND
__prompt_command_generator
fi
unset __prompt_command_generator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment