optimize prompt divider and update PS1 formatting

Refactor draw_prompt_divider for simpler timestamp output
and enhance PS1 for better git status display.
This commit is contained in:
Ray Elliott 2025-09-30 22:20:40 +01:00
parent ac7683930b
commit 76bf92b585
1 changed files with 12 additions and 6 deletions

18
bashrc
View File

@ -125,7 +125,7 @@ function show_date() {
export HISTCONTROL=ignoreboth:erasedups
# Optimized divider function - reduced subshells and calculations
# Optimized divider function - simple timestamp without coloring
function draw_prompt_divider() {
local timestamp prefix_length line_length i
printf -v timestamp '%(%H:%M:%S)T' -1 # Faster than $(date)
@ -133,12 +133,18 @@ function draw_prompt_divider() {
prefix_length=${#prefix}
line_length=$(( ${COLUMNS:-80} - prefix_length ))
# Print prefix then fill with box-drawing characters
# Print timestamp, junction, then fill with box-drawing characters
printf '%s' "$prefix"
for ((i=0; i<line_length; i++)); do printf '─'; done
echo
}
# Function to capture exit code before other PROMPT_COMMAND functions
function capture_exit_code() {
LAST_COMMAND_EXIT_CODE=$?
echo "DEBUG: capture_exit_code sees \$?=$LAST_COMMAND_EXIT_CODE" >&2
}
# Cache static values that don't change during session
_CACHED_USER_HOST="$USER@$HOSTNAME"
_CACHED_USER_HOST_LENGTH=${#_CACHED_USER_HOST}
@ -164,7 +170,7 @@ function calculate_git_alignment() {
# Load custom gitstatus configuration
[ -f "$HOME/.config/bash/gitstatus-custom.sh" ] && . "$HOME/.config/bash/gitstatus-custom.sh"
# Set up prompt command to draw divider, update git status, and calculate alignment
# 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"
# Custom command_not_found_handle function to show errors in red
@ -172,9 +178,9 @@ command_not_found_handle() {
echo -e "\033[01;31mbash: $1: command not found\033[00m" >&2
return 127
}
# 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\]'
_error_block='██████'
# Set PS1 with user@host, directory, git status, and prompt ($ color works!)
PS1='\[\033[01;32m\]\u@\h\[\033[00m\] \[\033[1;34m\]\w\[\033[00m\]\n${GITSTATUS_PROMPT:+$GIT_ALIGNMENT_PADDING$GITSTATUS_PROMPT\n}$([ "$?" -eq 0 ] && echo "" || echo "\[\033[01;31m\]$_error_block\[\033[00m\] ")\$ \[\e]0;\u@\h: \w\a\]'
# If there are multiple matches for completion, Tab should cycle through them
bind TAB:menu-complete