diff --git a/bashrc b/bashrc index 9ef4f28..5b9684b 100644 --- a/bashrc +++ b/bashrc @@ -139,11 +139,48 @@ function draw_prompt_divider() { echo } +# Function to calculate git status alignment based on hostname and branch lengths +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} + + # 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 + + local branch_length=${#branch_name} + + # 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 '') +} + # Load custom gitstatus configuration [ -f "$HOME/.config/bash/gitstatus-custom.sh" ] && . "$HOME/.config/bash/gitstatus-custom.sh" -# Set PS1 with user@host, directory on first line, git status on second line, prompt on third line -PS1='\[\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\]' +# Set up prompt command to draw divider, update git status, and calculate alignment +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 bind TAB:menu-complete diff --git a/gitstatus-custom.sh b/gitstatus-custom.sh index 9513701..371a071 100644 --- a/gitstatus-custom.sh +++ b/gitstatus-custom.sh @@ -60,5 +60,4 @@ function my_gitstatus_prompt_update() { # Start gitstatusd and set up prompt command gitstatus_stop && gitstatus_start -s -1 -u -1 -c -1 -d -1 -PROMPT_COMMAND="draw_prompt_divider; my_gitstatus_prompt_update" shopt -s promptvars \ No newline at end of file