about summary refs log tree commit diff stats
path: root/bash/prompt.bash
blob: 06ca63843f58d7169ee151b3bdace52bcc462107 (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
# bash prompt

if [[ -n "$INSIDE_EMACS" ]]; then
	PS1="\n\[\e[34m\]\$ \[\e[0m\]"
else
	PS1=

	# user, host, and cwd
	PROMPT_DIRTRIM=3 # how many dirs above current to print (rest are '...')
	PS1+='\[\e[0;46m\]\u@\h \w'

	# git bit
	# see https://unix.stackexchange.com/questions/278206
	possible_git_prompt_locations=(
		/usr/share/git/git-prompt.sh                         # Arch, etc. (default?)
		/usr/lib/git-core/git-sh-prompt                      # Debian, Ubuntu ...
		/usr/share/git-core/contrib/completion/git-prompt.sh # Fedora ??
		# I have yet to find Alpine's git prompt location.
	)

	for file in "${possible_git_prompt_locations[@]}"; do
		if [[ -f "$file" ]]; then
			source "$file" &&
				PS1+='\[\e[35m\]$(__git_ps1)'
			break
		fi
	done

	# newline
	PS1+='\[\e[0m\]\n'

	# exit code (only if error)
	__prompt_exit_code() {
		local ec=$?
		(($ec > 0)) &&
			printf "$ec"
	}
	PS1+='\[\e[31m\]$(__prompt_exit_code)\[\e[0m\]'

	# terminal title (current directory)
	__prompt_term_title() {
		printf '\033]0;%s\007\n' "$*"
	}
	PS1+='\[$(__prompt_term_title "$USER@$(hostname): $PWD")\]'

	# delimiter
	PS1+='; '
fi