commit d4a96b677e5a638b9113b5977d757308c24103fc
parent c7d0428c901507b4dd9c97d793f10b6eb5486fd0
Author: spacehobo <spacehobo@web>
Date: Sat, 16 May 2026 20:15:24 +0200
handle doas, missing __git_ps1, and simplify non-desktop session detection
Diffstat:
1 file changed, 13 insertions(+), 13 deletions(-)
diff --git a/spacehobo.mdwn b/spacehobo.mdwn
@@ -3,9 +3,11 @@
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
-# and we aren't on a new enough system to support it.
-[ $((${#SSH_CONNECTION}+${#SUDO_USER})) = 0 ] || true ${XDG_SESSION_TYPE:=tty}
+# Detect if we're in via ssh, sudo, or doas, or our session type is "tty"
+unset _tty
+_tty=$SSH_CONNECTION$SUDO_USER$DOAS_USER
+[ "$XDG_SESSION_TYPE" = "tty" ] && _tty="yes"
+
GIT_PS1_SHOWDIRTYSTATE='y'
GIT_PS1_SHOWSTASHSTATE='y'
GIT_PS1_SHOWUNTRACKEDFILES='y'
@@ -13,8 +15,6 @@ 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
@@ -25,8 +25,10 @@ if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
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='\$ '
+ type -t __git_ps1 > /dev/null && PS1='$(__git_ps1 "{%s}")'$PS1
+ PS1='${SSH_CONNECTION:+\h:}\w'
+ PS1='${_tty:-X}]:+\u${DOAS_USER:+($DOAS_USER)}${SUDO_USER:+($SUDO_USER)}@}'$PS1
PS1='${debian_chroot:+($debian_chroot)}'$PS1
PS1='${_noop[$(($?==0))]:+($?)}${_noop[$((\j==0))]:+[\j]}'$PS1
return
@@ -37,17 +39,15 @@ fi
# if $? is nonzero
PS1='\[\e[$(($?==0?0:91));$(($?==0?1:5))m\]\$\[\e[0m\] '
# Git branch information between gold curly braces
-PS1='$(__git_ps1 "\[\e[1;33m\]{%s\[\e[1;33m\]}")'$PS1
+type -t __git_ps1 > /dev/null && PS1='$(__git_ps1 "\[\e[1;33m\]{%s\[\e[1;33m\]}")'$PS1
# The only straightforward part: current directory in blue
PS1='\[\e[1;34m\]\w'$PS1
# If we ssh'd here, show the hostname in green
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.
# 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.
-PS1='${_session[${XDG_SESSION_TYPE:-X}]:+\[\e[1;35m\]\u${SUDO_USER:+\[\e[1;91m\]($SUDO_USER)}\[\e[1;33m\]@}'$PS1
+# If we did sudo/doas, show the username we came from in red.
+PS1='${_tty:+\[\e[1;35m\]\u\[\e[1;91m\]${SUDO_USER:+($SUDO_USER)}${DOAS_USER:+($DOAS_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,
@@ -62,7 +62,7 @@ The prompt environment is fairly limited. It can do parameter expansion, refere
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 use the length of some sudo/ssh vars to decide whether to set it.
+It also only displays the hostname if the `$XDG_SESSION_TYPE` is `"tty"` or if we have variables set by `ssh`, `sudo`, or `doas`. It dynamically displays the `$SUDO_USER` and `$DOAS_USER`, if that is available.
What this means is that on your local desktop system, when you log in, your prompt is merely: