-
Notifications
You must be signed in to change notification settings - Fork 14
/
prompt_powerline_setup
232 lines (182 loc) · 8.55 KB
/
prompt_powerline_setup
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
zmodload -i zsh/parameter || return 1
# vim: set syntax=zsh:
prompt_powerline_help () {
setopt localoptions nocshnullcmd noshnullcmd
autoload -U is-at-least
is-at-least 4.3 || echo 'Requires ZSH_VERSION 4.3!'
<<-\EOF
This is a rather heavyweight zsh prompt, based on the powerline font
from the popular eponymous vim plugin.
This prompt is configured using zstyles:
# whether the user should be hidden. you can use this to not
# display a username if it's your default one, or only show it
# if this is a remote shell, e.g. [[ -n $SSH_CONNECTION ]]
zstyle ':prompt:powerline:ps1' hide-user
# explicit color used for the hostname, otherwise a hash-based one
# will be selected
zstyle ':prompt:powerline:ps1' host-color
For a (more) detailed readme, see here:
https://github.com/Valodim/zsh-prompt-powerline/blob/master/readme.mkd
EOF
}
prompt_powerline_vcsstyles () {
# set formats
local FMT_BRANCH="%b%u%c" # e.g. master¹²
local FMT_ACTION="(%F{cyan}%a%f)" # e.g. (rebase-i)
local FMT_PATH="%K{black}$sep1%F{white} %R%%F{yellow}/%f%S%F{black}" # e.g. ~/repo/subdir
# we only use one, so onl pay for one.
zstyle ':vcs_info:*:powerline:*' max-exports 1
zstyle ':vcs_info:*:powerline:*' unstagedstr '¹' # display ¹ if there are unstaged changes
zstyle ':vcs_info:*:powerline:*' stagedstr '²' # display ² if there are staged changes
# non-vcs
zstyle ':vcs_info:*:powerline:*' nvcsformats "%K{black}$sep1%F{white} %1v%F{black} "
# generic vcs
zstyle ':vcs_info:*:powerline:*' formats "%K{23}$sep1%F{123} %s ${FMT_BRANCH} %F{23}${FMT_PATH} "
zstyle ':vcs_info:*:powerline:*' actionformats "%K{23}$sep1%F{123} %s ${FMT_BRANCH}${FMT_ACTION} %F{23}${FMT_PATH} "
# special hg stuff
zstyle ':vcs_info:hg:powerline:*' formats "%K{23}$sep1%F{123} ☿ ${FMT_BRANCH} %F{23} ${FMT_PATH} "
zstyle ':vcs_info:hg:powerline:*' actionformats "%K{23}$sep1%F{123} ☿ ${FMT_BRANCH}${FMT_ACTION} %F{23}${FMT_PATH} "
# special git stuff
zstyle ':vcs_info:git:powerline:*' formats "%K{23}$sep1%F{123} ± ${FMT_BRANCH} %m%F{23}${FMT_PATH} "
zstyle ':vcs_info:git:powerline:*' actionformats "%K{23}$sep1%F{123} ± ${FMT_BRANCH}${FMT_ACTION} %m%F{23}${FMT_PATH} "
# lofi formats
# for details see here: https://github.com/Valodim/zshrc/blob/master/custom/96-vcs_info-lofi.zsh
zstyle ':vcs_info:*:powerline:*' lofiformats "%K{23}$sep1%F{123} %s %F{23}${FMT_PATH} "
zstyle ':vcs_info:hg:powerline:*' lofiformats "%K{23}$sep1%F{123} ☿ %F{23}${FMT_PATH} "
zstyle ':vcs_info:git:powerline:*' lofiformats "%K{23}$sep1%F{123} ± %F{23}${FMT_PATH} "
# see https://github.com/Valodim/zshrc/blob/master/custom/96-vcs_info-githooks.zsh
# tracking formats: behind, ahead and seperator
zstyle ':vcs_info:git:powerline:*' trackingformats "%%F{46}+%a%%f" "%%F{160}-%b%%f" "%%F{46}+%a%%F{123}/%f%%F{160}-%b%%f"
# stash format
zstyle ':vcs_info:git:powerline:*' stashformat "%%F{81}$sep2 %%B%s %b"
# stgit formats
zstyle ':vcs_info:git:powerline:*' patch-format "%%F{186}$sep2%%F{81} %p %n/%a "
zstyle ':vcs_info:git:powerline:*' nopatch-format "$sep2 0/%a "
}
prompt_powerline_precmd () {
# preserve exitstatus for later (if only for a hook!)
local exstat=$?
setopt localoptions noksharrays unset
# have those here as well
local sep1 sep2 lock_char branch_char
zstyle -s ':prompt:poweline:ps1:' sep1-char sep1 || sep1='⮀'
zstyle -s ':prompt:poweline:ps1:' sep2-char sep2 || sep2='⮁'
zstyle -s ':prompt:poweline:ps1:' lock-char lock_char || lock_char='⭤'
zstyle -s ':prompt:poweline:ps1:' branch-char branch_char || branch_char='⭠'
# the variable we add our bits to
prompt_bits=( )
# add info about localhist status, if there is any
if [[ -n $localhist ]]; then
# disabled = red, enabled = blue, enabled for parent dir = yellow
if [[ -n $localhist_disable ]]; then
prompt_bits+=( "%F{red}$branch_char " )
elif [[ -n $localhist_specific ]]; then
prompt_bits+=( "%F{12}$branch_char " )
else
prompt_bits+=( "%F{yellow}$branch_char " )
fi
fi
# show stats for this dir
if [[ ! -O $PWD ]]; then
if [[ -w $PWD ]]; then
prompt_bits+=( "%F{blue}$lock_char " )
elif [[ -x $PWD ]]; then
prompt_bits+=( "%F{yellow}$lock_char " )
elif [[ -r $PWD ]]; then
prompt_bits+=( "%F{red}$lock_char " )
fi
fi
if [[ ! -w $PWD && ! -r $PWD ]]; then
prompt_bits+=( "%F{red}⭤ " )
fi
# end the hostname background
prompt_bits+=( "%F{238}" )
# add exitstatus segment, if it's non-zero
(( exstat > 0 )) && prompt_bits+=( "%K{red}$sep1%F{white} %B%? %b%F{red}" )
# we use %1v for the path (outside repositories), to allow the hook below
# to mess with it. I use it for disambiguate, mostly.
psvar[1]=${(%):-%3~}
vcs_info 'powerline'
# use this to add custom segments or do other wacky stuff
# to match colors properly, you will need to have a format like this,
# assuming bg is your background and fg is your foreground color:
# %K{bg}%F{fg}text%F{bg}
local hooks hook
if zstyle -a ':prompt:powerline:ps1' precmd-hooks hooks; then
for hook in $hooks; do
$hook || break
done
fi
# implode the array
prompt_bits=${(j::)prompt_bits}
}
prompt_powerline_ps1 () {
setopt localoptions noksharrays extendedglob
local host_color
# can we even make any assumptions about the terminal?
if ! (( $+terminfo[colors] )); then
host_color='green'
elif zstyle -s ":prompt:valodim:ps1:${HOST}" host-color host_color; then
# make sure it's within range
[[ host_color == <-> ]] && host_color=$(( host_color % $terminfo[colors] ))
# otherwise, don't care
else
# get a number from the sha1 hash of the hostname
host_color=${${"$(sha1sum <<< $HOST)"//[a-z]#/}[1,14]}
# divide by 256/88/8/whatever
host_color=$(( host_color % $terminfo[colors] ))
fi
# hide username if it's my regular one (anyone else might wanna change this)
local user=""
if ! zstyle -t ':prompt:powerline:ps1' hide-user; then
user=" %F{247}$sep2 %(!.%F{red}.)%n"
fi
integer default_level
zstyle -s ':prompt:powerline:ps1' default-sh-level default_level || default_level=1
# using eval here because (l::) expansion flag doesn't support variables.
# someone who can sneak in variables here can do code execution anyways~
eval 'local shlvl="%K{238}%F{$host_color}$sep1${(l:$SHLVL-$default_level::'$sep2':):-} "'
local userhost="%F{$host_color}${HOST}${user}"
local jobstat="%(1j.%K{green}$sep1%F{white} %B%j %b%F{green}.)"
# we do the $HOME replacement here to avoid annoyingly long paths. It's not
# as good as (D), but oh well!
local gitinfo='${${vcs_info_msg_0_%%.}/$HOME/~}'
PS1="${shlvl}${userhost} \$prompt_bits${jobstat}${gitinfo}%k$sep1%b%f "
}
prompt_powerline_ps2 () {
PS2="%K{black}$sep2 %_ %F{black}%k$sep1%b%f "
}
prompt_powerline_setup () {
setopt localoptions noksharrays unset
autoload -U is-at-least
if ! is-at-least 4.3; then
echo 'the powerline prompt requires at least zsh version 4.3 (y'\''know, the one released in 2007)' >&2
return 1
fi
# zmodload zsh/termcap
zmodload zsh/terminfo
if ! (( $+terminfo[colors] )) || (( terminfo[colors] < 256 )); then
echo 'the powerline prompt is designed to work with 256 colors.
a prompt with powerline patched font not supporting 256 colors is kind of a Rarity, isn'\''t it?' >&2
return 1
fi
local sep1 sep2 lock_char branch_char
zstyle -s ':prompt:poweline:ps1:' sep1-char sep1 || sep1='⮀'
zstyle -s ':prompt:poweline:ps1:' sep2-char sep2 || sep2='⮁'
zstyle -s ':prompt:poweline:ps1:' lock-char lock_char || lock_char='⭤'
zstyle -s ':prompt:poweline:ps1:' branch-char branch_char || branch_char='⭠'
# load vcs_info styles
autoload -Uz vcs_info
prompt_powerline_vcsstyles
# set up left-side prompt(s)
prompt_powerline_ps1
prompt_powerline_ps2
# clear right-side prompt
RPROMPT=
# we know add-zsh-hook has been loaded by promptinit
add-zsh-hook precmd prompt_powerline_precmd
# as I said, fairly heavyweight...
prompt_opts=( percent subst cr )
return 0
}
[[ -o kshautoload ]] || prompt_powerline_setup "$@"