b65e42d8447e3c3b91a00d4ddec329ed548ae59a
[profile.git] / .profile.d / ps1.bashrc
1 #!bash Coloured prompts.
2 # To use, add a call to __ps1 in your .bash_profile file.
3 #
4 # The prompt comprises multiple parts, some of which may be hidden by unsetting 
5 # shell variables or using the ``prompt'' function.
6 #
7 # The first part of the prompt is user@host where host is highlighted in 
8 # green if the last command exited 0 or in red otherwise.
9 # This part will be shown only if __ps1_user is 1.  By default it is 1.
10 # The success and failure colours can be changed by modifying 
11 # $PROMPT_OK_COLOUR and $PROMPT_FAILURE_COLOUR respectively.
12 #
13 # The second part of the prompt is taken from git-completion.bashrc.
14 # It is shown only if __ps1_git is 1.  By default it is 0.
15 #
16 # The third part of the prompt is taken from p4-completion.bashrc.
17 # It is shown only if __ps1_p4 is 1.  By default it is 0.
18 #
19 # The fourth part of the prompt is taken from svn-completion.bashrc.
20 # It is shown only if __ps1_svn is 1.  By default it is 0.
21 #
22 # The fifth part of the prompt is the exit status of the last command.
23 # This part will be shown only if __ps1_user is set and the exit status is 
24 # non-zero.
25
26 # The final part of the prompt is the (full) working directory and $ string.
27 #
28 # Colouring is performed by the __ps1_col() and __ps1_ret() functions.
29 # We redirect stderr to /dev/null when calling these functions to prevent 
30 # bash complaining about not knowing them when you su to another user, 
31 # retaining PS1 but not the function definitions.
32 #
33 # Note that $? is passed as an argument to - and is returned from - all 
34 # functions.  As $? is set following any shell activity it is only guaranteed 
35 # to represent the return code of the last command at the beginning of __ps1().
36 # By passing between subsequent functions we ensure that it is available for 
37 # __ps1_ret().
38 #
39
40 # Pick a colour based on the terminal capabilities.
41 # OK: dark green.
42 # Failed: dark red.
43 # Git: royal blue.
44 # P4: yellow.
45 # SVN: magenta.
46 case $(tput colors) in
47   256)
48     PROMPT_OK_COLOUR="1;38;5;34"
49     PROMPT_FAILED_COLOUR="1;38;5;160"
50     GIT_COLOUR="0;38;5;33"
51     SVN_COLOUR="0;38;5;127"
52     P4_COLOUR="0;38;5;142"
53   ;;
54
55   88)
56     PROMPT_OK_COLOUR="1;38;5;24"
57     PROMPT_FAILED_COLOUR="1;38;5;48"
58     GIT_COLOUR="0;38;5;23"
59     SVN_COLOUR="0;38;5;49"
60     P4_COLOUR="0;38;5;56"
61   ;;
62
63   *)
64     PROMPT_OK_COLOUR="1;32"
65     PROMPT_FAILED_COLOUR="1;31"
66     GIT_COLOUR="0;36"
67     SVN_COLOUR="0;35"
68     P4_COLOUR="0;33"
69   ;;
70 esac
71
72 function __ps1() {
73   # Default __ps1_user to 1.
74   [ -z "$__ps1_user" ] && __ps1_user=1
75
76   PS1='$(__ps1_user $? \u@)\[\033[$(__ps1_col $? 2>/dev/null)m\]$(__ps1_user $? \h)\[\033[$(__ps1_colour_escape $? $GIT_COLOUR)m\]$(__ps1_git $? 2>/dev/null)\[\033[0m\]\[\033[$(__ps1_colour_escape $? $P4_COLOUR)m\]$(__ps1_p4 $? 2>/dev/null)\[\033[0m\]\[\033[$(__ps1_colour_escape $? $SVN_COLOUR)m\]$(__ps1_svn $? 2>/dev/null)\[\033[0m\]$(__ps1_ret $? 2>/dev/null):\w\$ '
77   return 0
78 }
79
80 # iTerm doesn't like it if you set bold and colour at the same time.
81 function __ps1_colour_escape() {
82   local ret=$1; shift
83   local bold="${1%%;*}"
84   local colour="${1#*;}"
85
86   echo -en "${bold}m\033[$colour"
87   return $ret
88 }
89
90 function __ps1_col() {
91   local ret=$1; shift
92   if [ $ret -gt 0 ]; then
93     __ps1_colour_escape $ret "$PROMPT_FAILED_COLOUR"
94   else
95     __ps1_colour_escape $ret "$PROMPT_OK_COLOUR"
96   fi
97   return $ret
98 }
99
100 function __ps1_ret() {
101   [ "$__ps1_user" = "1" ] || return $1
102   [ $1 -gt 0 ] && echo -n " ($1)"
103   return $1
104 }
105
106 function __ps1_user() {
107   local ret=$1; shift
108   [ "$__ps1_user" = "1" ] || return $ret
109   echo -n ${1+"$@"}
110   return $ret
111 }
112
113 function __ps1_git() {
114   [ "$__ps1_git" = "1" ] || return $1
115   if [ "$__ps1_user" = "1" ]; then
116     __git_ps1 ' %s'
117   else
118     __git_ps1 '%s'
119   fi
120   return $1
121 }
122
123 function __ps1_p4() {
124   [ "$__ps1_p4" = "1" ] || return $1
125   if [ "$__ps1_user" = "1" -o "$__ps1_git" = "1" ]; then
126     __p4_ps1 ' %s'
127   else
128     __p4_ps1 '%s'
129   fi
130   return $1
131 }
132
133 function __ps1_svn() {
134   [ "$__ps1_svn" = "1" ] || return $1
135   if [ "$__ps1_user" = "1" -o "$__ps1_git" = "1" -o "$__ps1_p4" = "1" ]; then
136     __svn_ps1 ' %s'
137   else
138     __svn_ps1 '%s'
139   fi
140   return $1
141 }
142
143 function prompt() {
144   local blurb="Usage: prompt hide|show <what>"
145
146   if [ $# -lt 2 ]; then
147     echo >&2 "$blurb"
148     return 1
149   fi
150
151   action="$1"
152   if [ ! "$action" = "hide" -a ! "$action" = "show" ]; then
153     echo >&2 "$blurb"
154     return 1
155   fi
156   if [ "$action" = "hide" ]; then
157     action=0
158   else
159     action=1
160   fi
161
162   what="$(echo $2 | env LANG= LC_ALL= LC_CTYPE= tr '[:upper:]' '[:lower:]')"
163   eval __ps1_$what=$action
164 }