From 0e830afa2bcd7c353c4456acea4b7fbbb60df263 Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Wed, 1 Jan 2020 20:19:35 -0600 Subject: [PATCH] Changes to bash prompt calculation/display. --- .bashrc | 63 +++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 48 insertions(+), 15 deletions(-) diff --git a/.bashrc b/.bashrc index bc27926..3bbef52 100644 --- a/.bashrc +++ b/.bashrc @@ -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