-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzshrc
447 lines (372 loc) · 14.3 KB
/
zshrc
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# .zshrc is sourced in interactive shells.
# It should contain commands to set up aliases,
# functions, options, key bindings, etc.
#
autoload -U compinit
compinit
#allow tab completion in the middle of a word
setopt COMPLETE_IN_WORD
alias l=ls
alias ll="ls -la"
alias z=cd
alias t=touch
## keep background processes at full speed
#setopt NOBGNICE
## restart running processes on exit
#setopt HUP
## history
setopt APPEND_HISTORY
## for sharing history between zsh processes
setopt INC_APPEND_HISTORY
setopt SHARE_HISTORY
## never ever beep ever
setopt NO_BEEP
## automatically decide when to page a list of completions
#LISTMAX=0
## disable mail checking
MAILCHECK=0
autoload -U colors
#colors
# git-prompt.zsh -- a lightweight git prompt for zsh.
# Copyright © 2024 Wolfgang Popp
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
# OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
autoload -U colors && colors
# Settings
: "${ZSH_GIT_PROMPT_SHOW_UPSTREAM=""}"
: "${ZSH_GIT_PROMPT_SHOW_STASH=""}"
: "${ZSH_GIT_PROMPT_SHOW_TRACKING_COUNTS="1"}"
: "${ZSH_GIT_PROMPT_SHOW_LOCAL_COUNTS="1"}"
: "${ZSH_GIT_PROMPT_ENABLE_SECONDARY=""}"
: "${ZSH_GIT_PROMPT_NO_ASYNC=""}"
: "${ZSH_GIT_PROMPT_FORCE_BLANK=""}"
: "${ZSH_GIT_PROMPT_AWK_CMD=""}"
# Theming
: "${ZSH_THEME_GIT_PROMPT_PREFIX="["}"
: "${ZSH_THEME_GIT_PROMPT_SUFFIX="] "}"
: "${ZSH_THEME_GIT_PROMPT_SEPARATOR="|"}"
: "${ZSH_THEME_GIT_PROMPT_DETACHED="%{$fg_bold[cyan]%}:"}"
: "${ZSH_THEME_GIT_PROMPT_BRANCH="%{$fg_bold[magenta]%}"}"
: "${ZSH_THEME_GIT_PROMPT_UPSTREAM_SYMBOL="%{$fg_bold[yellow]%}B "}"
: "${ZSH_THEME_GIT_PROMPT_UPSTREAM_NO_TRACKING=""}"
: "${ZSH_THEME_GIT_PROMPT_UPSTREAM_PREFIX="%{$fg[red]%}(%{$fg[yellow]%}"}"
: "${ZSH_THEME_GIT_PROMPT_UPSTREAM_SUFFIX="%{$fg[red]%})"}"
: "${ZSH_THEME_GIT_PROMPT_BEHIND="↓"}"
: "${ZSH_THEME_GIT_PROMPT_AHEAD="↑"}"
: "${ZSH_THEME_GIT_PROMPT_UNMERGED="%{$fg[red]%}x"}"
: "${ZSH_THEME_GIT_PROMPT_STAGED="%{$fg[green]%}o"}"
: "${ZSH_THEME_GIT_PROMPT_UNSTAGED="%{$fg[red]%}+"}"
: "${ZSH_THEME_GIT_PROMPT_UNTRACKED="…"}"
: "${ZSH_THEME_GIT_PROMPT_STASHED="%{$fg[blue]%}S"}"
: "${ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg_bold[green]%}ok"}"
: "${ZSH_THEME_GIT_PROMPT_SECONDARY_PREFIX=""}"
: "${ZSH_THEME_GIT_PROMPT_SECONDARY_SUFFIX=""}"
: "${ZSH_THEME_GIT_PROMPT_TAGS_SEPARATOR=", "}"
: "${ZSH_THEME_GIT_PROMPT_TAGS_PREFIX="tag "}"
: "${ZSH_THEME_GIT_PROMPT_TAGS_SUFFIX=""}"
: "${ZSH_THEME_GIT_PROMPT_TAG="%{$fg_bold[magenta]%}"}"
# Disable promptinit if it is loaded
(( $+functions[promptinit] )) && {promptinit; prompt off}
# Allow parameter and command substitution in the prompt
setopt PROMPT_SUBST
# Override PROMPT if it does not use the gitprompt function
[[ "$PROMPT" != *gitprompt* && "$RPROMPT" != *gitprompt* ]] \
&& PROMPT='%B%40<..<%~ %b$(gitprompt)' \
&& PROMPT+='%(?.%(!.%F{white}❯%F{yellow}>%F{red}.%F{blue}>%F{cyan}>%F{green})>.%F{red}>>>)%f '
# Find an awk implementation
# Prefer nawk over mawk and mawk over awk
(( $+commands[mawk] )) && : "${ZSH_GIT_PROMPT_AWK_CMD:=mawk}"
(( $+commands[nawk] )) && : "${ZSH_GIT_PROMPT_AWK_CMD:=nawk}"
: "${ZSH_GIT_PROMPT_AWK_CMD:=awk}"
# Use --show-stash for git versions newer than 2.35.0
_zsh_git_prompt_git_version=$(command git version)
if [[ "${_zsh_git_prompt_git_version:12}" == 2.<35->.<-> ]]; then
_zsh_git_prompt_git_cmd() {
GIT_OPTIONAL_LOCKS=0 command git status --show-stash --branch --porcelain=v2 2>&1 \
|| echo "fatal: git command failed"
}
else
_zsh_git_prompt_git_cmd() {
[[ -n "$ZSH_GIT_PROMPT_SHOW_STASH" ]] && (
c=$(command git rev-list --walk-reflogs --count refs/stash 2> /dev/null)
[[ -n "$c" ]] && echo "# stash $c"
)
GIT_OPTIONAL_LOCKS=0 command git status --branch --porcelain=v2 2>&1 \
|| echo "fatal: git command failed"
}
fi
unset _zsh_git_prompt_git_version
function _zsh_git_prompt_git_status() {
emulate -L zsh
_zsh_git_prompt_git_cmd | $ZSH_GIT_PROMPT_AWK_CMD \
-v PREFIX="$ZSH_THEME_GIT_PROMPT_PREFIX" \
-v SUFFIX="$ZSH_THEME_GIT_PROMPT_SUFFIX" \
-v SEPARATOR="$ZSH_THEME_GIT_PROMPT_SEPARATOR" \
-v DETACHED="$ZSH_THEME_GIT_PROMPT_DETACHED" \
-v BRANCH="$ZSH_THEME_GIT_PROMPT_BRANCH" \
-v UPSTREAM_TYPE="$ZSH_GIT_PROMPT_SHOW_UPSTREAM" \
-v SHOW_TRACKING_COUNTS="$ZSH_GIT_PROMPT_SHOW_TRACKING_COUNTS" \
-v SHOW_LOCAL_COUNTS="$ZSH_GIT_PROMPT_SHOW_LOCAL_COUNTS" \
-v UPSTREAM_SYMBOL="$ZSH_THEME_GIT_PROMPT_UPSTREAM_SYMBOL" \
-v UPSTREAM_NO_TRACKING="$ZSH_THEME_GIT_PROMPT_UPSTREAM_NO_TRACKING" \
-v UPSTREAM_PREFIX="$ZSH_THEME_GIT_PROMPT_UPSTREAM_PREFIX" \
-v UPSTREAM_SUFFIX="$ZSH_THEME_GIT_PROMPT_UPSTREAM_SUFFIX" \
-v BEHIND="$ZSH_THEME_GIT_PROMPT_BEHIND" \
-v AHEAD="$ZSH_THEME_GIT_PROMPT_AHEAD" \
-v UNMERGED="$ZSH_THEME_GIT_PROMPT_UNMERGED" \
-v STAGED="$ZSH_THEME_GIT_PROMPT_STAGED" \
-v UNSTAGED="$ZSH_THEME_GIT_PROMPT_UNSTAGED" \
-v UNTRACKED="$ZSH_THEME_GIT_PROMPT_UNTRACKED" \
-v STASHED="$ZSH_THEME_GIT_PROMPT_STASHED" \
-v SHOW_STASH="$ZSH_GIT_PROMPT_SHOW_STASH" \
-v CLEAN="$ZSH_THEME_GIT_PROMPT_CLEAN" \
-v RC="%{$reset_color%}" \
'
BEGIN {
ORS = "";
fatal = 0;
oid = "";
head = "";
upstream = "";
ahead = 0;
behind = 0;
untracked = 0;
unmerged = 0;
staged = 0;
unstaged = 0;
stashed = 0;
}
function prompt_element(prefix, content, suffix) {
print(prefix);
gsub("%", "%%", content);
print(content);
print(suffix);
print(RC);
}
function count_element(prefix, count, show_count) {
content = "";
if (show_count) {
content = count;
}
if (count > 0) {
prompt_element(prefix, content);
}
}
function local_element(prefix, count) {
count_element(prefix, count, SHOW_LOCAL_COUNTS)
}
function tracking_element(prefix, count) {
count_element(prefix, count, SHOW_TRACKING_COUNTS)
}
$1 == "fatal:" {
fatal = 1;
}
$2 == "branch.oid" {
oid = $3;
}
$2 == "branch.head" {
head = $3;
}
$2 == "branch.upstream" {
upstream = $3;
}
$2 == "branch.ab" {
ahead = $3;
behind = $4;
}
$1 == "?" {
++untracked;
}
$1 == "u" {
++unmerged;
}
$1 == "1" || $1 == "2" {
split($2, arr, "");
if (arr[1] != ".") {
++staged;
}
if (arr[2] != ".") {
++unstaged;
}
}
$2 == "stash" {
stashed = $3;
}
END {
if (fatal == 1) {
exit(1);
}
prompt_element(PREFIX);
if (head == "(detached)") {
prompt_element(DETACHED, substr(oid, 0, 7));
} else {
prompt_element(BRANCH, head);
}
if (upstream == "") {
prompt_element(UPSTREAM_NO_TRACKING);
} else if (UPSTREAM_TYPE == "symbol") {
prompt_element(UPSTREAM_SYMBOL);
} else if (UPSTREAM_TYPE == "full") {
prompt_element(UPSTREAM_PREFIX, upstream, UPSTREAM_SUFFIX);
}
tracking_element(BEHIND, behind * -1);
tracking_element(AHEAD, ahead * 1);
prompt_element(SEPARATOR);
local_element(UNMERGED, unmerged);
local_element(STAGED, staged);
local_element(UNSTAGED, unstaged);
local_element(UNTRACKED, untracked);
if (SHOW_STASH) {
local_element(STASHED, stashed);
}
if (unmerged == 0 && staged == 0 && unstaged == 0 && untracked == 0) {
prompt_element(CLEAN);
}
prompt_element(SUFFIX);
}
'
}
function _zsh_git_prompt_git_status_secondary() {
tags=$(command git tag --points-at=HEAD 2> /dev/null)
[[ -z "$tags" ]] && return
echo -n ${ZSH_THEME_GIT_PROMPT_SECONDARY_PREFIX}
echo -n ${ZSH_THEME_GIT_PROMPT_TAGS_PREFIX}
echo "$tags" | $ZSH_GIT_PROMPT_AWK_CMD \
-v SEPARATOR="$ZSH_THEME_GIT_PROMPT_TAGS_SEPARATOR" \
-v TAG="$ZSH_THEME_GIT_PROMPT_TAG" \
-v RC="%{$reset_color%}" \
'
BEGIN {
ORS = "";
}
{
if (NR != 1) {
print SEPARATOR;
print RC;
}
print TAG;
print $0;
print RC;
}
'
echo -n ${ZSH_THEME_GIT_PROMPT_TAGS_SUFFIX}
echo -n ${ZSH_THEME_GIT_PROMPT_SECONDARY_SUFFIX}
}
# The async code is taken from
# https://github.com/zsh-users/zsh-autosuggestions/blob/master/src/async.zsh
zmodload zsh/system
function _zsh_git_prompt_async_request() {
typeset -g _ZSH_GIT_PROMPT_ASYNC_FD _ZSH_GIT_PROMPT_ASYNC_PID
# If we've got a pending request, cancel it
if [[ -n "$_ZSH_GIT_PROMPT_ASYNC_FD" ]] && { true <&$_ZSH_GIT_PROMPT_ASYNC_FD } 2>/dev/null;
then
# Close the file descriptor and remove the handler
exec {_ZSH_GIT_PROMPT_ASYNC_FD}<&-
zle -F $_ZSH_GIT_PROMPT_ASYNC_FD
# Zsh will make a new process group for the child process only if job
# control is enabled (MONITOR option)
if [[ -o MONITOR ]]; then
# Send the signal to the process group to kill any processes that may
# have been forked by the suggestion strategy
kill -TERM -$_ZSH_GIT_PROMPT_ASYNC_PID 2>/dev/null
else
# Kill just the child process since it wasn't placed in a new process
# group. If the suggestion strategy forked any child processes they may
# be orphaned and left behind.
kill -TERM $_ZSH_GIT_PROMPT_ASYNC_PID 2>/dev/null
fi
fi
# Fork a process to fetch the git status and open a pipe to read from it
exec {_ZSH_GIT_PROMPT_ASYNC_FD}< <(
# Tell parent process our pid
builtin echo $sysparams[pid]
_zsh_git_prompt_git_status
[[ -n "$ZSH_GIT_PROMPT_ENABLE_SECONDARY" ]] \
&& builtin echo -n "##secondary##" \
&& _zsh_git_prompt_git_status_secondary
)
# There's a weird bug here where ^C stops working unless we force a fork
# See https://github.com/zsh-users/zsh-autosuggestions/issues/364
command true
# Read the pid from the child process
read _ZSH_GIT_PROMPT_ASYNC_PID <&$_ZSH_GIT_PROMPT_ASYNC_FD
# When the fd is readable, call the response handler
zle -F "$_ZSH_GIT_PROMPT_ASYNC_FD" _zsh_git_prompt_callback
}
# Called when new data is ready to be read from the pipe
# First arg will be fd ready for reading
# Second arg will be passed in case of error
_ZSH_GIT_PROMPT_STATUS_OUTPUT=""
_ZSH_GIT_PROMPT_STATUS_SECONDARY_OUTPUT=""
function _zsh_git_prompt_callback() {
emulate -L zsh
local old_primary="$_ZSH_GIT_PROMPT_STATUS_OUTPUT"
local old_secondary="$_ZSH_GIT_PROMPT_STATUS_SECONDARY_OUTPUT"
local fd_data
local -a output
if [[ -z "$2" || "$2" == "hup" ]]; then
# Read output from fd
fd_data="$(cat <&$1)"
output=( ${(s:##secondary##:)fd_data} )
_ZSH_GIT_PROMPT_STATUS_OUTPUT="${output[1]}"
_ZSH_GIT_PROMPT_STATUS_SECONDARY_OUTPUT="${output[2]}"
if [[ "$old_primary" != "$_ZSH_GIT_PROMPT_STATUS_OUTPUT" ]] \
|| [[ "$old_secondary" != "$_ZSH_GIT_PROMPT_STATUS_SECONDARY_OUTPUT" ]] ; then
zle reset-prompt
zle -R
fi
# Close the fd
exec {1}<&-
fi
# Always remove the handler
zle -F "$1"
# Unset global FD variable to prevent closing user created FDs in the precmd hook
unset _ZSH_GIT_PROMPT_ASYNC_FD
}
function _zsh_git_prompt_precmd_hook() {
if [[ -n "$ZSH_GIT_PROMPT_FORCE_BLANK" ]]; then
_ZSH_GIT_PROMPT_STATUS_OUTPUT=""
_ZSH_GIT_PROMPT_STATUS_SECONDARY_OUTPUT=""
fi
_zsh_git_prompt_async_request
}
if (( $+commands[git] )); then
if [[ -z "$ZSH_GIT_PROMPT_NO_ASYNC" ]]; then
autoload -U add-zsh-hook \
&& add-zsh-hook precmd _zsh_git_prompt_precmd_hook
function gitprompt() {
echo -n "$_ZSH_GIT_PROMPT_STATUS_OUTPUT"
}
function gitprompt_secondary() {
echo -n "$_ZSH_GIT_PROMPT_STATUS_SECONDARY_OUTPUT"
}
else
function gitprompt() {
_zsh_git_prompt_git_status
}
function gitprompt_secondary() {
[[ -n "$ZSH_GIT_PROMPT_ENABLE_SECONDARY" ]] \
&& _zsh_git_prompt_git_status_secondary
}
fi
else
function gitprompt() { }
function gitprompt_secondary() { }
fi
# vim: ft=zsh