-
Notifications
You must be signed in to change notification settings - Fork 1
/
zsh.prompt
78 lines (60 loc) · 2.14 KB
/
zsh.prompt
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
# fastest possible way to check if repo is dirty
prompt_git_dirty() {
vcs_info
command git st &>/dev/null
(($? == 128)) && return
local pre=" git/"
local branch_name="$vcs_info_msg_0_"
# check if we're in a git repo
command git rev-parse --is-inside-work-tree &>/dev/null
# check if it's dirty
command git diff --quiet --ignore-submodules HEAD &>/dev/null
(($? == 0)) && echo "$pre%F{green}$branch_name"
(($? == 1)) && echo "$pre%F{red}$branch_name"
}
# string length ignoring ansi escapes
prompt_string_length() {
echo ${#${(S%%)1//(\%([KF1]|)\{*\}|\%[Bbkf])}}
}
prompt_precmd() {
# shows the full path in the title
print -Pn '\e]0;%~\a'
# git info
vcs_info
local prompt_preprompt="\n%F{cyan}%~%F{242}`prompt_git_dirty` $prompt_username%f %F{yellow}%f"
print -P $prompt_preprompt
# check async if there is anything to pull
(( ${PURE_GIT_PULL:-1} )) && {
# check if we're in a git repo
command git rev-parse --is-inside-work-tree &>/dev/null &&
# check check if there is anything to pull
command git fetch &>/dev/null &&
# check if there is an upstream configured for this branch
command git rev-parse --abbrev-ref @'{u}' &>/dev/null && {
local arrows=''
(( $(command git rev-list --right-only --count HEAD...@'{u}' 2>/dev/null) > 0 )) && arrows='⇣'
(( $(command git rev-list --left-only --count HEAD...@'{u}' 2>/dev/null) > 0 )) && arrows+='⇡'
print -Pn "\e7\e[A\e[1G\e[`prompt_string_length $prompt_preprompt`C%F{cyan}${arrows}%f\e8"
}
} &!
# reset value since `preexec` isn't always triggered
unset cmd_timestamp
}
prompt_setup() {
# prevent percentage showing up
# if output doesn't end with a newline
export PROMPT_EOL_MARK=''
prompt_opts=(cr subst percent)
zmodload zsh/datetime
autoload -Uz add-zsh-hook
autoload -Uz vcs_info
add-zsh-hook precmd prompt_precmd
zstyle ':vcs_info:*' enable git
zstyle ':vcs_info:git*' formats '%b'
zstyle ':vcs_info:git*' actionformats ' %b|%a'
# show username@host if logged in through SSH
[[ "$SSH_CONNECTION" != '' ]] && prompt_username='%n@%m '
# prompt turns red if the previous command didn't exit with 0
PROMPT='%(?.%F{magenta}.%F{red})λ%f '
}
prompt_setup "$@"