commit 2765a69bd3c65af9bd72a927cff065439401e612
parent b05a07373ba5884964e083f13329d8996fe6c482
Author: spacehobo <spacehobo@web>
Date: Fri, 15 May 2026 21:01:23 +0200
new version with simpler logic
Diffstat:
1 file changed, 8 insertions(+), 6 deletions(-)
diff --git a/spacehobo.mdwn b/spacehobo.mdwn
@@ -9,8 +9,11 @@ GIT_PS1_SHOWSTASHSTATE='y'
GIT_PS1_SHOWUNTRACKEDFILES='y'
GIT_PS1_DESCRIBE_STYLE='contains'
GIT_PS1_SHOWUPSTREAM='auto'
+# 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}
-# We build the prompt from right to left, starting with a
+# We build the shell from right to left, starting with a
# $ or # that is either bold default, or blinking red
# if $? is nonzero
PS1='\[\e[$(($?==0?0:91));$(($?==0?1:5))m\]\$\[\e[0m\] '
@@ -22,14 +25,12 @@ PS1='\[\e[1;34m\]\w'$PS1
PS1='${SSH_CONNECTION:+\[\e[1;32m\]\h\[\e[1;33m\]:}'$PS1
# freedesktop.org defines the $XDG_SESSION_TYPE variable,
# which is "tty" for sudo or ssh sessions, but this is kind of new.
-# So we default to tty but mess it up with as many environment
-# variables used by GUI desktop sessions to detect if you're local.
# 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 ran sudo under in red.
+# 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:-tty${TERMUX_VERSION}${!GNOME@}${!DESKTOP@}${!KDE@}}]:+\[\e[1;35m\]\u${SUDO_USER:+\[\e[1;91m\]($SUDO_USER)}\[\e[1;33m\]@}'$PS1
+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,
@@ -39,13 +40,14 @@ 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.
This prompt uses the `_noop` array (with only `${_noop[0]}` defined) to turn arithmetic expressions into defined/undefined responses. This lets us use some of the brace-expansion features to specify alternate output, so we can display the error code or number of jobs in the background *only* if they're non-zero, and we can style them with a high degree of freedom.
-It does a similar trick with a string-indexed associative array, and only displays the hostname if the `$XDG_SESSION_TYPE` is `"tty"` (which happens in `ssh` or `sudo` sessions). It dynamically displays the `$SUDO_USER` if that is available. If the system doesn't have the freedesktop XDG variable set, we try to set it to `"tty"` and then override that by trying to include any evidence of GUI desktop environment variables. This ends up doing some double-default nesting via the `_session` array.
+It does a similar trick with a string-indexed associative array, and only displays the hostname if the `$XDG_SESSION_TYPE` is `"tty"` (which happens in `ssh` or `sudo` sessions). It dynamically displays the `$SUDO_USER` if that is available. If the system doesn't have the freedesktop XDG variable set, we use the length of some sudo/ssh vars to decide whether to set it.
What this means is that on your local desktop system, when you log in, your prompt is merely: