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:
parent
ac7683930b
commit
76bf92b585
18
bashrc
18
bashrc
|
@ -125,7 +125,7 @@ function show_date() {
|
||||||
|
|
||||||
export HISTCONTROL=ignoreboth:erasedups
|
export HISTCONTROL=ignoreboth:erasedups
|
||||||
|
|
||||||
# Optimized divider function - reduced subshells and calculations
|
# Optimized divider function - simple timestamp without coloring
|
||||||
function draw_prompt_divider() {
|
function draw_prompt_divider() {
|
||||||
local timestamp prefix_length line_length i
|
local timestamp prefix_length line_length i
|
||||||
printf -v timestamp '%(%H:%M:%S)T' -1 # Faster than $(date)
|
printf -v timestamp '%(%H:%M:%S)T' -1 # Faster than $(date)
|
||||||
|
@ -133,12 +133,18 @@ function draw_prompt_divider() {
|
||||||
prefix_length=${#prefix}
|
prefix_length=${#prefix}
|
||||||
line_length=$(( ${COLUMNS:-80} - prefix_length ))
|
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"
|
printf '%s' "$prefix"
|
||||||
for ((i=0; i<line_length; i++)); do printf '─'; done
|
for ((i=0; i<line_length; i++)); do printf '─'; done
|
||||||
echo
|
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
|
# Cache static values that don't change during session
|
||||||
_CACHED_USER_HOST="$USER@$HOSTNAME"
|
_CACHED_USER_HOST="$USER@$HOSTNAME"
|
||||||
_CACHED_USER_HOST_LENGTH=${#_CACHED_USER_HOST}
|
_CACHED_USER_HOST_LENGTH=${#_CACHED_USER_HOST}
|
||||||
|
@ -164,7 +170,7 @@ function calculate_git_alignment() {
|
||||||
# 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 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"
|
PROMPT_COMMAND="draw_prompt_divider; my_gitstatus_prompt_update; calculate_git_alignment"
|
||||||
|
|
||||||
# Custom command_not_found_handle function to show errors in red
|
# 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
|
echo -e "\033[01;31mbash: $1: command not found\033[00m" >&2
|
||||||
return 127
|
return 127
|
||||||
}
|
}
|
||||||
|
_error_block='██████'
|
||||||
# Set PS1 with user@host, directory on first line, dynamically aligned git status, prompt on third line
|
# 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}└── \[\033[01;$((31+!$?))m\]\$\[\033[00m\] \[\e]0;\u@\h: \w\a\]'
|
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
|
# If there are multiple matches for completion, Tab should cycle through them
|
||||||
bind TAB:menu-complete
|
bind TAB:menu-complete
|
||||||
|
|
Loading…
Reference in New Issue