permacomputing

Source repository for the main permacomputing wiki site
git clone http://git.permacomputing.net/repos/permacomputing.git # read-only access
Log | Files | Refs

commit b05a07373ba5884964e083f13329d8996fe6c482
parent 2beca7cba055dabe8cc125c85675617e1704b65a
Author: spacehobo <spacehobo@web>
Date:   Fri, 15 May 2026 20:17:33 +0200

Hacks to make it work with pre-freedesktop.org environments on desktop.

Diffstat:
Mspacehobo.mdwn | 40+++++++++++++++++++++++++++++-----------
1 file changed, 29 insertions(+), 11 deletions(-)

diff --git a/spacehobo.mdwn b/spacehobo.mdwn @@ -2,7 +2,7 @@ Hey, check out my neat `bash` prompt: -``` +```{sh} GIT_PS1_SHOWCOLORHINTS='y' GIT_PS1_SHOWDIRTYSTATE='y' GIT_PS1_SHOWSTASHSTATE='y' @@ -10,24 +10,42 @@ GIT_PS1_SHOWUNTRACKEDFILES='y' GIT_PS1_DESCRIBE_STYLE='contains' GIT_PS1_SHOWUPSTREAM='auto' -_noop[0]='array accesses turn 0/nonzero into defined/undef!' +# We build the prompt 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\] ' +# Git branch information between gold curly braces +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. +# 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. declare -A _session _session["tty"]='array accesses turn tty/* into defined/undef!' -PS1='\[\e[$(($?==0?0:91));$(($?==0?1:5))m\]\$\[\e[0m\] ' # $?-coloured $ -PS1='$(__git_ps1 "\[\e[1;33m\]{%s\[\e[1;33m\]}")'$PS1 # Gold git -PS1='\[\e[1;34m\]\w'$PS1 # Blue CWD -PS1='${SSH_CONNECTION:+\[\e[1;32m\]\h\[\e[1;33m\]:}'$PS1 # Green hostname, gold : -PS1='${_session[$XDG_SESSION_TYPE]:+\[\e[1;35m\]\u${SUDO_USER:+\[\e[1;91m\]($SUDO_USER)}\[\e[1;33m\]@}'$PS1 # Purple username -PS1='${debian_chroot:+\[\e[1;36m\]($debian_chroot)}'$PS1 # Cyan chroot -PS1='${_noop[$((\j==0))]:+\[\e[1;33m\][\j]}'$PS1 # gold nonzero jobs -PS1='${_noop[$(($?==0))]:+\[\e[1;91m\]($?)}'$PS1 # Red nonzero $? +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 +# 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. 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. +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. What this means is that on your local desktop system, when you log in, your prompt is merely: