paullongtine

bash prompt

Jul 3rd, 2013
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.52 KB | None | 0 0
  1. # ANSI CODES - SEPARATE MULTIPLE VALUES WITH ;
  2. #
  3. #  0  reset          4  underline
  4. #  1  bold           7  inverse
  5. #
  6. # FG  BG  COLOR     FG  BG  COLOR
  7. # 30  40  black     34  44  blue
  8. # 31  41  red       35  45  magenta
  9. # 32  42  green     36  46  cyan
  10. # 33  43  yellow    37  47  white
  11.  
  12. if [[ ! "${prompt_colors[@]}" ]]; then
  13.   prompt_colors=(
  14.     "1;31" # information color
  15.     "1;30" # bracket color
  16.     "31" # error color
  17.   )
  18.  
  19.   if [[ "$SSH_TTY" ]]; then
  20.     # connected via ssh
  21.     prompt_colors[1]="1;33"
  22.   elif [[ "$USER" == "root" ]]; then
  23.     # logged in as root
  24.     prompt_colors[1]="1;44"
  25.   fi
  26. fi
  27.  
  28. # Inside a prompt function, run this alias to setup local $c0-$c9 color vars.
  29. alias prompt_getcolors='prompt_colors[9]=; local i; for i in ${!prompt_colors[@]}; do local c$i="\[\e[0;${prompt_colors[$i]}m\]"; done'
  30.  
  31. # Git status.
  32. function prompt_git() {
  33.   prompt_getcolors
  34.   local status output flags
  35.   status="$(git status 2>/dev/null)"
  36.   [[ $? != 0 ]] && return;
  37.   output="$(echo "$status" | awk '/# Initial commit/ {print "(init)"}')"
  38.  [[ "$output" ]] || output="$(echo "$status" | awk '/# On branch/ {print $4}')"
  39.   [[ "$output" ]] || output="$(git branch | perl -ne '/^\* (.*)/ && print $1')"
  40.   flags="$(
  41.    echo "$status" | awk 'BEGIN {r=""} \
  42.      /^# Changes to be committed:$/        {r=r "+"}\
  43.      /^# Changes not staged for commit:$/  {r=r "!"}\
  44.      /^# Untracked files:$/                {r=r "?"}\
  45.      END {print r}'
  46.  )"
  47.   if [[ "$flags" ]]; then
  48.     output="$output$c1:$c0$flags"
  49.   fi
  50.   echo "$c1-$c0$output$c1$c9"
  51. }
  52. # Maintain a per-execution call stack.
  53. prompt_stack=()
  54. trap 'prompt_stack=("${prompt_stack[@]}" "$BASH_COMMAND")' DEBUG
  55.  
  56. function prompt_command() {
  57.   local exit_code=$?
  58.   # If the first command in the stack is prompt_command, no command was run.
  59.   # Set exit_code to 0 and reset the stack.
  60.   [[ "${prompt_stack[0]}" == "prompt_command" ]] && exit_code=0
  61.   prompt_stack=()
  62.  
  63.   # Manually load z here, after $? is checked, to keep $? from being clobbered.
  64.   [[ "$(type -t _z)" ]] && _z --add "$(pwd -P 2>/dev/null)" 2>/dev/null
  65.  
  66.   # While the simple_prompt environment var is set, disable the awesome prompt.
  67.   [[ "$simple_prompt" ]] && PS1='\n$ ' && return
  68.  
  69.   prompt_getcolors
  70.   PS1="\n"
  71.   # misc: [cmd#]
  72.   #PS1="$PS1$c1[$c0#\#$c1]$c9"
  73.   # name: user@host:path
  74.   PS1="$PS1\[\033[0;36m\]\u$c1@\[\033[0;36m\]\h$c1:\[\033[0;37m\]\w$c9"
  75.   # git: [branch:flags]
  76.   PS1="$PS1$(prompt_git)"
  77.   PS1="$PS1 $c0\$\[\033[0;37m\] "
  78. }
  79.  
  80. PROMPT_COMMAND="prompt_command"
Advertisement
Add Comment
Please, Sign In to add comment