Generic handling of SSH_FORWARDED overrides.
[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 the exit status of the last command.
20 # This part will be shown only if __ps1_user is set and the exit status is 
21 # non-zero.
22
23 # The final part of the prompt is the (full) working directory and $ string.
24 #
25 # Colouring is performed by the __ps1_col() and __ps1_ret() functions.
26 # We redirect stderr to /dev/null when calling these functions to prevent 
27 # bash complaining about not knowing them when you su to another user, 
28 # retaining PS1 but not the function definitions.
29 #
30 # Note that $? is passed as an argument to - and is returned from - all 
31 # functions.  As $? is set following any shell activity it is only guaranteed 
32 # to represent the return code of the last command at the beginning of __ps1().
33 # By passing between subsequent functions we ensure that it is available for 
34 # __ps1_ret().
35 #
36
37 # Pick a colour based on the terminal capabilities.
38 # OK: dark green.
39 # Failed: dark red.
40 # Git: royal blue.
41 # P4: yellow.
42 case $(tput colors) in
43   256)
44     export PROMPT_OK_COLOUR="1;38;5;34"
45     export PROMPT_FAILED_COLOUR="1;38;5;160"
46     export GIT_COLOUR="0;38;5;33"
47     export P4_COLOUR="0;38;5;142"
48   ;;
49
50   88)
51     export PROMPT_OK_COLOUR="1;38;5;24"
52     export PROMPT_FAILED_COLOUR="1;38;5;48"
53     export GIT_COLOUR="0;38;5;23"
54     export P4_COLOUR="0;38;5;56"
55   ;;
56
57   *)
58     export PROMPT_OK_COLOUR="1;32"
59     export PROMPT_FAILED_COLOUR="1;31"
60     export GIT_COLOUR="0;36"
61     export P4_COLOUR="0;33"
62   ;;
63 esac
64
65 function __ps1() {
66   # Default __ps1_user to 1.
67   [ -z "$__ps1_user" ] && __ps1_user=1
68
69   export PS1='$(__ps1_user $? \u@)\[\033[$(__ps1_col $? 2>/dev/null)m\]$(__ps1_user $? \h)\[\033[${GIT_COLOUR}m\]$(__ps1_git $? 2>/dev/null)\[\033[0m\]\[\033[${P4_COLOUR}m\]$(__ps1_p4 $? 2>/dev/null)\[\033[0m\]$(__ps1_ret $? 2>/dev/null):\w\$ '
70   return 0
71 }
72
73 function __ps1_col() {
74   [ $1 -gt 0 ] && echo -n "$PROMPT_FAILED_COLOUR" || echo -n "$PROMPT_OK_COLOUR"
75   return $1
76 }
77
78 function __ps1_ret() {
79   [ "$__ps1_user" = "1" ] || return $1
80   [ $1 -gt 0 ] && echo -n " ($1)"
81   return $1
82 }
83
84 function __ps1_user() {
85   local ret=$1; shift
86   [ "$__ps1_user" = "1" ] || return $ret
87   echo -n ${1+"$@"}
88   return $ret
89 }
90
91 function __ps1_git() {
92   [ "$__ps1_git" = "1" ] || return $1
93   if [ "$__ps1_user" = "1" ]; then
94     __git_ps1 ' %s'
95   else
96     __git_ps1 '%s'
97   fi
98   return $1
99 }
100
101 function __ps1_p4() {
102   [ "$__ps1_p4" = "1" ] || return $1
103   if [ "$__ps1_user" = "1" -o "$__ps1_git" = "1" ]; then
104     __p4_ps1 ' %s'
105   else
106     __p4_ps1 '%s'
107   fi
108   return $1
109 }
110
111 function prompt() {
112   local blurb="Usage: prompt hide|show <what>"
113
114   if [ $# -lt 2 ]; then
115     echo >&2 "$blurb"
116     return 1
117   fi
118
119   action="$1"
120   if [ ! "$action" = "hide" -a ! "$action" = "show" ]; then
121     echo >&2 "$blurb"
122     return 1
123   fi
124   if [ "$action" = "hide" ]; then
125     action=0
126   else
127     action=1
128   fi
129
130   what="$(echo $2 | env LANG= LC_ALL= LC_CTYPE= tr '[:upper:]' '[:lower:]')"
131   eval __ps1_$what=$action
132 }