summary refs log tree commit diff
path: root/.zsh/gitprompt.zsh
blob: 0a420786b46a70ae500e7378708981dde9867ca7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Ported from http://sebastiancelis.com/2009/11/16/zsh-prompt-git-users/

function gitprompt_update
{
  unset __CURRENT_GIT_BRANCH
  unset __CURRENT_GIT_BRANCH_STATUS
  unset __CURRENT_GIT_BRANCH_IS_DIRTY

  local st="$(git status 2>/dev/null)"
  if [[ -n "$st" ]]; then
    local -a arr
    arr=(${(f)st})

    if [[ $arr[1] =~ 'Not currently on any branch.' ]]; then
      __CURRENT_GIT_BRANCH='no-branch'
    else
      __CURRENT_GIT_BRANCH="${arr[1][(w)4]}";
    fi

    if [[ $arr[2] =~ 'Your branch is' ]]; then
      if [[ $arr[2] =~ 'ahead' ]]; then
        __CURRENT_GIT_BRANCH_STATUS='ahead'
      elif [[ $arr[2] =~ 'diverged' ]]; then
        __CURRENT_GIT_BRANCH_STATUS='diverged'
      else
        __CURRENT_GIT_BRANCH_STATUS='behind'
      fi
    fi

    if [[ ! $st =~ 'nothing' ]]; then # Untracked files count as clean
      __CURRENT_GIT_BRANCH_IS_DIRTY='1'
    fi
  fi
}

# Changed around the formatting here a bunch
function gitprompt
{
  if [ -n "$__CURRENT_GIT_BRANCH" ]; then
    local s="%{${fg[yellow]}%}"
    if [ -n "$__CURRENT_GIT_BRANCH_IS_DIRTY" ]; then
      s+="⚡"
    else
      s+=":"
    fi
    s+="$__CURRENT_GIT_BRANCH"
    case "$__CURRENT_GIT_BRANCH_STATUS" in
      ahead)
        s+="↑"
        ;;
      diverged)
        s+="↕"
        ;;
      behind)
        s+="↓"
        ;;
    esac

    echo $s
  fi
}

function gitprompt_preexec
{
  case "$1" in
    g*) # Switched from git* to also detect my short aliases
      __EXECUTED_GIT_COMMAND=1
      ;;
  esac
}

function gitprompt_precmd
{
  if [ -n "$__EXECUTED_GIT_COMMAND" ]; then
    gitprompt_update
    unset __EXECUTED_GIT_COMMAND
  fi
}

typeset -ga preexec_functions
typeset -ga precmd_functions
typeset -ga chpwd_functions

preexec_functions+='gitprompt_preexec'
precmd_functions+='gitprompt_precmd'
chpwd_functions+='gitprompt_update'