From 42a1d34740a5c1f14dd5517d231ac62063b9b55e Mon Sep 17 00:00:00 2001 From: Jordan Atwood Date: Thu, 18 Aug 2022 11:27:30 -0700 Subject: [PATCH] Construct PS1 string once Rather than re-constructing PS1 each time the prompt is called, it can be built once. The portions of it which call functions are still called, so there is no drawback to setting it and forgetting it. --- src/.bashrc | 46 ++++++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/src/.bashrc b/src/.bashrc index 0515067..fc8d09f 100755 --- a/src/.bashrc +++ b/src/.bashrc @@ -453,31 +453,10 @@ function _ps1_color { echo -e "$color" } -# PS1 builder and history appender -# see _ps1_color for color reference for each PS1 segment -# Note: all non-printing sequences must be surrounded by \[ \] -# Note: bash PS1 title can only reference variables eg. $PWD -# (calling functions causes errors in title eg. pwd) +# history appender function _bashrc_prompt_command { - # this must run first to capture exit status - local EXIT="$?" - # append last command to history history -a - - # set window title - # format: $PWD; $?=# - PS1='\[\e]0;'"$PWD"'; \$?='"$EXIT"'\007\]' - PS1+='\n' - - # set first line - # format: [user@host path] (git branch) - PS1+='\[$(_ps1_color bracket)\][\[$(_ps1_color user)\]\u\[$(_ps1_color @)\]@\[$(_ps1_color host)\]\[$(hostname -f 2>/dev/null || hostname)\] \[$(_ps1_color path)\]\w\[$(_ps1_color bracket)\]]\[$(_ps1_color branch)\]\[$(__git_ps1 2>/dev/null)\]\[$(_ps1_color prompt)\]' - PS1+='\n' - - # set second line - # format: $ - PS1+='\[$(_ps1_color preprompt)\]\$\[$(_ps1_color prompt)\] ' } # exit function @@ -628,6 +607,25 @@ _bashrc_ssh-add-keys # echo motd _bashrc_motd -# sets PS1, appends to history -PROMPT_COMMAND="_bashrc_prompt_command" +### set PS1/PS2, appends to history + +# see _ps1_color for color reference for each PS1 segment +# Note: all non-printing sequences must be surrounded by \[ \] +# Note: bash PS1 title can only reference variables eg. $PWD +# (calling functions causes errors in title eg. pwd) + +# set window title +# format: $PWD; $?=# +PS1='\[\e]0;'"$PWD"'; \$?=$?\007\]' +PS1+='\n' + +# set first line +# format: [user@host path] (git branch) +PS1+='\[$(_ps1_color bracket)\][\[$(_ps1_color user)\]\u\[$(_ps1_color @)\]@\[$(_ps1_color host)\]\[$(hostname -f 2>/dev/null || hostname)\] \[$(_ps1_color path)\]\w\[$(_ps1_color bracket)\]]\[$(_ps1_color branch)\]\[$(__git_ps1 2>/dev/null)\]\[$(_ps1_color prompt)\]' +PS1+='\n' + +# set second line +# format: $ +PS1+='\[$(_ps1_color preprompt)\]\$\[$(_ps1_color prompt)\] ' PS2='\[$(_ps1_color preprompt)\]>\[$(_ps1_color prompt)\] ' +PROMPT_COMMAND="_bashrc_prompt_command"