Skip to content

Instantly share code, notes, and snippets.

@derickr derickr/.bashrc-section.sh
Last active Mar 23, 2020

Embed
What would you like to do?
Colourful bashrc PHP and Xdebug prompt
function _php_prompt() {
local php_v="`php -n -v 2>&1`"
local php_status="`echo "${php_v}" | head -n 1 | cut -d " " -f 2`"
local php_zts="`echo "${php_v}" | head -n 1 | grep ZTS`"
local php_debug="`echo "${php_v}" | head -n 1 | grep DEBUG`"
local php_64="`php -n -r 'echo PHP_INT_SIZE == 4 ? "32" : "64";' 2>&1`"
local flag1="`set | egrep '^(USE_ZEND_ALLOC=0)'`"
local flag2="`set | egrep '^(ZEND_DONT_UNLOAD_MODULES=1)'`"
flags=" "
if ! [[ "$flag1" == "" ]]; then
flags="${flags}"'\[\e[1;32m\]'
else
flags="${flags}"'\[\e[0;48;5;236;31m\]'
fi
flags="${flags}";
if ! [[ "$flag2" == "" ]]; then
flags="${flags}"'\[\e[1;32m\]'
else
flags="${flags}"'\[\e[0;48;5;236;31m\]'
fi
flags="${flags}";
if ! [[ "$php_debug" == "" ]]; then
flags="${flags}"'\[\e[1;32m\]'
else
flags="${flags}"'\[\e[0;48;5;236;31m\]'
fi
flags="${flags}";
flags="${flags}"'\[\e[1;32m\]'
if [[ "$php_zts" == "" ]]; then
flags="${flags}";
else
flags="${flags}";
fi
flags="${flags}"'\[\e[1;96m\]'
if [[ "$php_64" == "64" ]]; then
flags="${flags}⁶⁴";
else
flags="${flags}₃₂";
fi
if ! [[ "$php_status" =~ php: ]]; then
if ! [[ "$php_status" =~ error ]]; then
tmp='\[\e[39;48;5;236m\] 𝑷 \[\e[1m\]'"$php_status"'\[\e[0;48;5;236m\]'"$flags"'\[\e[0m\]'
local xdebug_status="`php -v | grep Xdebug | sed 's/.*Xdebug v//' | sed 's/,.*//'`"
if ! [[ "$xdebug_status" == "" ]]; then
echo "$tmp"'\[\e[39;48;5;235m\] 𝑿 \[\e[1m\]'"$xdebug_status"'\[\e[0;48;5;236m\] \[\e[0m\]'
else
echo "$tmp"
fi
fi
fi
}
function _git_prompt() {
local git_status="`git status -unormal 2>&1`"
if ! [[ "$git_status" =~ ot\ a\ git\ repo ]]; then
if [[ "$git_status" =~ nothing\ to\ commit ]]; then
local ansi="92"
elif [[ "$git_status" =~ nothing\ added\ to\ commit\ but\ untracked\ files\ present ]]; then
local ansi="93"
else
local ansi="31"
fi
symbol=""
if [[ "$git_status" =~ On\ branch\ ([^[:space:]]+) ]]; then
branch=${BASH_REMATCH[1]}
else
# Detached HEAD. (branch=HEAD is a faster alternative.)
branch="(`git describe --all --contains --abbrev=4 HEAD 2> /dev/null ||
echo HEAD`)"
fi
if [[ "${branch}" == "master" ]]; then
symbol=""
fi
echo '\[\e[0;48;5;239;'"$ansi"'m\] '"${symbol}"' \[\e[39;48;5;239m\]'"$branch"'\[\e[39m\] \[\e[0m\]'
fi
}
function _prompt_command() {
PS1='\[\e[48;5;234m\]\[$(printf " %.0s" $(seq 1 $(tput cols)))\r\]\[\e[0m\]'"`_git_prompt``_php_prompt`\n"'${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
}
PROMPT_COMMAND=_prompt_command
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.