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