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