]> code.delx.au - dotfiles/blob - .bash/colors
bash: print exit code on failure
[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 [ -r /usr/share/git/completion/git-prompt.sh ] && source /usr/share/git/completion/git-prompt.sh
62 function my_git_ps1 {
63 find_up_recurse .git || return
64 GIT_PS1_SHOWDIRTYSTATE=1 \
65 GIT_PS1_SHOWUNTRACKEDFILES=1 \
66 __git_ps1 2> /dev/null
67 }
68
69 function my_hg_ps1 {
70 find_up_recurse .hg || return
71 b="$(hg branch 2>/dev/null)" || return
72 s="$(hg status | cut -c1 | sort -u | tr -d " \n")"
73 echo -n " ($b"
74 [ -n "$s" ] && echo -n " $s"
75 echo -n ")"
76 }
77
78 function my_svn_ps1 {
79 find_up_recurse .svn || return
80 s="$(svn status --ignore-externals 2>/dev/null | cut -c1 | sort -u | tr -d " \n")"
81 [ -z "$s" ] && return
82 echo -n " ($s)"
83 }
84
85 # Set the prompt colour, and the colors for the 'ls' command appropriately,
86 # depending on the background of the terminal.
87 if [ ${PS1_COLOR:-0} -eq 1 ]; then
88 eval $(TERM=xterm dircolors 2> /dev/null)
89
90 if [ -z "$debian_chroot" -a -r /etc/debian_chroot ]; then
91 debian_chroot=$(cat /etc/debian_chroot)
92 fi
93 PS1='${debian_chroot:+($debian_chroot)}'
94 PS1="$PS1"'\[\033[00;31m\]\u@\h\[\033[00m\]:\[\033[00;34m\]\w\[\033[00m\]'
95 PS1="$PS1"'\[\033[00;36m\]$(my_git_ps1 ; my_hg_ps1 ; my_svn_ps1)\[\033[00m\]'
96 PS1="$PS1"'\n\$ '
97
98 if [ ${DARK} -eq 0 ]; then
99 LS_COLORS="$(echo "${LS_COLORS}" | sed 's/01;/00;/g')"
100 LSCOLORS="exfxcxdxbxegedabagacad" # BSD ls
101 else
102 # We need to bold the LS_COLORS and PS1 environment variables
103 # so they show up on a dark terminal
104 LS_COLORS="$(echo "${LS_COLORS}" | sed 's/00;/01;/g')"
105 LSCOLORS="ExFxCxDxBxEGEDABAGACAD" # BSD ls
106 PS1="$(echo "${PS1}" | sed 's/00;/01;/g')"
107 fi
108
109 export LS_COLORS
110 export LSCOLORS
111 fi
112 unset PS1_COLOR
113 unset DARK
114