Compare commits

..

4 Commits

Author SHA1 Message Date
Ray Elliott 33c485caa5 improve branch name extraction in git alignment
Use sed for reliable escape sequence removal in the
calculate_git_alignment function to enhance performance.
2025-09-30 21:07:50 +01:00
Ray Elliott 82fd24681f 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.
2025-09-30 21:03:56 +01:00
Ray Elliott 05afd18b1a add git alignment calculation for prompt display
Enhance the prompt by calculating alignment based on the
hostname and branch lengths, ensuring better visibility
of git status in the terminal.
2025-09-30 21:01:00 +01:00
Ray Elliott f730f0f604 add prompt divider and update command for gitstatus
Introduced a function to draw a terminal-width divider with a timestamp
and updated the PROMPT_COMMAND to include this new function for better
visual separation in the terminal prompt.
2025-09-30 20:44:24 +01:00
2 changed files with 41 additions and 3 deletions

43
bashrc
View File

@ -125,11 +125,50 @@ function show_date() {
export HISTCONTROL=ignoreboth:erasedups export HISTCONTROL=ignoreboth:erasedups
# Optimized divider function - reduced subshells and calculations
function draw_prompt_divider() {
local timestamp prefix_length line_length i
printf -v timestamp '%(%H:%M:%S)T' -1 # Faster than $(date)
local prefix="$timestamp"
prefix_length=${#prefix}
line_length=$(( ${COLUMNS:-80} - prefix_length ))
# Print prefix then fill with box-drawing characters
printf '%s' "$prefix"
for ((i=0; i<line_length; i++)); do printf '─'; done
echo
}
# 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() {
# Only calculate if git status exists
[[ -z "$GITSTATUS_PROMPT" ]] && { GIT_ALIGNMENT_PADDING=""; return; }
# Fast branch name extraction - use sed for reliable escape sequence removal
local cleaned
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)
# Calculate padding (cache the static hostname length)
local needed_padding=$((_CACHED_USER_HOST_LENGTH - ${#branch_name}))
[[ $needed_padding -lt 8 ]] && needed_padding=8
# Use printf once to generate padding
printf -v GIT_ALIGNMENT_PADDING '%*s' $needed_padding ''
}
# Load custom gitstatus configuration # Load custom gitstatus configuration
[ -f "$HOME/.config/bash/gitstatus-custom.sh" ] && . "$HOME/.config/bash/gitstatus-custom.sh" [ -f "$HOME/.config/bash/gitstatus-custom.sh" ] && . "$HOME/.config/bash/gitstatus-custom.sh"
# Set PS1 with time, user@host, directory on first line, git status on second line, prompt on third line # Set up prompt command to draw divider, update git status, and calculate alignment
PS1='\t \[\033[01;32m\]\u@\h\[\033[00m\] \[\033[1;34m\]\w\[\033[00m\]\n${GITSTATUS_PROMPT:+ $GITSTATUS_PROMPT\n}\[\033[01;$((31+!$?))m\]\$\[\033[00m\] \[\e]0;\u@\h: \w\a\]' PROMPT_COMMAND="draw_prompt_divider; my_gitstatus_prompt_update; calculate_git_alignment"
# Set PS1 with user@host, directory on first line, dynamically aligned git status, prompt on third line
PS1='\[\033[01;32m\]\u@\h\[\033[00m\] \[\033[1;34m\]\w\[\033[00m\]\n${GITSTATUS_PROMPT:+$GIT_ALIGNMENT_PADDING$GITSTATUS_PROMPT\n}\[\033[01;$((31+!$?))m\]\$\[\033[00m\] \[\e]0;\u@\h: \w\a\]'
# If there are multiple matches for completion, Tab should cycle through them # If there are multiple matches for completion, Tab should cycle through them
bind TAB:menu-complete bind TAB:menu-complete

View File

@ -60,5 +60,4 @@ function my_gitstatus_prompt_update() {
# Start gitstatusd and set up prompt command # Start gitstatusd and set up prompt command
gitstatus_stop && gitstatus_start -s -1 -u -1 -c -1 -d -1 gitstatus_stop && gitstatus_start -s -1 -u -1 -c -1 -d -1
PROMPT_COMMAND=my_gitstatus_prompt_update
shopt -s promptvars shopt -s promptvars