Fix issue with bash prompt length detection

This commit is contained in:
Leo 2026-01-29 17:56:41 +02:00
parent fd2c0c1421
commit b394159a6e

View file

@ -3,22 +3,28 @@ case "$TERM" in
xterm-color|*-256color) color_prompt=yes;;
esac
function parse_git_branch() {
# git branch --show-current 2>/dev/null | sed 's/^/ (/' | sed 's/$/)/'
function __set_prompt() {
local branch
branch=$(git branch --show-current 2>/dev/null)
#[[ -n "$branch" ]] && printf ' \033[44;30m %s \033[0;34m\ue0b0\033[0m' "$branch"
# Colors (wrapped in \[ \] for proper length calculation)
local yellow_bg='\[\033[43;30m\]'
local blue_bg='\[\033[44;30m\]'
local yellow_on_blue='\[\033[44;33m\]'
local blue='\[\033[0;34m\]'
local yellow='\[\033[0;33m\]'
local reset='\[\033[0m\]'
local arrow=$'\ue0b0'
if [[ -n "$branch" ]]; then
printf '\033[44;33m'$'\ue0b0''\033[44;30m %s \033[0;34m'$'\ue0b0''\033[0m' "$branch"
PS1="${yellow_bg} \w ${yellow_on_blue}${arrow}${blue_bg} ${branch} ${blue}${arrow}${reset} "
else
printf '\033[0;33m'$'\ue0b0''\033[0m'
PS1="${yellow_bg} \w ${yellow}${arrow}${reset} "
fi
}
}
if [ "$color_prompt" = yes ]; then
PS1='\[\033[43;30m\] \w \[$(parse_git_branch)\] '
PROMPT_COMMAND=__set_prompt
else
PS1='\w\$ '
fi