git.lirion.de

Of git, get, and gud

aboutsummaryrefslogtreecommitdiffstats
path: root/.bash/prompt.bash
blob: 8c4dbaadceff6fdfe8302075b3abcdaafafaf5eb (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/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 :)
OSMAJVER="$(grep '^VERSION_ID' /etc/os-release|awk -F= '{print $NF}'|sed 's/"//g'|awk -F\. '{print $1}')"
case "$MYUID" in
	0)
		FGC1="fgcolor 88"
		case "$OSNAME" in
			debian)
				# Debian 12 = emerald
				if [ "$OSMAJVER" -ge 12 ];then
							FGC3="fgcolor 36";FGC2="fgtcolor 155 15 64"
				else
							FGC3="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" -ge 22 ];then
					if printf "%b" "$TERM"|grep -P 'xterm|256' > /dev/null;then
							FGC2="fgcolor 89";FGC3="fgcolor 208"
					else
							FGC2="fgcolor 53";FGC3="fgcolor 184"
					fi
				else
							FGC2="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";;
		esac
		if [ -n "$TERMUX_APK_RELEASE" ]; then
			if [ "$TERMUX_APK_RELEASE" == "F_DROID" ]; then
				FGC2="fgcolor 27";FGC3="fgcolor 35"
			fi
		fi
	;;
	*)
		FGC2="fgcolor 253"
		case "$OSNAME" in
			debian)
				if [ "$OSMAJVER" -ge 12 ];then
							FGC1="fgtcolor 155 15 64";FGC3="fgcolor 36"
				else
							FGC1="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" -ge 22 ];then
					if printf "%b" "$TERM"|grep -P 'xterm|256' > /dev/null;then
							FGC1="fgcolor 89";FGC3="fgcolor 208"
					else
							FGC1="fgcolor 53";FGC3="fgcolor 184"
					fi
				else
							FGC1="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";;
		esac
		if [ -n "$TERMUX_APK_RELEASE" ]; then
			if [ "$TERMUX_APK_RELEASE" == "F_DROID" ]; then
				FGC1="fgcolor 27";FGC3="fgcolor 35"
			fi
		fi
	;;
esac
# HYPERVISOR LAYER 8 SOFTWARE
if pveversion > /dev/null 2>&1;then
	FGC1="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() {
	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)\]\\n"
		;;
		*)
			PS1="\\# \[$(tput bold)\]\[$($FGC1)\]\t \[$($FGC2)\]\\u$UHSEP\[$($FGC3)\]$PHN \\w \[$(tput sgr0)\]\\n"
		;;
	esac
	# git-completion. Sources:
	# - https://github.com/git/git/tree/master/contrib/completion (main)
	# - https://github.com/felipec/git-completion (fork)
	# - Debian: easygit (outdated! you may want to use something like /git-prompt inside this repo)
	# - 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.sh
	if command -v __git_ps1 >/dev/null 2>&1;then
		GIT_PS1_SHOWCOLORHINTS=1
		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