]> code.delx.au - dotfiles/blob - .bash/colors
8df95df3a9b66ce1deaad0c20cd3563eb738b52b
[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=1
17
18
19 # Terminals that we want coloured prompts in
20 [ -n "${COLORTERM}" ] && PS1_COLOR=1
21 [ "${TERM}" = "linux" ] && DARK="${DARK:-1}"
22 [ "${TERM}" = "screen.linux" ] && DARK="${DARK:-1}"
23 [ "${TERM}" = "putty" ] && DARK="${DARK:-1}"
24 [ "${TERM}" = "cygwin" ] && DARK="${DARK:-1}"
25 [ "$(tput colors)" -gt 2 ] && PS1_COLOR=1
26
27 # Override COLORFGBG (probably used the darkterm or lightterm function
28 if [ -n "${DARK}" ]; then
29 unset COLORFGBG
30 else
31 DARK="${DEFAULTDARK}"
32 fi
33 unset DEFAULTDARK
34
35 # If COLORFGBG is set, use it to determine the terminal type, DARK=0 is
36 # dark on light, DARK=1 is light on dark.
37 if [ -n "${COLORFGBG}" ]; then
38 BG="$(echo "$COLORFGBG" | sed 's/.*;//')"
39 if ! [ "${BG}" -ge 0 ]; then
40 if [ "${BG}" = "white" ]; then
41 DARK=0
42 else
43 DARK=1
44 fi
45 elif [ "${BG}" -ge 0 -a "${BG}" -le 6 -o "${BG}" -eq 8 ]; then
46 DARK=1
47 else
48 DARK=0
49 fi
50 unset BG
51 else
52 # Otherwise we just do our best based on the setting of DARK
53 if [ ${DARK} -eq 0 ]; then
54 export COLORFGBG="0;15"
55 else
56 export COLORFGBG="15;0"
57 fi
58 fi
59
60
61 function my_git_ps1 {
62 find_up_recurse .git || return
63 GIT_PS1_SHOWDIRTYSTATE=1 \
64 GIT_PS1_SHOWUNTRACKEDFILES=1 \
65 __git_ps1 2> /dev/null
66 }
67
68 function my_hg_ps1 {
69 find_up_recurse .hg || return
70 b="$(hg branch 2>/dev/null)" || return
71 s="$(hg status | cut -c1 | sort -u | tr -d " \n")"
72 echo -n " ($b"
73 [ -n "$s" ] && echo -n " $s"
74 echo -n ")"
75 }
76
77 function my_svn_ps1 {
78 find_up_recurse .svn || return
79 s="$(svn status --ignore-externals 2>/dev/null | cut -c1 | sort -u | tr -d " \n")"
80 [ -z "$s" ] && return
81 echo -n " ($s)"
82 }
83
84 # Set the prompt colour, and the colors for the 'ls' command appropriately,
85 # depending on the background of the terminal.
86 if [ ${PS1_COLOR:-0} -eq 1 ]; then
87 eval $(TERM=xterm dircolors 2> /dev/null)
88
89 if [ -z "$debian_chroot" -a -r /etc/debian_chroot ]; then
90 debian_chroot=$(cat /etc/debian_chroot)
91 fi
92 PS1='${debian_chroot:+($debian_chroot)}'
93 PS1="$PS1"'\[\033[00;31m\]\u@\h\[\033[00m\]:\[\033[00;34m\]\w\[\033[00m\]'
94 PS1="$PS1"'\[\033[00;36m\]$(my_git_ps1 ; my_hg_ps1 ; my_svn_ps1)\[\033[00m\]'
95 PS1="$PS1"'\n\$ '
96
97 if [ ${DARK} -eq 0 ]; then
98 LS_COLORS="$(echo "${LS_COLORS}" | sed 's/01;/00;/g')"
99 LSCOLORS="exfxcxdxbxegedabagacad" # BSD ls
100 else
101 # We need to bold the LS_COLORS and PS1 environment variables
102 # so they show up on a dark terminal
103 LS_COLORS="$(echo "${LS_COLORS}" | sed 's/00;/01;/g')"
104 LSCOLORS="ExFxCxDxBxEGEDABAGACAD" # BSD ls
105 PS1="$(echo "${PS1}" | sed 's/00;/01;/g')"
106 fi
107
108 export LS_COLORS
109 export LSCOLORS
110 fi
111 unset PS1_COLOR
112 unset DARK
113