commit c7d0428c901507b4dd9c97d793f10b6eb5486fd0
parent 150ceb0e45a2e0c1738092cb73deffb80ff291fa
Author: spacehobo <spacehobo@web>
Date: Sat, 16 May 2026 12:17:35 +0200
uncoloured version works now
Diffstat:
1 file changed, 13 insertions(+), 7 deletions(-)
diff --git a/spacehobo.mdwn b/spacehobo.mdwn
@@ -3,7 +3,7 @@
Hey, check out my neat `bash` prompt, which I put in eik's `/etc/bash_completion.d/prompt.bash` so everyone gets it:
```{sh}
-# Set XDG_SESSION_TYPE to "tty" if sudo/ssh are involved
+# Set XDG_SESSION_TYPE to "tty" if sudo/ssh are involved
# and we aren't on a new enough system to support it.
[ $((${#SSH_CONNECTION}+${#SUDO_USER})) = 0 ] || true ${XDG_SESSION_TYPE:=tty}
GIT_PS1_SHOWDIRTYSTATE='y'
@@ -11,6 +11,11 @@ GIT_PS1_SHOWSTASHSTATE='y'
GIT_PS1_SHOWUNTRACKEDFILES='y'
GIT_PS1_DESCRIBE_STYLE='contains'
GIT_PS1_SHOWUPSTREAM='auto'
+# We use defined/undef logic on array indices to control values inside the
+# prompt, which is faster than shelling out to test data
+declare -A _session
+_session["tty"]='array accesses turn tty/* into defined/undef!'
+_noop[0]='array accesses turn 0/nonzero into defined/undef!'
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
# We have color support; assume it's compliant with Ecma-48
@@ -18,10 +23,15 @@ if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
# a case would tend to support setf rather than setaf.)
GIT_PS1_SHOWCOLORHINTS='y'
else
+ # simpler, uncoloured version
+ unset GIT_PS1_SHOWCOLORHINTS
+ PS1='${SSH_CONNECTION:+\h:}\w$(__git_ps1 "{%s}")\$ '
+ PS1='${_session[${XDG_SESSION_TYPE:-X}]:+\u${SUDO_USER:+($SUDO_USER)}@}'$PS1
+ PS1='${debian_chroot:+($debian_chroot)}'$PS1
+ PS1='${_noop[$(($?==0))]:+($?)}${_noop[$((\j==0))]:+[\j]}'$PS1
return
fi
-
# We build the shell from right to left, starting with a
# $ or # that is either bold default, or blinking red
# if $? is nonzero
@@ -37,19 +47,15 @@ PS1='${SSH_CONNECTION:+\[\e[1;32m\]\h\[\e[1;33m\]:}'$PS1
# If this is our local machine and we didn't sudo, we should know
# our username already! Otherwise if it's "tty", show it in purple.
# If we did sudo, show the username we came from in red.
-declare -A _session
-_session["tty"]='array accesses turn tty/* into defined/undef!'
PS1='${_session[${XDG_SESSION_TYPE:-X}]:+\[\e[1;35m\]\u${SUDO_USER:+\[\e[1;91m\]($SUDO_USER)}\[\e[1;33m\]@}'$PS1
# If the debian_chroot var is defined, show it in cyan.
PS1='${debian_chroot:+\[\e[1;36m\]($debian_chroot)}'$PS1
# If the number of jobs suspended/backgrounded is nonzero,
# show it in yellow square brackets.
-_noop[0]='array accesses turn 0/nonzero into defined/undef!'
PS1='${_noop[$((\j==0))]:+\[\e[1;33m\][\j]}'$PS1
# Likewise if the last exit code was non-zero,
# show it in red parentheses.
PS1='${_noop[$(($?==0))]:+\[\e[1;91m\]($?)}'$PS1
-
```
The prompt environment is fairly limited. It can do parameter expansion, reference expansion, arithmetic expansion, and a couple other neat tricks. You can shell out, but if you do that overwrites the variable that contains the exit code of the last program you ran (`$?`). Tools such as the `__git_ps1` hacks do their best to only use features of bash that work internally to bash itself.
@@ -70,6 +76,6 @@ And if you are in a chroot with background processes looking at a git repo after
(1)[2](thechroot)user@hostname:~/src/therepo{main $%}$
-with coloured styling information on all the various parts. I'm tinkering with a branch to turn off colours but keep the dynamism if tput fails.
+with coloured styling information on all the various parts, if `tput setaf 1` was successful on your terminal. Otherwise you get a plain text version.
I just think it's neat!