Skip to content

Instantly share code, notes, and snippets.

@Prototyped
Last active January 28, 2024 19:49
Show Gist options
  • Select an option

  • Save Prototyped/af12c2ee74a7b9be74d8142349cd36ab to your computer and use it in GitHub Desktop.

Select an option

Save Prototyped/af12c2ee74a7b9be74d8142349cd36ab to your computer and use it in GitHub Desktop.
Ultimate bash prompt
__normesc="$(tput sgr0)"
__boldesc="$(tput bold)"
__unboldesc='\e[22m'
__dimesc="$(tput dim)"
__italicesc="$(tput sitm)"
__unitalicesc="$(tput ritm)"
__underlineesc="$(tput smul)"
__ununderlineesc="$(tput rmul)"
__blinkesc="$(tput blink)"
__unblinkesc='\e[25m'
__reverseesc="$(tput rev)"
__unreverseesc="$(tput rmso)"
__invisibleesc="$(tput invis)"
__visibleesc='\e[28m'
__blackesc="$(tput setaf 0)"
__redesc="$(tput setaf 1)"
__greenesc="$(tput setaf 2)"
__brownesc="$(tput setaf 3)"
__blueesc="$(tput setaf 4)"
__magentaesc="$(tput setaf 5)"
__cyanesc="$(tput setaf 6)"
__whiteesc="$(tput setaf 7)"
__defaultesc='\e[39m'
__blackbgesc="$(tput setab 0)"
__redbgesc="$(tput setab 1)"
__greenbgesc="$(tput setab 2)"
__brownbgesc="$(tput setab 3)"
__bluebgesc="$(tput setab 4)"
__magentabgesc="$(tput setab 5)"
__cyanbgesc="$(tput setab 6)"
__whitebgesc="$(tput setab 7)"
__defaultbgesc='\e[49m'
ansistyle ()
{
local tostyle="$1"
shift
local styles=($@)
local codes=('\[')
local uncodes=('\]')
for s in "${styles[@]}"
do
case $s in
bold)
codes+=($__boldesc)
uncodes=($__unboldesc "${uncodes[@]}")
;;
dim)
codes+=($__dimesc)
uncodes=($__unboldesc "${uncodes[@]}")
;;
italic)
codes+=($__italicesc)
uncodes=($__unitalicesc "${uncodes[@]}")
;;
underline)
codes+=($__underlineesc)
uncodes=($__ununderlineesc "${uncodes[@]}")
;;
blink)
codes+=($__blinkesc)
uncodes=(__$unblinkesc "${uncodes[@]}")
;;
reverse)
codes+=($__reverseesc)
uncodes=($__unreverseesc "${uncodes[@]}")
;;
invisible)
codes+=($__invisibleesc)
uncodes=($__visibleesc "${uncodes[@]}")
;;
black)
codes+=($__blackesc)
uncodes=($__defaultesc "${uncodes[@]}")
;;
blackbg)
codes+=($__blackbgesc)
uncodes=($__defaultbgesc "${uncodes[@]}")
;;
red)
codes+=($__redesc)
uncodes=($__defaultesc "${uncodes[@]}")
;;
redbg)
codes+=($__redbgesc)
uncodes=($__defaultbgesc "${uncodes[@]}")
;;
green)
codes+=($__greenesc)
uncodes=($__defaultesc "${uncodes[@]}")
;;
greenbg)
codes+=($__greenbgesc)
uncodes=($__defaultbgesc "${uncodes[@]}")
;;
brown)
codes+=($__brownesc)
uncodes=($__defaultesc "${uncodes[@]}")
;;
brownbg)
codes+=($__brownbgesc)
uncodes=($__defaultbgesc "${uncodes[@]}")
;;
blue)
codes+=($__blueesc)
uncodes=($__defaultesc "${uncodes[@]}")
;;
bluebg)
codes+=($__bluebgesc)
uncodes=($__defaultbgesc "${uncodes[@]}")
;;
magenta)
codes+=($__magentaesc)
uncodes=($__defaultesc "${uncodes[@]}")
;;
magentabg)
codes+=($__magentabgesc)
uncodes=($__defaultbgesc "${uncodes[@]}")
;;
cyan)
codes+=($__cyanesc)
uncodes=($__defaultesc "${uncodes[@]}")
;;
cyanbg)
codes+=($__cyanbgesc)
uncodes=($__defaultbgesc "${uncodes[@]}")
;;
white)
codes+=($__whiteesc)
uncodes=($__defaultesc "${uncodes[@]}")
;;
whitebg)
codes+=($__whitebgesc)
uncodes=($__defaultbgesc "${uncodes[@]}")
;;
*)
codes+=('\]<'"$s"'>\[')
uncodes=('\]</'"$s"'>\[' "${uncodes[@]}")
;;
esac
done
codes+=('\]')
uncodes=('\[' "${uncodes[@]}")
local old_ifs="$IFS"
IFS=''
echo -n "${codes[*]}${tostyle}${uncodes[*]}"
IFS="$old_ifs"
}
__git_prompt_git () {
GIT_OPTIONAL_LOCKS=0 command git "$@"
}
__git_dirty_str="$(ansistyle x red)"
__git_clean_str="$(ansistyle o green)"
parse_git_dirty () {
local STATUS
local -a FLAGS
FLAGS=('--porcelain')
STATUS=$(__git_prompt_git status "${FLAGS[@]}" 2> /dev/null | tail -n 1)
echo -n ' '
if [[ -n $STATUS ]]
then
echo -n "$__git_dirty_str"
else
echo -n "$__git_clean_str"
fi
}
git_prompt_info () {
if ! __git_prompt_git rev-parse --git-dir &> /dev/null
then
return 0
fi
local ref
local upstream
ref="$(__git_prompt_git symbolic-ref --short HEAD 2> /dev/null)" || ref="$(__git_prompt_git rev-parse --short HEAD 2> /dev/null)" || return 0
upstream="$(__git_prompt_git rev-parse --abbrev-ref --symbolic-full-name "@{upstream}" 2>/dev/null)" && upstream=" -> ${upstream}"
echo -n ' on '
ansistyle "$ref$upstream" cyan
parse_git_dirty
}
previous_command_exit_code () {
local exit_code=$?
if [[ $exit_code -gt 0 ]]
then
echo -n C:
ansistyle $exit_code red
fi
}
make_prompt () {
local before_git_components=(
'\[\e]0;\h (\u)\a\]'
$'\n'
'# '
"$(ansistyle '\u' cyan bold)"
' @ '
"$(ansistyle '\h' green bold underline)"
' in '
"$(ansistyle '\w' brown bold)"
)
local after_git_components=(
' [\D{%T}] '
)
local after_exit_code_components=(
$"\n"
"$(ansistyle '\$' re\d bold)"
' '
)
local old_ifs="$IFS"
IFS=
PROMPT_COMMAND=(
'__previous_command_exit_code="$(previous_command_exit_code)"'
'__git_prompt_info="$(git_prompt_info)"'
'PS1="'"${before_git_components[*]}"'$__git_prompt_info'"${after_git_components[*]}"'$__previous_command_exit_code'"${after_exit_code_components[*]}"'"'
'unset __previous_command_exit_code __git_prompt_info'
)
IFS="$old_ifs"
}
make_prompt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment