Updates to .bashrc for variable-length prompt, tmux conf.

This commit is contained in:
Jonathan Bernard 2022-11-08 09:46:04 -06:00
parent 55103b47cd
commit 77372ea780
2 changed files with 39 additions and 29 deletions

63
.bashrc
View File

@ -7,7 +7,7 @@ prompt_command() {
local retval=$?
local branch="";
local clean="";
local workingDir="";
local cols=$COLUMNS
local numcolor="\[\e[0;33m\]"
local red="\[\e[0;31m\]"
@ -22,40 +22,47 @@ prompt_command() {
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
fi
workingDir="$(basename ${repoDir})/"
workingDir+=`realpath "--relative-to=${repoDir}" "$(pwd)"`
userHost="${debian_chroot:+($debian_chroot)}$numcolor\!$red \u$sepcolor@${brightblue}\h${default}:${branch}${clean}"
userHostExp="${debian_chroot:+($debian_chroot)}\! \u@\h:${branch}${clean}"
userHostExp=${userHostExp@P}
wrkDir="$default\w$green"
wrkDirExp="\w"
wrkDirExp=${wrkDirExp@P}
userPrompt="\$${default}"
if (( ${#userHostExp} + ${#wrkDirExp} < $cols - 20 )); then
# We can fit everything on one line
# echo "Oneline: $((${#userHostExp} + ${#wrkDirExp})) < $(($cols - 20))"
PS1="${userHost} ${wrkDir}${userPrompt} "
elif (( ${#userHostExp} + ${#wrkDirExp} < $cols + 6 )); then
# We just need to put the prompt on it's own line
# echo "Twoline: $((${#userHostExp} + ${#wrkDirExp})) < $(($cols + 6))"
PS1="\n${userHost} ${wrkDir}\n${userPrompt} "
else
workingDir=`pwd`
# We need to put the userHost on it's own line
PS1="\n${userHost}\n"
if (( ${#wrkDirExp} < $cols - 20 )); then
# We can fit the prompt and working dir on one line
# echo "Twoline2: $(( ${#wrkDirExp} )) < $(($cols - 20 ))"
PS1+="${wrkDir}${userPrompt} "
else
# We have to put the prompt on it's own line
# echo "Threeline"
PS1+="${wrkDir}\n${userPrompt} "
fi
fi
PS1="$numcolor\!$red \u$sepcolor@${brightblue}\h${default}:${branch}${clean} "
# 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} "
unset clean numcolor branch retval cols
unset userHost userHostExp wrkDir wrkDirExp userPrompt userPromptExp
}
PROMPT_COMMAND=prompt_command

View File

@ -14,10 +14,13 @@ set -g history-limit 5000
set -g status-bg black
set -g status-fg white
set -g status-justify centre
set -g status-left '#[fg=green][ #[fg=blue,bright]jdb-hp#[fg=green,nobright] ]['
set -g status-left '#[fg=green][ #[fg=magenta]jdb-mg-element-win10#[fg=green,nobright] ]['
set -g status-left-length 70
set -g status-right '#[fg=green]][ #[fg=brightblue]%m-%d #[fg=brightgreen]%H:%M #[fg=green]]'
set -g status-right-length 20
set -g repeat-time 0
# Always create the main window group and session if it doesn't exist. Then,
# use tmux new-session -t main to create new sessions that attach to this
# existing window group.
new-session -s main