commit 15d2da7d889c929fc3126aac6dfb7cb9bfe2a305
Author: Nick Moffitt <nick@zork.net>
Date: Sun, 17 May 2026 17:10:03 +0100
Initial commit of current version.
Diffstat:
| A | README.md | | | 39 | +++++++++++++++++++++++++++++++++++++++ |
| A | prompt.sh | | | 69 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 108 insertions(+), 0 deletions(-)
diff --git a/README.md b/README.md
@@ -0,0 +1,39 @@
+# Prompt Engineering
+
+## A dynamic low-resource shell prompt for bash
+
+ If you're opening a shell from your own desktop system, it just shows the path:
+
+```console
+ ~$
+```
+
+ If you got to this shell via sudo, or doas, we show the usernames involved:
+
+```console
+ root<myuser>@~#
+```
+
+ If you came via ssh, we show the hostname and username:
+
+```console
+ remoteuser@remotehost:~$
+```
+
+ We display non-zero values for command exit codes and background/suspended jobs:
+
+```console
+ root<remoteuser>@remotehost:/var/www/$ less index.html
+ [1]+ Stopped less index.html
+ (148)[1]root<remoteuser>@remotehost:/var/www/$
+```
+
+ We also support $debian_chroot and the git shell status if available:
+
+```console
+ (buildchroot)remoteuser@remotehost:~/src/project{main +}$
+```
+
+ Colours have been chosen to highlight urgent information, such as non-zero
+ exit codes blinking the $ or # as appropriate.
+
diff --git a/prompt.sh b/prompt.sh
@@ -0,0 +1,69 @@
+# Dynamic low-resource prompt for bash.
+
+# define $_tty iff we know we're not on local desktop
+unset _tty
+_tty=$SSH_CONNECTION$SUDO_USER$DOAS_USER
+[ "$XDG_SESSION_TYPE" = "tty" ] && _tty="yes"
+
+# We use defined/undef logic on array indices to detect non-zero values inside
+# the prompt, which is faster than shelling out
+_noop[0]='array accesses turn 0/nonzero into defined/undef!'
+
+# Detect colour support
+case "$TERM" in
+ *[is]tty*|foot*|*color*) color_prompt=yes;;
+ *) [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null && color_prompt=yes;;
+esac
+
+# Show all of the characters for local git checkout state
+GIT_PS1_SHOWDIRTYSTATE='y'
+GIT_PS1_SHOWSTASHSTATE='y'
+GIT_PS1_SHOWUNTRACKEDFILES='y'
+GIT_PS1_DESCRIBE_STYLE='contains'
+GIT_PS1_SHOWUPSTREAM='auto'
+
+if [ "$color_prompt" = yes ]; then
+ GIT_PS1_SHOWCOLORHINTS='y'
+else
+ # simpler, uncoloured version
+ unset GIT_PS1_SHOWCOLORHINTS
+ PS1='${_noop[$(($?==0))]:+($?)}${_noop[$((\j==0))]:+[\j]}'
+ PS1+='${debian_chroot:+($debian_chroot)}'
+ PS1+='${_tty:+\u${DOAS_USER:+<$DOAS_USER>}${SUDO_USER:+<$SUDO_USER>}@}'
+ PS1+='${SSH_CONNECTION:+\h:}\w'
+ type -t __git_ps1 > /dev/null && PS1+='$(__git_ps1 "{%s}")'
+ PS1+='\$ '
+ return
+fi
+
+#################################
+# The full-colour prompt itself #
+#################################
+
+# If the last exit code ($?) was non-zero,
+# show it in red parentheses
+PS1='${_noop[$(($?==0))]:+\[\e[1;91m\]($?)}'
+# Likewise, show non-zero background/suspend count
+# in yellow square brackets.
+PS1+='${_noop[$((\j==0))]:+\[\e[1;33m\][\j]}'
+# If the debian_chroot var is defined, show it in cyan.
+PS1+='${debian_chroot:+\[\e[1;36m\]($debian_chroot)}'
+# 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/doas, show the username we came from in red.
+PS1+='${_tty:+\[\e[1;35m\]\u\[\e[1;91m\]'
+ PS1+='${SUDO_USER:+<$SUDO_USER>}${DOAS_USER:+<$DOAS_USER>}'
+ PS1+='\[\e[1;33m\]@}'
+# If we ssh'd here, show the hostname in green
+PS1+='${SSH_CONNECTION:+\[\e[1;32m\]\h\[\e[1;33m\]:}'
+# The only straightforward part: current directory in blue!
+PS1+='\[\e[1;34m\]\w'
+# If possible, show git branch info between gold curly braces
+if type -t __git_ps1 > /dev/null; then
+ PS1+='$(__git_ps1 "\[\e[1;33m\]{%s\[\e[1;33m\]}")'
+fi
+# Finally, the classic $ or # that is either bold default,
+# or blinking red if $? is nonzero
+PS1+='\[\e[$(($?==0?0:91));$(($?==0?1:5))m\]\$\[\e[0m\] '
+
+