commit 0ece370405487760ffa90d5da156f032f6f74638
parent 4d3573234058c5f6b6d341f4a736d0985ff034b4
Author: Kévin Le Gouguec <kevin.legouguec@gmail.com>
Date: Sun, 21 Jun 2026 20:31:09 +0200
Mess with Bash prompt
Boil PS1 down to just '\$ ', and echo extra info during
PROMPT_COMMAND. Not sure why, maybe a hunch that debugging PS1 is
simpler if it's not so full of crud.
This does mean that ^L (clear-screen) leaves me context-less. Might
end up too annoying, will see.
While in there,
* remove horizontal rule; living without it in M-x shell, so not sure
why I'd need it in Konsole,
* use dumb settings for all 'dumb*' terminals (e.g. dumb-emacs-ansi).
Diffstat:
| M | .bash_prompt | | | 96 | +++++++++++++++++++++++++++++++++---------------------------------------------- |
1 file changed, 40 insertions(+), 56 deletions(-)
diff --git a/.bash_prompt b/.bash_prompt
@@ -25,12 +25,20 @@ __have-gitprompt ()
test -f /usr/lib/git-core/git-sh-prompt
}
+__ps1-pwd ()
+{
+ # Empirically, no need to escape slashes in $HOME: Bash seems to
+ # parse ${VAR/#PAT/REP} by (1) splitting VAR, PAT & REP (2) *then*
+ # expanding PAT?
+ echo "${PWD/#${HOME}/\~}"
+}
+
__set-title ()
{
local title=${USER}
__show-hostname && title+=@${HOSTNAME}
title+=:
- title+=${PWD/~/\~}
+ title+=$(__ps1-pwd)
# Cf. console_codes(4):
#
@@ -105,58 +113,46 @@ __current-column ()
__signal-no-newline ()
{
+ local -ir col=$(__current-column)
+ ((col > 1)) || return
+
__fontify ∅ bold red
echo
}
-__draw-rule ()
+__signal-rc ()
{
- local -ir pos=$(__current-column)
- local -ir rule_len=$((COLUMNS-pos))
-
- local rule
- # Hey Future Me 👋 Breaking this down:
- # -v rule "put this in LINE, do not write to stdout"
- # "%…s" "pad string…"
- # - " … with spaces…"
- # * " … with length given by next argument"
- # ${rule_len} (padding length)
- # ∅ (psych! no arg for %s, default to null string)
- printf -v rule '%-*s' ${rule_len}
- rule=${rule// /─}
-
- # Flush line to avoid future text being shoved offscreen when autowrap
- # is off.
- rule+=$'\n'
-
- __fontify "${rule}" "$@"
+ local -ir rc=$1
+ ((rc)) || return
+
+ __fontify '$?: ' dim red
+ __fontify ${rc} bold red
+ echo
}
__write-context ()
{
- __fontify '\u' green
+ # Decided to remove things off PS1 & print them directly instead,
+ # on a whim. No way to expand backslash sequences outside PS𝓃(?)
+ # so reimplement things like \w's s/$HOME/~.
+
+ __fontify "${USER}" green
__show-hostname && {
__fontify @ dim
- __fontify '\H' bold green
+ __fontify "${HOSTNAME}" bold green
}
printf ' '
- __fontify '\w' bold blue
+
+ __fontify "$(__ps1-pwd)" bold blue
test "${PS1_SHOWGITSTATUS}" && {
printf ' '
__fontify "$(__git_ps1 '(%s)')" red
}
-}
-
-__smart-term/set-ps1 ()
-{
- BUILDING_PS=t
- PS1="$(__write-context)\n$(__fontify '\$' dim) "
-
- unset BUILDING_PS
+ echo
}
__smart-term/refresh ()
@@ -164,19 +160,9 @@ __smart-term/refresh ()
local -ir rc=$?
__set-title
-
- local -ir pos=$(__current-column)
- ((pos != 1)) && __signal-no-newline
-
- if ((rc))
- then
- __fontify "${rc} " bold red
- __draw-rule dim red
- else
- __draw-rule dim
- fi
-
- __smart-term/set-ps1
+ __signal-no-newline
+ __signal-rc ${rc}
+ __write-context
}
__smart-term/init ()
@@ -193,6 +179,10 @@ __smart-term/init ()
PROMPT_COMMAND=__smart-term/refresh
+ PS1=$(
+ BUILDING_PS=t
+ __fontify '\$ ' dim
+ )
PS2=$(
BUILDING_PS=t
__fontify '… ' dim
@@ -206,27 +196,21 @@ __smart-term/init ()
tty -s && stty stop ''
}
-__dumb-term/set-ps1 ()
+__dumb-term/refresh ()
{
local -ir rc=$?
-
- BUILDING_PS=t
-
- PS1=''
- ((rc)) && PS1+=$(__fontify "${rc} " bold red)
- PS1+='\W\$ '
-
- unset BUILDING_PS
+ __signal-rc ${rc}
}
__dumb-term/init ()
{
- PROMPT_COMMAND=__dumb-term/set-ps1
+ PROMPT_COMMAND=__dumb-term/refresh
+ PS1='\W\$ '
}
case "${TERM}"
in
- dumb) __dumb-term/init ;;
+ dumb*) __dumb-term/init ;;
*) __smart-term/init ;;
esac