# Coloured prompts for bash. # # Prompt is user@host:/dir$ where host is highlighted in green if the last # command exited 0 or in red (followed by the error code) otherwise. # # Colouring is performed by the __ps1_col() and __ps1_ret() functions. # We redirect stderr to /dev/null when calling these functions to prevent # bash complaining about not knowing them when you su to another user, # retaining PS1 but not the function definitions. # # To use, add a call to __ps1 in your .bash_profile file. # case $(tput colors) in 256) export PROMPT_OK_COLOUR="1;38;5;34" export PROMPT_FAILED_COLOUR="1;38;5;160" ;; 88) export PROMPT_OK_COLOUR="1;38;5;24" export PROMPT_FAILED_COLOUR="1;38;5;48" ;; *) export PROMPT_OK_COLOUR="1;32" export PROMPT_FAILED_COLOUR="1;31" ;; esac function __ps1() { export PS1='\u@\[\033[$(__ps1_col $?)m\]\h\[\033[0m\]$(__ps1_ret $?):\w\$ ' export PS1='\u@\[\033[$(__ps1_col $? 2>/dev/null)m\]\h\[\033[0m\]$(__ps1_ret $? 2>/dev/null):\w\$ ' return 0 } function __ps1_col() { [ $1 -gt 0 ] && echo -n "$PROMPT_FAILED_COLOUR" || echo -n "$PROMPT_OK_COLOUR" return $1 } function __ps1_ret() { [ $1 -gt 0 ] && echo -n " ($1)" return 0 }