optimize prompt divider and git alignment calculations

Refactor the draw_prompt_divider function for improved performance
by reducing subshells and using faster timestamp retrieval. Update
the calculate_git_alignment function to cache static values and
simplify branch name extraction for better efficiency.
This commit is contained in:
Ray Elliott 2025-09-30 21:03:56 +01:00
parent 05afd18b1a
commit 82fd24681f
1 changed files with 23 additions and 35 deletions

58
bashrc
View File

@ -125,52 +125,40 @@ function show_date() {
export HISTCONTROL=ignoreboth:erasedups
# Function to draw a terminal-width divider line with timestamp
# Optimized divider function - reduced subshells and calculations
function draw_prompt_divider() {
local width=${COLUMNS:-80} # Use terminal width, fallback to 80
local timestamp=$(date '+%H:%M:%S')
local timestamp prefix_length line_length i
printf -v timestamp '%(%H:%M:%S)T' -1 # Faster than $(date)
local prefix="$timestamp"
local prefix_length=${#prefix}
local line_length=$((width - prefix_length))
prefix_length=${#prefix}
line_length=$(( ${COLUMNS:-80} - prefix_length ))
# Print timestamp + junction + line to fill remaining width
# Print prefix then fill with box-drawing characters
printf '%s' "$prefix"
printf '%.0s─' $(seq 1 $line_length)
for ((i=0; i<line_length; i++)); do printf '─'; done
echo
}
# Function to calculate git status alignment based on hostname and branch lengths
# Cache static values that don't change during session
_CACHED_USER_HOST="$USER@$HOSTNAME"
_CACHED_USER_HOST_LENGTH=${#_CACHED_USER_HOST}
# Optimized git alignment calculation
function calculate_git_alignment() {
# Get the raw hostname length (without color codes) - use actual prompt format
local user_host="$USER@$HOSTNAME"
local hostname_only="$HOSTNAME" # Just the hostname part after @
local hostname_length=${#hostname_only}
# Only calculate if git status exists
[[ -z "$GITSTATUS_PROMPT" ]] && { GIT_ALIGNMENT_PADDING=""; return; }
# Extract branch name from GITSTATUS_PROMPT if it exists
local branch_name=""
if [[ -n "$GITSTATUS_PROMPT" ]]; then
# Remove all escape sequences: \001, \002, and ANSI color codes
local cleaned_prompt=$(echo "$GITSTATUS_PROMPT" | sed -e 's/\x01//g' -e 's/\x02//g' -e 's/\x1b\[[0-9;]*m//g')
# Extract first word as branch name and trim any whitespace
branch_name=$(echo "$cleaned_prompt" | awk '{print $1}' | tr -d '[:space:]')
fi
# Fast branch name extraction using bash parameter expansion
local cleaned="${GITSTATUS_PROMPT//[$'\001\002']/}" # Remove \001 \002
cleaned="${cleaned//$'\e['[0-9\;]*m/}" # Remove ANSI codes
local branch_name="${cleaned%% *}" # Get first word (branch)
local branch_length=${#branch_name}
# Calculate padding (cache the static hostname length)
local needed_padding=$((_CACHED_USER_HOST_LENGTH - ${#branch_name}))
[[ $needed_padding -lt 8 ]] && needed_padding=8
# Calculate padding to align the END of branch name with END of hostname
# We want: [padding spaces] + branch_name to end at same position as hostname
# Total user@hostname length is where we want to align to
local total_user_host_length=${#user_host} # "ray@MachineOne" = 14 chars
local needed_padding=$((total_user_host_length - branch_length))
# Ensure minimum indentation of 8 spaces even if branch is very long
if [[ $needed_padding -lt 8 ]]; then
needed_padding=8
fi
# Generate the padding string
GIT_ALIGNMENT_PADDING=$(printf '%*s' $needed_padding '')
# Use printf once to generate padding
printf -v GIT_ALIGNMENT_PADDING '%*s' $needed_padding ''
}
# Load custom gitstatus configuration