Skip to content

Instantly share code, notes, and snippets.

@fbouynot
Last active May 13, 2024 15:56
Show Gist options
  • Save fbouynot/e5c011f9f4bea4e1aa9c95c41b71ab79 to your computer and use it in GitHub Desktop.
Save fbouynot/e5c011f9f4bea4e1aa9c95c41b71ab79 to your computer and use it in GitHub Desktop.
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version. Please see LICENSE.txt at the top level of
# the source code distribution for details.
# /etc/profile.d/prompt.sh
PROMPT_COMMAND='lstatus=$?'
# Print date and space separator
PS1='\D{%F %T} '
# Print green if user, red if root - escaped so it doesn't count for usable line length
PS1+='$(if [ "${EUID}" -ne 0 ]; then echo "\[\e[01;32m\]"; else echo "\[\e[01;31m\]"; fi)'
# Print user
PS1+='\u'
# Reset color - escaped so it doesn't count for usable line length
PS1+='\[\e[00m\]'
# Print @
PS1+='@'
# Green color - escaped so it doesn't count for usable line length
PS1+='\[\e[01;32m\]'
# Print Host
PS1+='\h'
# Reset color - escaped so it doesn't count for usable line length
PS1+='\[\e[00m\]'
# Print :
PS1+=':'
# Blue color - escaped so it doesn't count for usable line length
PS1+='\[\e[01;34m\]'
# Print path
PS1+='\w'
# Brown color - escaped so it doesn't count for usable line length
PS1+='\[\e[01;33m\]'
# Add git branch if any
# Sed first pass get the right line, second pass do the formatting: keep only the branch name, prepend space and [, append ]
PS1+='$(command -v git 1> /dev/null && git branch 2> /dev/null | sed -e "/^[^*]/d" -e "s/* \(.*\)/ [\1]/")'
# Add current kube cluster if any, prepend space and [, append ]
# kubectl is slow so we use awk instead
PS1+='$(kube_ns_cluster=$(if [ -f ~/.kube/config ]; then awk "/^current-context:/{print \$2;exit;}" <~/.kube/config 2> /dev/null; fi); echo "${kube_ns_cluster:+ [$kube_ns_cluster]}")'
# Reset color - escaped so it doesn't count for usable line length
PS1+='\[\e[00m\]'
# Get return code; if !0 print it in red prepended with ( appended with ), and keep red color - escaped so it doesn't count for usable line length
PS1+='$(code=${lstatus##0}; echo "${code:+\[\e[01;31m\] (${code})}")'
# Print $ if user, # if root, prepend with space
PS1+='$(if [ "${EUID}" -ne 0 ]; then echo " $"; else echo " #"; fi) '
# Reset color - escaped so it doesn't count for usable line length
PS1+='\[\e[00m\]'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment