From 76bf92b585da671e452fbb209238f0e2e879883f Mon Sep 17 00:00:00 2001 From: ray Date: Tue, 30 Sep 2025 22:20:40 +0100 Subject: [PATCH] optimize prompt divider and update PS1 formatting Refactor draw_prompt_divider for simpler timestamp output and enhance PS1 for better git status display. --- bashrc | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/bashrc b/bashrc index 3d77ef8..2f75cd6 100644 --- a/bashrc +++ b/bashrc @@ -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&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