improve branch name extraction in git alignment

Use sed for reliable escape sequence removal in the
calculate_git_alignment function to enhance performance.
This commit is contained in:
Ray Elliott 2025-09-30 21:07:50 +01:00
parent 82fd24681f
commit 33c485caa5
1 changed files with 4 additions and 4 deletions

8
bashrc
View File

@ -148,10 +148,10 @@ function calculate_git_alignment() {
# Only calculate if git status exists # Only calculate if git status exists
[[ -z "$GITSTATUS_PROMPT" ]] && { GIT_ALIGNMENT_PADDING=""; return; } [[ -z "$GITSTATUS_PROMPT" ]] && { GIT_ALIGNMENT_PADDING=""; return; }
# Fast branch name extraction using bash parameter expansion # Fast branch name extraction - use sed for reliable escape sequence removal
local cleaned="${GITSTATUS_PROMPT//[$'\001\002']/}" # Remove \001 \002 local cleaned
cleaned="${cleaned//$'\e['[0-9\;]*m/}" # Remove ANSI codes cleaned=$(echo "$GITSTATUS_PROMPT" | sed -e 's/\x01//g' -e 's/\x02//g' -e 's/\x1b\[[0-9;]*m//g')
local branch_name="${cleaned%% *}" # Get first word (branch) local branch_name="${cleaned%% *}" # Get first word (branch)
# Calculate padding (cache the static hostname length) # Calculate padding (cache the static hostname length)
local needed_padding=$((_CACHED_USER_HOST_LENGTH - ${#branch_name})) local needed_padding=$((_CACHED_USER_HOST_LENGTH - ${#branch_name}))