-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompt.zsh
92 lines (73 loc) · 2.86 KB
/
prompt.zsh
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
87
88
89
90
91
92
# zsh prompt
autoload -Uz add-zsh-hook
VCS_SYMBOL_BRANCH=""
VCS_SYMBOL_AHEAD=""
VCS_SYMBOL_BEHIND=""
VCS_SYMBOL_STAGED="●"
VCS_SYMBOL_CONFLICTS=""
VCS_SYMBOL_UNSTAGED=""
VCS_SYMBOL_UNTRACKED=""
VCS_SYMBOL_STASHED=""
VCS_SYMBOL_CLEAN='✔'
# git info
vcs_prompt_info() {
# check if in git directory
if git rev-parse --is-inside-work-tree 2>/dev/null | grep -q 'true'; then
# set all variables
VCS_INFO_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
_VCS_INFO_COMMIT_STATUS=$(git for-each-ref --format="%(push:track)" refs/heads/$VCS_INFO_BRANCH | awk '{gsub(/\[|]|,/,""); print}')
_VCS_INFO_STATUS=$(git status --porcelain 2>/dev/null)
VCS_INFO_AHEAD=$(echo $_VCS_INFO_COMMIT_STATUS | awk '{for(i=1;i<=NF;i++) if ($i=="ahead") print $(i+1)}')
VCS_INFO_BEHIND=$(echo $_VCS_INFO_COMMIT_STATUS | awk '{for(i=1;i<=NF;i++) if ($i=="behind") print $(i+1)}')
VCS_INFO_STAGED=$(git diff --cached --numstat 2>/dev/null | wc -l | tr -d ' ')
VCS_INFO_UNSTAGED=$(git diff --name-status 2>/dev/null | wc -l | tr -d ' ')
VCS_INFO_UNTRACKED=$(echo "${_VCS_INFO_STATUS}" | grep "^??" | wc -l | tr -d ' ')
VCS_INFO_CONFLICTS=$(git ls-files --unmerged 2>/dev/null | wc -l | tr -d ' ')
VCS_INFO_STASHED=$(git rev-list --walk-reflogs --count refs/stash 2>/dev/null)
# add indicators to prompt
VCS_INFO="%F{green}${VCS_SYMBOL_BRANCH} %F{cyan}${VCS_INFO_BRANCH}%f "
if [ ! -z "$VCS_INFO_AHEAD" ]; then
VCS_INFO+="${VCS_SYMBOL_AHEAD} ${VCS_INFO_AHEAD} "
fi
if [ ! -z "$VCS_INFO_BEHIND" ]; then
VCS_INFO+="${VCS_SYMBOL_BEHIND} ${VCS_INFO_BEHIND} "
fi
if [ "$VCS_INFO_STAGED" -ne "+0" ]; then
VCS_INFO+="%F{green}${VCS_SYMBOL_STAGED} ${VCS_INFO_STAGED}%f "
fi
if [ "$VCS_INFO_UNSTAGED" -ne "+0" ]; then
VCS_INFO+="%F{yellow}${VCS_SYMBOL_UNSTAGED} ${VCS_INFO_UNSTAGED}%f "
fi
if [ "$VCS_INFO_UNTRACKED" -ne "+0" ]; then
VCS_INFO+="%F{red}${VCS_SYMBOL_UNTRACKED} ${VCS_INFO_UNTRACKED}%f "
fi
if [ "$VCS_INFO_STASHED" -ne "0" ]; then
VCS_INFO+="%F{blue}${VCS_SYMBOL_STASHED} ${VCS_INFO_STASHED}%f "
fi
if [ "$VCS_INFO_CONFLICTS" -ne "+0" ]; then
VCS_INFO+="%F{red}${VCS_SYMBOL_CONFLICTS} ${VCS_INFO_CONFLICTS}%f "
fi
if [ -z "${_VCS_INFO_STATUS}" ]; then
VCS_INFO+="%F{green}${VCS_SYMBOL_CLEAN}%f"
fi
echo $VCS_INFO
else
echo ""
fi
}
# Virtual env indicator
export VIRTUAL_ENV_DISABLE_PROMPT=yes
function virtenv_indicator() {
if [[ -z $VIRTUAL_ENV ]]; then
psvar[1]=''
else
psvar[1]=${VIRTUAL_ENV##*/}
fi
}
add-zsh-hook precmd virtenv_indicator
# Finalized prompt
precmd() {
PS1="%(1V.(%1v).)%f%(1j.%F{cyan}[%j]%f .)%F{blue} %~%f " # start of promt: ~
PS1+="$(vcs_prompt_info)" # git info
PS1+=$'\n%(?.%F{green}.%F{red}) %f' # input line
}