Gitify the working tree.
[profile.git] / .profile.d / ps1.bashrc
1 # Coloured prompts for bash.
2 #
3 # Prompt is user@host:/dir$ where host is highlighted in green if the last 
4 # command exited 0 or in red (followed by the error code) otherwise.
5 #
6 # Colouring is performed by the __ps1_col() and __ps1_ret() functions.
7 # We redirect stderr to /dev/null when calling these functions to prevent 
8 # bash complaining about not knowing them when you su to another user, 
9 # retaining PS1 but not the function definitions.
10 #
11 # To use, add a call to __ps1 in your .bash_profile file.
12 #
13
14 case $(tput colors) in
15   256)
16     export PROMPT_OK_COLOUR="1;38;5;34"
17     export PROMPT_FAILED_COLOUR="1;38;5;160"
18   ;;
19
20   88)
21     export PROMPT_OK_COLOUR="1;38;5;24"
22     export PROMPT_FAILED_COLOUR="1;38;5;48"
23   ;;
24
25   *)
26     export PROMPT_OK_COLOUR="1;32"
27     export PROMPT_FAILED_COLOUR="1;31"
28   ;;
29 esac
30
31 function __ps1() {
32   export PS1='\u@\[\033[$(__ps1_col $?)m\]\h\[\033[0m\]$(__ps1_ret $?):\w\$ '
33   export PS1='\u@\[\033[$(__ps1_col $? 2>/dev/null)m\]\h\[\033[0m\]$(__ps1_ret $? 2>/dev/null):\w\$ '
34   return 0
35 }
36
37 function __ps1_col() {
38   [ $1 -gt 0 ] && echo -n "$PROMPT_FAILED_COLOUR" || echo -n "$PROMPT_OK_COLOUR"
39   return $1
40 }
41
42 function __ps1_ret() {
43   [ $1 -gt 0 ] && echo -n " ($1)"
44   return 0
45 }