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:
parent
05afd18b1a
commit
82fd24681f
58
bashrc
58
bashrc
|
@ -125,52 +125,40 @@ function show_date() {
|
||||||
|
|
||||||
export HISTCONTROL=ignoreboth:erasedups
|
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() {
|
function draw_prompt_divider() {
|
||||||
local width=${COLUMNS:-80} # Use terminal width, fallback to 80
|
local timestamp prefix_length line_length i
|
||||||
local timestamp=$(date '+%H:%M:%S')
|
printf -v timestamp '%(%H:%M:%S)T' -1 # Faster than $(date)
|
||||||
local prefix="$timestamp ├"
|
local prefix="$timestamp ├"
|
||||||
local prefix_length=${#prefix}
|
prefix_length=${#prefix}
|
||||||
local line_length=$((width - prefix_length))
|
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 '%s' "$prefix"
|
||||||
printf '%.0s─' $(seq 1 $line_length)
|
for ((i=0; i<line_length; i++)); do printf '─'; done
|
||||||
echo
|
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() {
|
function calculate_git_alignment() {
|
||||||
# Get the raw hostname length (without color codes) - use actual prompt format
|
# Only calculate if git status exists
|
||||||
local user_host="$USER@$HOSTNAME"
|
[[ -z "$GITSTATUS_PROMPT" ]] && { GIT_ALIGNMENT_PADDING=""; return; }
|
||||||
local hostname_only="$HOSTNAME" # Just the hostname part after @
|
|
||||||
local hostname_length=${#hostname_only}
|
|
||||||
|
|
||||||
# Extract branch name from GITSTATUS_PROMPT if it exists
|
# Fast branch name extraction using bash parameter expansion
|
||||||
local branch_name=""
|
local cleaned="${GITSTATUS_PROMPT//[$'\001\002']/}" # Remove \001 \002
|
||||||
if [[ -n "$GITSTATUS_PROMPT" ]]; then
|
cleaned="${cleaned//$'\e['[0-9\;]*m/}" # Remove ANSI codes
|
||||||
# Remove all escape sequences: \001, \002, and ANSI color codes
|
local branch_name="${cleaned%% *}" # Get first word (branch)
|
||||||
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
|
|
||||||
|
|
||||||
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
|
# Use printf once to generate padding
|
||||||
# We want: [padding spaces] + branch_name to end at same position as hostname
|
printf -v GIT_ALIGNMENT_PADDING '%*s' $needed_padding ''
|
||||||
# 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 '')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Load custom gitstatus configuration
|
# Load custom gitstatus configuration
|
||||||
|
|
Loading…
Reference in New Issue