]> git.rmz.io Git - dotfiles.git/blob - zsh/lib/prompt.zsh
vim: also pull on BufNewFile
[dotfiles.git] / zsh / lib / prompt.zsh
1 # Setup the prompt with pretty colors
2 setopt prompt_subst
3 autoload colors; colors;
4
5 hostcolor=green
6 [[ $(hostname) == "tardis" ]] && hostcolor=red
7
8 precmd() {
9 PROMPT='%T %n@%{$fg[$hostcolor]%}%m%(1j. $fg_bold[white]↵%{$fg_bold[red]%}%j.)%{$reset_color%}%-0>..>$(git_prompt_status)%>>
10 %(?..%{$fg_bold[white]%}%?)%{$reset_color%}$(vi_prompt_info)%{%(!.$fg[red]❰.$fg[green]❱)%1G%} '
11 RPROMPT='%{$fg[green]%}%~%{$reset_color%}'
12 }
13
14 vi_prompt_info() {
15 local vicmd="$fg_bold[green]❰$reset_color%1G"
16 local viins="$fg_bold[blue]❱$reset_color%1G"
17 local map=${KEYMAP:-viins}
18 printf '%s' "%{${${map/vicmd/$vicmd}/(main|viins)/$viins}%}"
19 }
20
21 function zle-line-init zle-line-finish zle-keymap-select {
22 zle reset-prompt
23 zle -R
24 }
25
26 # zle -N zle-line-init
27 # zle -N zle-line-finish
28 zle -N zle-keymap-select
29
30 # reset zle on resize
31 TRAPWINCH() {
32 zle && { zle reset-prompt; zle -R }
33 }
34
35 # Get the status of the working tree
36 git_prompt_status() {
37 local branch ahead behind
38 local added deleted modified renamed unmerged untracked dirty
39 # Use porcelain status for easy parsing.
40 local status_cmd="git status --porcelain -b"
41
42 # Get current status.
43 while IFS=$'\n' read line; do
44 if [[ "$line" == \#\#\ * ]]; then
45 [[ "$line" =~ '## (.*?)(\.\.\.|$)' ]] && branch=$match[1]
46 [[ "$line" =~ 'ahead ([0-9]+)' ]] && ahead=$match[1]
47 [[ "$line" =~ 'behind ([0-9]+)' ]] && behind=$match[1]
48 else
49 # Count added, deleted, modified, renamed, unmerged, untracked, dirty.
50 # T (type change) is undocumented, see http://git.io/FnpMGw.
51 # index
52 [[ "$line" == M[\ MTD]\ * ]] && (( updated++ ))
53 [[ "$line" == [AC][\ MTD]\ * ]] && (( added++ ))
54 [[ "$line" == D[\ MT]\ * ]] && (( deleted++ ))
55 [[ "$line" == R[\ MTD]\ * ]] && (( renamed++ ))
56
57 # work tree
58 [[ "$line" == [\ MARCT]M\ * ]] && (( modified++ ))
59 [[ "$line" == [\ MARCT]D\ * ]] && (( deleted_wt++ ))
60 [[ "$line" == \?\?\ * ]] && (( untracked++ ))
61
62 # morge conflicts
63 [[ "$line" == (AA|DD|U?|?U)\ * ]] && (( unmerged++ ))
64 fi
65 done < <(${(z)status_cmd} 2> /dev/null)
66
67 local git_status=" %{$fg[yellow]%}"
68
69 # Format branch
70 if [[ -n $branch ]]; then
71 git_status+="$branch"
72 else
73 git_status+="$(git rev-parse --short HEAD 2> /dev/null)"
74 [[ $? -ne 0 ]] && return
75 fi
76
77 # Format upstream
78 local upstream_str
79 (( ahead > 0 )) && upstream_str+="%{$fg[blue]%} >$ahead"
80 (( behind > 0 )) && upstream_str+="%{$fg[blue]%} <$behind"
81 git_status+="$upstream_str"
82
83 # Format stashed
84 stashed=$(git stash list | wc -l)
85 if (( stashed > 0 )) then
86 stashed_str+="%{$fg_bold[cyan]%} ⋎$stashed%{$reset_color%}"
87 fi
88 git_status+="$stashed_str"
89
90 # Format index
91 local index_str
92 (( updated > 0 )) && index_str+="%{$fg[green]%} *$updated"
93 (( added > 0 )) && index_str+="%{$fg[green]%} +$added"
94 (( deleted > 0 )) && index_str+="%{$fg[green]%} -$deleted"
95 (( renamed > 0 )) && index_str+="%{$fg[green]%} ≈$renamed"
96 git_status+="$index_str"
97
98 # Format working tree
99 local wt_str
100 (( modified > 0 )) && wt_str+="%{$fg[red]%} *$modified"
101 (( deleted_wt > 0 )) && wt_str+="%{$fg[red]%} -$deleted_wt"
102 (( untracked > 0 )) && wt_str+="%{$fg[red]%} +$untracked"
103 (( unmerged > 0 )) && wt_str+="%{$fg[magenta]%} ♒$unmerged"
104 git_status+="$wt_str"
105
106 git_status+="%{$reset_color%}"
107
108 echo $git_status
109 }
110
111 function print_if_fits() {
112 local zero length
113
114 zero='%([BSUbfksu]|([FB]|){*})'
115 length=${#${(S%%)1//$~zero/}}
116 echo "%-$length(l.$1.)"
117 }