szaydel
11/10/2012 - 4:16 AM

Prompt for fish with git statuses

Prompt for fish with git statuses

set -U fish_color_git_clean green
set -U fish_color_git_dirty red
set -U fish_color_git_ahead red
set -U fish_color_git_staged yellow

set -U fish_color_git_added green
set -U fish_color_git_modified blue
set -U fish_color_git_renamed magenta
set -U fish_color_git_deleted red
set -U fish_color_git_unmerged yellow
set -U fish_color_git_untracked cyan

set -U fish_prompt_git_status_added '✚'
set -U fish_prompt_git_status_modified '*'
set -U fish_prompt_git_status_renamed '➜'
set -U fish_prompt_git_status_deleted '✖'
set -U fish_prompt_git_status_unmerged '═'
set -U fish_prompt_git_status_untracked '.'

function fish_prompt_git --description 'Write out the git prompt'
  set -l branch (git symbolic-ref --quiet --short HEAD 2>/dev/null)
  if test -z $branch
    return
  end

  printf '|'

  set -l index (git status --porcelain 2>/dev/null)
  if test -z "$index"
    set_color $fish_color_git_clean
    printf $branch'✓'
    set_color normal
    return
  end

  git diff-index --quiet --cached HEAD 2>/dev/null
  set -l staged $status
  if test $staged = 1
    set_color $fish_color_git_staged
  else
    set_color $fish_color_git_dirty
  end

  printf $branch'⚡'

  set -l info
  for i in $index
    switch $i
      case 'A  *'
        set i added
      case 'M  *' ' M *'
        set i modified
      case 'R  *'
        set i renamed
      case 'D  *' ' D *'
        set i deleted
      case 'U  *'
        set i unmerged
      case '?? *'
        set i untracked
    end

    if not contains $i $info
      set info $info $i
    end
  end

  for i in added modified renamed deleted unmerged untracked
    if contains $i $info
      eval 'set_color $fish_color_git_'$i
      eval 'printf $fish_prompt_git_status_'$i
    end
  end

  set_color normal
end
set -U fish_color_user magenta
set -U fish_color_host yellow

function fish_prompt --description 'Write out the prompt'
  set -l last_status $status

  # User
  set_color $fish_color_user
  printf (whoami)
  set_color normal

  printf '@'

  # Host
  set_color $fish_color_host
  printf (hostname -s)
  set_color normal

  printf ':'

  # PWD
  set_color $fish_color_cwd
  printf (prompt_pwd)
  set_color normal

  fish_prompt_git
  # I prefer to have a new-line here but messes with the prompt when resizing
  echo

  if not test $last_status -eq 0
    set_color $fish_color_error
  end

  printf '➤ '
  set_color normal
end