#!/usr/bin/env bash# vim:syntax=sh:ts=4# ----- PROMPT COLOURS ----- #
fgcolor(){#echo "\\[\\033[38;5;"$1"m\\]"echo"\033[38;5;${1}m"}
fgtcolor(){echo"\033[38;2;$1;$2;$3m"}
bgcolor(){#echo "\\[\\033[48;5;"$1"m\\]"echo"\033[48;5;${1}m"}# This is Solaris bash compatible, so we cannot use + here for "at least once) but instead use the# stupid old asterisk (*). Well, here hetting "no digits" practically won't hurt.MYUID="$(id|sed's/.*uid=\([0-9]*\).*/\1/g')";exportMYUID
# ...because "empty" will hit the "any" condition in the end :)OSMAJVER="$(grep'^VERSION_ID'/etc/os-release|awk-F='{print $NF}'|sed's/"//g'|awk-F\.'{print $1}')"case"$MYUID"in0)FGC1="fgcolor 88"case"$OSNAME"indebian)# Debian 12 = emeraldif["$OSMAJVER"-ge12];thenFGC3="fgcolor 36";FGC2="fgtcolor 155 15 64"elseFGC3="fgcolor 38";FGC2="fgtcolor 155 15 64"fi;;arch)FGC2="fgcolor 24";FGC3="fgcolor 240";;redhat)FGC1="fgcolor 23";FGC2="fgcolor 88";FGC3="fgcolor 203";;fedora)FGC1="fgcolor 160";FGC2="fgcolor 88";FGC3="fgcolor 203";;# with the advent of Rocky, we'll match the time to SUSE's blue and the hostname to# the colour of Geeko:#*suse|aix) FGC2="fgcolor 22";FGC3="fgcolor 41";;*sles*)FGC2="fgcolor 22";FGC3="fgcolor 83";;*suse*)case"$OSNAME"in*leap)FGC2="fgcolor 214";FGC3="fgcolor 83";;*)FGC2="fgcolor 30";FGC3="fgcolor 83";;esac;;aix)FGC2="fgcolor 238";FGC3="fgcolor 83";;*centos*)FGC2="fgcolor 25";FGC3="fgcolor 208";;*solaris)FGC2="fgcolor 31";FGC3="fgcolor 124";;ubuntu)if["$OSMAJVER"-ge22];thenifprintf"%b""$TERM"|grep-P'xterm|256'>/dev/null;thenFGC2="fgcolor 89";FGC3="fgcolor 208"elseFGC2="fgcolor 53";FGC3="fgcolor 184"fielseFGC2="fgcolor 130";FGC3="fgcolor 215"fi;;rasp*)FGC2="fgcolor 253";FGC3="fgcolor 126";;rocky)FGC2="fgcolor 22";FGC3="fgcolor 41";;*)FGC2="fgcolor 240";FGC3="fgcolor 244";;esacif[-n"$TERMUX_APK_RELEASE"];thenif["$TERMUX_APK_RELEASE"=="F_DROID"];thenFGC2="fgcolor 27";FGC3="fgcolor 35"fifi;;*)FGC2="fgcolor 253"case"$OSNAME"indebian)if["$OSMAJVER"-ge12];thenFGC1="fgtcolor 155 15 64";FGC3="fgcolor 36"elseFGC1="fgtcolor 155 15 64";FGC3="fgcolor 38"fi;;arch)FGC1="fgcolor 24";FGC3="fgcolor 239";;redhat)FGC1="fgcolor 23";FGC3="fgcolor 88";;fedora)FGC1="fgcolor 88";FGC3="fgcolor 203";;#*suse|aix) FGC1="fgcolor 22";FGC3="fgcolor 41";;*sles*)FGC1="fgcolor 22";FGC3="fgcolor 83";;*suse*)case"$OSNAME"in*leap)FGC1="fgcolor 214";FGC3="fgcolor 83";;*)FGC1="fgcolor 30";FGC3="fgcolor 83";;esac;;aix)FGC1="fgcolor 238";FGC3="fgcolor 83";;*centos*)FGC1="fgcolor 25";FGC3="fgcolor 208";;*solaris)FGC1="fgcolor 31";FGC3="fgcolor 124";;ubuntu)if["$OSMAJVER"-ge22];thenifprintf"%b""$TERM"|grep-P'xterm|256'>/dev/null;thenFGC1="fgcolor 89";FGC3="fgcolor 208"elseFGC1="fgcolor 53";FGC3="fgcolor 184"fielseFGC1="fgcolor 130";FGC3="fgcolor 215"fi;;rasp*)FGC1="fgcolor 53";FGC3="fgcolor 126";;rocky)FGC1="fgcolor 22";FGC3="fgcolor 41";;*)FGC1="fgcolor 240";FGC3="fgcolor 244";;esacif[-n"$TERMUX_APK_RELEASE"];thenif["$TERMUX_APK_RELEASE"=="F_DROID"];thenFGC1="fgcolor 27";FGC3="fgcolor 35"fifi;;esac# HYPERVISOR LAYER 8 SOFTWAREifpveversion>/dev/null2>&1;thenFGC1="fgcolor 236";FGC3="fgcolor 130"fi# make it possible to draw user-defined colours# shellcheck disable=SC1090,SC1091[-r"${BBD}/prompt.bash.local"]&&source"${BBD}/prompt.bash.local"# ----- USER SEP. AND HOSTNAME ----- #UHSEP="@"[-n"$TMUX_PANE"]&&UHSEP="[T]"[-n"$BYOBU_WINDOW_NAME"]&&UHSEP="[B]"["$(hostname-s)"=="monitoring"]&&PHN="$(hostname-f)"||PHN="$(hostname-s)"# ----- ASSEMBLE THE PROMPT COMMAND ADDITION ----- #
__prompt_command(){localEXIT="$?"if["$EXIT"-lt10];thenlocalEXIT2="00$EXIT"elif["$EXIT"-lt100];thenlocalEXIT2="0$EXIT"elselocalEXIT2="$EXIT"ficase"$MYUID"in0)PS1="\\# \[$(tputbold)\]\[$($FGC1)\]\\u\[$($FGC2)\]@$PHN \[$($FGC3)\]\\w \[$(tputsgr0)\]\\n";;*)PS1="\\# \[$(tputbold)\]\[$($FGC1)\]\t \[$($FGC2)\]\\u$UHSEP\[$($FGC3)\]$PHN \\w \[$(tputsgr0)\]\\n";;esac# git-completion. Sources:# - https://github.com/git/git/tree/master/contrib/completion (main)# - https://github.com/felipec/git-completion (fork)# - Debian: easygit# - Arch: git-completion (AUR, with functionality tests) - search for ...share/git(-completion)?/prompt.sh# - Manual measure: this repository, /git-prompt# Why all this fuss? magicmonty takes control over the whole prompt, __git_ps1 only serves a part# of it. We don't like monoliths, we like modules; the latter is good practice.# also, see postexec --> one-time sourcing of git's prompt.shifcommand-v__git_ps1>/dev/null2>&1;thenGIT_PS1_SHOWCOLORHINTS=1PS1+="$(__git_ps1"%s ")"fiif["$EXIT"-eq0];thenPS1+="\[\033[0;32m\]# "elif["$EXIT"-eq255]||["$EXIT"-eq127];thenPS1+="\[\033[0;33m\]$EXIT2 # "elif["$EXIT"-eq130];thenPS1+="\[\033[0;35m\]$EXIT2 # "elsePS1+="\[\033[0;31m\]$EXIT2 # "fiPS1+="\[\033[0m\]"}# ----- ADD __prompt_command() TO $PROMPT_COMMAND ----- #if!printf"%b""$PROMPT_COMMAND"|grep'__prompt_command'>/dev/null;thencase"$TERM"in# adding in "screen" because tmuxxterm|xterm-color|xterm-256color|rxvt*|screen-256color|screen)if[-n"$PROMPT_COMMAND"];thenexportPROMPT_COMMAND="__prompt_command;${PROMPT_COMMAND}"elseexportPROMPT_COMMAND="__prompt_command"fi;;*)if[-n"$PROMPT_COMMAND"];thenexportPROMPT_COMMAND="__prompt_command;${PROMPT_COMMAND}"elseexportPROMPT_COMMAND="__prompt_command"fi;;esacfi# ----- HISTORY SUBMISSION IN $PROMPT_COMMAND ----- #if!printf"%b""$PROMPT_COMMAND"|grep'history -a'>/dev/null;thenif[-n"$PROMPT_COMMAND"];thenexportPROMPT_COMMAND="$PROMPT_COMMAND;history -a;history -c;history -r"elseexportPROMPT_COMMAND="history -a;history -c;history -r"fifi