summaryrefslogtreecommitdiff
path: root/app-shells/bash/files/bashrc.d/10-gentoo-color-r2.bash
blob: 847e76f8f186dd37de397abe16b647b8f35e6fa4 (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
# /etc/bash/bashrc.d/10-gentoo-color.bash

if [[ ${NO_COLOR} ]]; then
	# Respect the user's wish not to use color. See https://no-color.org/.
	gentoo_color=0
elif [[ ${COLORTERM@a} == *x* && ${COLORTERM} == @(24bit|truecolor) ]]; then
	# The COLORTERM environment variable can reasonably be trusted here.
	# See https://github.com/termstandard/colors for further information.
	gentoo_color=1
else
	# Check TERM against a whitelist covering a majority of popular
	# terminal emulators and virtual console implementations known to
	# support color. If no matching entry is found, try to use tput(1) to
	# determine whether color is supported.
	case ${TERM} in
		*color*    |\
		*direct*   |\
		*ghostty   |\
		[Ekx]term* |\
		alacritty  |\
		aterm      |\
		contour    |\
		dtterm     |\
		foot*      |\
		jfbterm    |\
		linux      |\
		mlterm     |\
		rxvt*      |\
		screen*    |\
		tmux*      |\
		wsvt25*    ) gentoo_color=1 ;;
		*          ) gentoo_color=$(tput colors 2>/dev/null)
	esac
fi

# For direxpand to be missing indicates that bash is lacking readline support.
if (( gentoo_color <= 0 )) || ( ! shopt -u direxpand 2>/dev/null ); then
	# Define a prompt without color.
	PS1='\u@\h \w \$ '
elif (( EUID == 0 )); then
	# If root, omit the username and print the hostname in red.
	PS1='\[\e[01;31m\]\h\[\e[01;34m\] \w \$\[\e[00m\] '
else
	# Otherwise, print the username and hostname in green.
	PS1='\[\e[01;32m\]\u@\h\[\e[01;34m\] \w \$\[\e[00m\] '
fi

if (( gentoo_color > 0 )); then
	# Colorize the output of diff(1), grep(1) and a few coreutils utilities.
	# However, do so only where no alias/function by the given name exists.
	for _ in diff dir grep ls vdir; do
		if [[ $(type -t "$_") == file ]]; then
			alias "$_=$_ --color=auto"
		fi
	done

	# Enable colors for ls(1) and some other utilities that respect the
	# LS_COLORS variable. Prefer ~/.dir_colors, per bug #64489.
	if hash dircolors 2>/dev/null; then
		if [[ -f ~/.dir_colors ]]; then
			eval "$(COLORTERM=1 dircolors -b -- ~/.dir_colors)"
		elif [[ -f /etc/DIR_COLORS ]]; then
			eval "$(COLORTERM=1 dircolors -b /etc/DIR_COLORS)"
		else
			eval "$(COLORTERM=1 dircolors -b)"
		fi
	fi
fi

unset -v gentoo_color