git.lirion.de

Of git, get, and gud

aboutsummaryrefslogtreecommitdiffstats
path: root/.bash/prompt.bash
blob: 12b1a6ead55bd39c5aa5ffd52668bbededb94506 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/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')";export MYUID
#   ...because "empty" will hit the "any" condition in the end :)
case "$MYUID" in
	0)
		FGC1="fgcolor 88"
		case "$OSNAME" in
			debian)         FGC3="fgcolor 38";FGC2="fgtcolor 155 15 64";;
			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)			FGC2="fgcolor 30";FGC3="fgcolor 83";;
			aix)			FGC2="fgcolor 238";FGC3="fgcolor 83";;
			*centos*)		FGC2="fgcolor 25";FGC3="fgcolor 208";;
			*solaris)		FGC2="fgcolor 31";FGC3="fgcolor 124";;
			ubuntu)			FGC2="fgcolor 130";FGC3="fgcolor 215";;
			rasp*)			FGC2="fgcolor 253";FGC3="fgcolor 126";;
			rocky)			FGC2="fgcolor 22";FGC3="fgcolor 41";;
			*)				FGC2="fgcolor 240";FGC3="fgcolor 244";;
		esac
	;;
	*)
		FGC2="fgcolor 253"
		case "$OSNAME" in
			debian)         FGC1="fgtcolor 155 15 64";FGC3="fgcolor 38";;
			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)			FGC1="fgcolor 30";FGC3="fgcolor 83";;
			aix)			FGC1="fgcolor 238";FGC3="fgcolor 83";;
			*centos*)		FGC1="fgcolor 25";FGC3="fgcolor 208";;
			*solaris)		FGC1="fgcolor 31";FGC3="fgcolor 124";;
			ubuntu)			FGC1="fgcolor 130";FGC3="fgcolor 215";;
			rasp*)			FGC1="fgcolor 53";FGC3="fgcolor 126";;
			rocky)			FGC1="fgcolor 22";FGC3="fgcolor 41";;
			*)				FGC1="fgcolor 240";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
# ----- 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