-
Notifications
You must be signed in to change notification settings - Fork 265
Expand file tree
/
Copy pathprompt.zsh
More file actions
274 lines (231 loc) · 7.77 KB
/
Copy pathprompt.zsh
File metadata and controls
274 lines (231 loc) · 7.77 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# User customizable options
# PR_ARROW_CHAR="[some character]"
# RPR_SHOW_USER=(true, false) - show username in rhs prompt
# RPR_SHOW_HOST=(true, false) - show host in rhs prompt
# RPR_SHOW_GIT=(true, false) - show git status in rhs prompt
# Set custom prompt
# Allow for variable/function substitution in prompt
setopt prompt_subst
# Load color variables to make it easier to color things
autoload -U colors && colors
# Make using 256 colors easier
if [[ "$(tput colors)" == "256" ]]; then
source ~/.zsh/plugins/spectrum.zsh
# change default colors
fg[green]=$FG[064]
fg[cyan]=$FG[037]
fg[blue]=$FG[033]
fg[teal]=$FG[041]
fg[red]=$FG[160]
fg[orange]=$FG[166]
fg[yellow]=$FG[136]
fg[magenta]=$FG[125]
fg[violet]=$FG[061]
fg[brown]=$FG[094]
fg[neon]=$FG[112]
fg[pink]=$FG[183]
fg[darkred]=$FG[088]
else
fg[teal]=$fg[blue]
fg[orange]=$fg[yellow]
fg[violet]=$fg[magenta]
fg[brown]=$fg[orange]
fg[neon]=$fg[green]
fg[pink]=$fg[magenta]
fg[darkred]=$fg[red]
fi
# Current directory, truncated to 3 path elements (or 4 when one of them is "~")
# The number of elements to keep can be specified as ${1}
function PR_DIR() {
local sub=${1}
if [[ "${sub}" == "" ]]; then
sub=3
fi
local len="$(expr ${sub} + 1)"
local full="$(print -P "%d")"
local relfull="$(print -P "%~")"
local shorter="$(print -P "%${len}~")"
local current="$(print -P "%${len}(~:.../:)%${sub}~")"
local last="$(print -P "%1~")"
# Longer path for '~/...'
if [[ "${shorter}" == \~/* ]]; then
current=${shorter}
fi
local truncated="$(echo "${current%/*}/")"
# Handle special case of directory '/' or '~something'
if [[ "${truncated}" == "/" || "${relfull[1,-2]}" != */* ]]; then
truncated=""
fi
# Handle special case of last being '/...' one directory down
if [[ "${full[2,-1]}" != "" && "${full[2,-1]}" != */* ]]; then
truncated="/"
last=${last[2,-1]} # take substring
fi
echo "%{$fg[green]%}${truncated}%{$fg[orange]%}%B${last}%b%{$reset_color%}"
}
# An exclamation point if the previous command did not complete successfully
function PR_ERROR() {
echo "%(?..%(!.%{$fg[violet]%}.%{$fg[red]%})%B!%b%{$reset_color%} )"
}
# The arrow symbol that is used in the prompt
PR_ARROW_CHAR=">"
# The arrow in red (for root) or violet (for regular user)
function PR_ARROW() {
echo "%(!.%{$fg[red]%}.%{$fg[violet]%})${PR_ARROW_CHAR}%{$reset_color%}"
}
# Set custom rhs prompt
# User in red (for root) or violet (for regular user)
RPR_SHOW_USER=true # Set to false to disable user in rhs prompt
function RPR_USER() {
if [[ "${RPR_SHOW_USER}" == "true" ]]; then
echo "%(!.%{$fg[red]%}.%{$fg[violet]%})%B%n%b%{$reset_color%}"
fi
}
# Host in yellow
RPR_SHOW_HOST=true # Set to false to disable host in rhs prompt
function RPR_HOST() {
local colors
colors=(yellow pink darkred brown neon teal)
local index=$(python -c "print(hash('$(hostname)') % ${#colors} + 1)")
local color=$colors[index]
if [[ "${RPR_SHOW_HOST}" == "true" ]]; then
echo "%{$fg[$color]%}%m%{$reset_color%}"
fi
}
# ' at ' in orange outputted only if both user and host enabled
function RPR_AT() {
if [[ "${RPR_SHOW_USER}" == "true" ]] && [[ "${RPR_SHOW_HOST}" == "true" ]]; then
echo "%{$fg[blue]%} at %{$reset_color%}"
fi
}
# Build the rhs prompt
function RPR_INFO() {
echo "$(RPR_USER)$(RPR_AT)$(RPR_HOST)"
}
# Set RHS prompt for git repositories
DIFF_SYMBOL="-"
GIT_PROMPT_SYMBOL=""
GIT_PROMPT_PREFIX="%{$fg[violet]%}%B(%b%{$reset_color%}"
GIT_PROMPT_SUFFIX="%{$fg[violet]%}%B)%b%{$reset_color%}"
GIT_PROMPT_AHEAD="%{$fg[teal]%}%B+NUM%b%{$reset_color%}"
GIT_PROMPT_BEHIND="%{$fg[orange]%}%B-NUM%b%{$reset_color%}"
GIT_PROMPT_MERGING="%{$fg[cyan]%}%Bx%b%{$reset_color%}"
GIT_PROMPT_UNTRACKED="%{$fg[red]%}%B$DIFF_SYMBOL%b%{$reset_color%}"
GIT_PROMPT_MODIFIED="%{$fg[yellow]%}%B$DIFF_SYMBOL%b%{$reset_color%}"
GIT_PROMPT_STAGED="%{$fg[green]%}%B$DIFF_SYMBOL%b%{$reset_color%}"
GIT_PROMPT_DETACHED="%{$fg[neon]%}%B!%b%{$reset_color%}"
# Show Git branch/tag, or name-rev if on detached head
function parse_git_branch() {
(git symbolic-ref -q HEAD || git name-rev --name-only --no-undefined --always HEAD) 2> /dev/null
}
function parse_git_detached() {
if ! git symbolic-ref HEAD >/dev/null 2>&1; then
echo "${GIT_PROMPT_DETACHED}"
fi
}
# Show different symbols as appropriate for various Git repository states
function parse_git_state() {
# Compose this value via multiple conditional appends.
local GIT_STATE=""
local NUM_AHEAD="$(git log --oneline @{u}.. 2> /dev/null | wc -l | tr -d ' ')"
if [ "$NUM_AHEAD" -gt 0 ]; then
GIT_STATE=$GIT_STATE${GIT_PROMPT_AHEAD//NUM/$NUM_AHEAD}
fi
local NUM_BEHIND="$(git log --oneline ..@{u} 2> /dev/null | wc -l | tr -d ' ')"
if [ "$NUM_BEHIND" -gt 0 ]; then
if [[ -n $GIT_STATE ]]; then
GIT_STATE="$GIT_STATE "
fi
GIT_STATE=$GIT_STATE${GIT_PROMPT_BEHIND//NUM/$NUM_BEHIND}
fi
local GIT_DIR="$(git rev-parse --git-dir 2> /dev/null)"
if [ -n $GIT_DIR ] && test -r $GIT_DIR/MERGE_HEAD; then
if [[ -n $GIT_STATE ]]; then
GIT_STATE="$GIT_STATE "
fi
GIT_STATE=$GIT_STATE$GIT_PROMPT_MERGING
fi
if [[ -n $(git ls-files --other --exclude-standard :/ 2> /dev/null) ]]; then
GIT_DIFF=$GIT_PROMPT_UNTRACKED
fi
if ! git diff --quiet 2> /dev/null; then
GIT_DIFF=$GIT_DIFF$GIT_PROMPT_MODIFIED
fi
if ! git diff --cached --quiet 2> /dev/null; then
GIT_DIFF=$GIT_DIFF$GIT_PROMPT_STAGED
fi
if [[ -n $GIT_STATE && -n $GIT_DIFF ]]; then
GIT_STATE="$GIT_STATE "
fi
GIT_STATE="$GIT_STATE$GIT_DIFF"
if [[ -n $GIT_STATE ]]; then
echo "$GIT_PROMPT_PREFIX$GIT_STATE$GIT_PROMPT_SUFFIX"
fi
}
# If inside a Git repository, print its branch and state
RPR_SHOW_GIT=true # Set to false to disable git status in rhs prompt
function git_prompt_string() {
if [[ "${RPR_SHOW_GIT}" == "true" ]]; then
local git_where="$(parse_git_branch)"
local git_detached="$(parse_git_detached)"
[ -n "$git_where" ] && echo " $GIT_PROMPT_SYMBOL$(parse_git_state)$GIT_PROMPT_PREFIX%{$fg[magenta]%}%B${git_where#(refs/heads/|tags/)}%b$git_detached$GIT_PROMPT_SUFFIX"
fi
}
PROMPT_MODE=0
# Function to toggle between prompt modes
function tog() {
if [[ "${PROMPT_MODE}" == 0 ]]; then
PROMPT_MODE=1
elif [[ "${PROMPT_MODE}" == 1 ]]; then
PROMPT_MODE=2
else
PROMPT_MODE=0
fi
}
# Prompt
function PCMD() {
if [[ "${PROMPT_MODE}" == 0 ]]; then
echo "$(PR_DIR) $(PR_ERROR)$(PR_ARROW) " # space at the end
elif [[ "${PROMPT_MODE}" == 1 ]]; then
echo "$(PR_DIR 1) $(PR_ERROR)$(PR_ARROW) " # space at the end
else
echo "$(PR_ERROR)$(PR_ARROW) " # space at the end
fi
}
PROMPT='$(PCMD)' # single quotes to prevent immediate execution
RPROMPT='' # set asynchronously and dynamically
# Right-hand prompt
function RCMD() {
if [[ "${PROMPT_MODE}" == 0 ]]; then
echo "$(RPR_INFO)$(git_prompt_string)"
elif [[ "${PROMPT_MODE}" == 1 ]]; then
echo "$(git_prompt_string)"
else
echo ""
fi
}
ASYNC_PROC=0
function precmd() {
function async() {
# save to temp file
printf "%s" "$(RCMD)" > "${HOME}/.zsh_tmp_prompt"
# signal parent
kill -s USR1 $$
}
# do not clear RPROMPT, let it persist
# kill child if necessary
if [[ "${ASYNC_PROC}" != 0 ]]; then
kill -s HUP $ASYNC_PROC >/dev/null 2>&1 || :
fi
# start background computation
async &!
ASYNC_PROC=$!
}
function TRAPUSR1() {
# read from temp file
RPROMPT="$(cat ${HOME}/.zsh_tmp_prompt)"
# reset proc number
ASYNC_PROC=0
# redisplay
zle && zle reset-prompt
}