]> code.delx.au - dotfiles/blob - .bash/colors
Better colour detection
[dotfiles] / .bash / colors
1 #! bash
2
3 # This file is responsible for setting up the pretty colours in GNU 'ls' and
4 # the bash prompt. Depending on whether you use a light on dark, or dark on
5 # light terminal you want different colour schemes. Rxvt and some other
6 # set the COLORFGBG environment variable to make autodetecting this easy.
7
8 # These scripts attempt to detect the terminal colours, and then set COLORFGBG
9 # if it isn't already set so that programs like vim can set the correct colours.
10
11 # If your terminal isn't detected correctly then you can run lightterm or
12 # darkterm to get the colours right. The default can be adjusted below.
13
14 # Set to 0 for light background terminal colours by default
15 # Set to 1 for dark background terminal colours by default
16 DEFAULTDARK=0
17
18
19 # Terminals that we want coloured prompts in
20 [ -n "${COLORTERM}" ] && PS1_COLOR=1
21 [ "${TERM}" = "linux" ] && DARK="${DARK:-1}"
22 [ "$(tput colors)" -gt 2 ] && PS1_COLOR=1
23
24 # Override COLORFGBG (probably used the darkterm or lightterm function
25 if [ -n "${DARK}" ]; then
26 unset COLORFGBG
27 else
28 DARK="${DEFAULTDARK}"
29 fi
30 unset DEFAULTDARK
31
32 # If COLORFGBG is set, use it to determine the terminal type, DARK=0 is
33 # dark on light, DARK=1 is light on dark.
34 if [ -n "${COLORFGBG}" ]; then
35 FGBG="$(echo $COLORFGBG | sed 's/;.*;/;/')" # Allow for rxvt without xpm
36 FG=$(echo "${FGBG}" | cut -d ';' -f 1)
37 BG=$(echo "${FGBG}" | cut -d ';' -f 2)
38 if [ "${FG}" = "white" -o "${FG}" = "green" ]; then
39 DARK=1
40 elif [ "${BG}" = "black" ]; then
41 DARK=1
42 elif [ "${FG}" = "black" ]; then
43 DARK=0
44 elif [ "${BG}" = "white" ]; then
45 DARK=0
46 elif [ "${FG}" -gt "${BG}" ]; then
47 DARK=1
48 elif [ "${FG}" -lt "${BG}" ]; then
49 DARK=0
50 fi
51 unset FG
52 unset BG
53 unset FGBG
54 else
55 # Otherwise we just do our best based on the setting of DARK
56 if [ ${DARK} -eq 0 ]; then
57 export COLORFGBG="0;15"
58 else
59 export COLORFGBG="15;0"
60 fi
61 fi
62
63 # Set the prompt colour, and the colors for the 'ls' command appropriately,
64 # depending on the background of the terminal.
65 if [ ${PS1_COLOR:-0} -eq 1 ]; then
66 eval $(dircolors 2> /dev/null)
67 PS1='\[\033[00;31m\]\u@\h\[\033[00m\]:\[\033[00;34m\]\w\[\033[00m\]\$ '
68
69 if [ ${DARK} -eq 0 ]; then
70 LS_COLORS="$(echo "${LS_COLORS}" | sed 's/01;/00;/g')"
71 else
72 # We need to bold the LS_COLORS and PS1 environment variables
73 # so they show up on a dark terminal
74 LS_COLORS="$(echo "${LS_COLORS}" | sed 's/00;/01;/g')"
75 PS1="$(echo "${PS1}" | sed 's/00;/01;/g')"
76 fi
77
78 export LS_COLORS
79 fi
80 unset PS1_COLOR
81 unset DARK
82