Changes to bash prompt calculation/display.

This commit is contained in:
Jonathan Bernard 2020-01-01 20:19:35 -06:00
parent 716793265d
commit 0e830afa2b

63
.bashrc
View File

@ -1,33 +1,66 @@
prompt_command() {
retval=$?
branch="";
clean="";
# requires the following utilities:
# git
# GNU coreutils (for realpath)
# BSD basename
numcolor="\[\e[0;33m\]"
red="\[\e[0;31m\]"
sepcolor="\[\e[1;30m\]"
yellow="\[\e[0;33m\]"
brightyellow="\[\e[1;33m\]"
brightblue="\[\e[1;34m\]"
default="\[\e[0m\]"
green="\[\e[0;32m\]"
prompt_command() {
local retval=$?
local branch="";
local clean="";
local workingDir="";
local numcolor="\[\e[0;33m\]"
local red="\[\e[0;31m\]"
local sepcolor="\[\e[1;30m\]"
local yellow="\[\e[0;33m\]"
local brightyellow="\[\e[1;33m\]"
local brightblue="\[\e[1;34m\]"
local default="\[\e[0m\]"
local green="\[\e[0;32m\]"
if [[ $retval != 0 ]]; then
numcolor="\[\e[1;41;33m\]"
fi
# If we're in a git repo
if git branch &>/dev/null; then
local repoDir=`git rev-parse --show-toplevel`
branch="$green$(git branch 2>/dev/null | grep \* | cut -d ' ' -f 2-)"
clean=$(git status | grep clean)
if [ -z "$clean" ]; then clean="${red}"; else clean=""; fi
# if [ -z "$clean" ]; then clean="${red}✗"; else clean=""; fi
if [ -z "$clean" ]; then clean="${red}++"; else clean=""; fi
workingDir="$(basename ${repoDir})/"
workingDir+=`realpath "--relative-to=${repoDir}" "$(pwd)"`
else
workingDir=`pwd`
fi
PS1="${debian_chroot:+($debian_chroot)}$numcolor\!$red \u$sepcolor@${brightblue}\h${default}:${branch}${clean} $default\w$green\$${default} "
PS1="$numcolor\!$red \u$sepcolor@${brightblue}\h${default}:${branch}${clean} "
unset clean numcolor branch retval
# Add a linebreak if the branchname + working dir will make the line
# overly long
# echo "Branch and working dir length is $(expr ${#branch} + ${#workingDir})"
# echo "COLUMNS is $COLUMNS. COLUMNS / 3 is $(expr $COLUMNS / 3)"
if (( $(expr ${#branch} + ${#workingDir}) > $(expr $COLUMNS / 3) )); then
PS1="\r\n${PS1}\r\n"
fi
PS1+="$default$workingDir"
# If the working dir is itself too long, put it on it's own line and put
# the prompt below it.
if (( ${#workingDir} > $(expr $COLUMNS / 3) )); then PS1+="\r\n"; fi
PS1+="$green\$${default} "
# PS1="$numcolor\!$red \u$sepcolor@${brightblue}\h${default}:${branch}${clean} $default\w$green\$${default} "
}
PROMPT_COMMAND=prompt_command
eval "$(thefuck --alias fixit)"
set -o vi
source ~/.bash_aliases