diff options
author | mail_redacted_for_web | 2022-03-19 19:27:36 +0100 |
---|---|---|
committer | mail_redacted_for_web | 2022-03-19 19:27:36 +0100 |
commit | 53b55b4b10ef6344a882402d5dcaf11d87b78d3f (patch) | |
tree | a4c8f2248fe0ef17d2c7cd98a62adfc5f0a2a7ca /.bash/prompt.bash | |
download | dotfiles-53b55b4b10ef6344a882402d5dcaf11d87b78d3f.tar.bz2 |
Initial commit
Diffstat (limited to '.bash/prompt.bash')
-rw-r--r-- | .bash/prompt.bash | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/.bash/prompt.bash b/.bash/prompt.bash new file mode 100644 index 0000000..a78f2ed --- /dev/null +++ b/.bash/prompt.bash @@ -0,0 +1,129 @@ +#!/usr/bin/env bash +# vim:syntax=sh:ts=4 + +# ----- PROMPT COLOURS ----- # +fgcolor () { + #echo "\\[\\033[38;5;"$1"m\\]" + echo "\033[38;5;${1}m" +} +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')";export MYUID +# ...because "empty" will hit the "any" condition in the end :) +case "$MYUID" in + 0) + case "$OSNAME" in + debian) FGC1="fgcolor 88";FGC2="fgcolor 25";FGC3="fgcolor 38";; + arch) FGC1="fgcolor 88";FGC2="fgcolor 24";FGC3="fgcolor 240";; + redhat|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) FGC1="fgcolor 88";FGC2="fgcolor 22";FGC3="fgcolor 41";; + *sles*) FGC1="fgcolor 88";FGC2="fgcolor 22";FGC3="fgcolor 83";; + *suse|aix) FGC1="fgcolor 88";FGC2="fgcolor 30";FGC3="fgcolor 83";; + *centos*) FGC1="fgcolor 88";FGC2="fgcolor 25";FGC3="fgcolor 208";; + *solaris) FGC1="fgcolor 88";FGC2="fgcolor 31";FGC3="fgcolor 124";; + ubuntu) FGC1="fgcolor 88";FGC2="fgcolor 130";FGC3="fgcolor 215";; + rasp*) FGC1="fgcolor 88";FGC2="fgcolor 253";FGC3="fgcolor 126";; + rocky) FGC1="fgcolor 88";FGC2="fgcolor 22";FGC3="fgcolor 41";; + *) FGC1="fgcolor 88";FGC2="fgcolor 240";FGC3="fgcolor 244";; + esac + ;; + *) + case "$OSNAME" in + debian) FGC1="fgcolor 25";FGC2="fgcolor 253";FGC3="fgcolor 38";; + arch) FGC1="fgcolor 24";FGC2="fgcolor 253";FGC3="fgcolor 239";; + redhat|fedora) FGC1="fgcolor 88";FGC2="fgcolor 253";FGC3="fgcolor 203";; + #*suse|aix) FGC1="fgcolor 22";FGC2="fgcolor 253";FGC3="fgcolor 41";; + *sles*) FGC1="fgcolor 22";FGC2="fgcolor 253";FGC3="fgcolor 83";; + *suse|aix) FGC1="fgcolor 30";FGC2="fgcolor 253";FGC3="fgcolor 83";; + *centos*) FGC1="fgcolor 25";FGC2="fgcolor 253";FGC3="fgcolor 208";; + *solaris) FGC1="fgcolor 31";FGC2="fgcolor 253";FGC3="fgcolor 124";; + ubuntu) FGC1="fgcolor 130";FGC2="fgcolor 253";FGC3="fgcolor 215";; + rasp*) FGC1="fgcolor 53";FGC2="fgcolor 253";FGC3="fgcolor 126";; + rocky) FGC1="fgcolor 22";FGC2="fgcolor 253";FGC3="fgcolor 41";; + *) FGC1="fgcolor 240";FGC2="fgcolor 253";FGC3="fgcolor 244";; + esac + ;; +esac +# make it possible to draw user-defined colours +# shellcheck disable=SC1090 +[ -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() { + local EXIT="$?" + if [ "$EXIT" -lt 10 ];then + local EXIT2="00$EXIT" + elif [ "$EXIT" -lt 100 ];then + local EXIT2="0$EXIT" + else + local EXIT2="$EXIT" + fi + case "$MYUID" in + 0) + PS1="\\# \[$(tput bold)\]\[$($FGC1)\]\\u\[$($FGC2)\]@$PHN \[$($FGC3)\]\\w \[$(tput sgr0)\]" + ;; + *) + PS1="\\# \[$(tput bold)\]\[$($FGC1)\]\t \[$($FGC2)\]\\u$UHSEP\[$($FGC3)\]$PHN \\w \[$(tput sgr0)\]" + ;; + esac + if command -v __git_ps1 >/dev/null 2>&1;then PS1+="$(__git_ps1 "%s ")";fi + if [ "$EXIT" -eq 0 ];then + PS1+="\[\033[0;32m\]# " + elif [ "$EXIT" -eq 255 ]||[ "$EXIT" -eq 127 ];then + PS1+="\[\033[0;33m\]$EXIT2 # " + elif [ "$EXIT" -eq 130 ];then + PS1+="\[\033[0;35m\]$EXIT2 # " + else + PS1+="\[\033[0;31m\]$EXIT2 # " + fi + PS1+="\[\033[0m\]" +} + +# ----- ADD __prompt_command() TO $PROMPT_COMMAND ----- # +if ! printf "%b" "$PROMPT_COMMAND" | grep '__prompt_command' >/dev/null;then + case "$TERM" in + # adding in "screen" because tmux + xterm|xterm-color|xterm-256color|rxvt*|screen-256color|screen) + if [ -n "$PROMPT_COMMAND" ];then + export PROMPT_COMMAND="__prompt_command;${PROMPT_COMMAND}" + else + export PROMPT_COMMAND="__prompt_command" + fi + ;; + *) + if [ -n "$PROMPT_COMMAND" ];then + export PROMPT_COMMAND="__prompt_command;${PROMPT_COMMAND}" + else + export PROMPT_COMMAND="__prompt_command" + fi + ;; + esac +fi +# ----- ADD SSH ENV FILE TO PROMPT_COMMAND ----- # +if [ -r "$SSHENVFILE" ];then + if ! printf "%b" "$PROMPT_COMMAND" | grep "source $SSHENVFILE" >/dev/null;then + # we don't need to check whether PROMPT_COMMAND is set as after the paragraph + # above, something will be definitely set. + PROMPT_COMMAND="${PROMPT_COMMAND};source $SSHENVFILE" + fi +fi +# ----- HISTORY SUBMISSION IN $PROMPT_COMMAND ----- # +if ! printf "%b" "$PROMPT_COMMAND"|grep 'history -a' >/dev/null;then + if [ -n "$PROMPT_COMMAND" ];then + export PROMPT_COMMAND="$PROMPT_COMMAND;history -a;history -c;history -r" + else + export PROMPT_COMMAND="history -a;history -c;history -r" + fi +fi |