-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprompt_sfz_setup
186 lines (161 loc) · 6.53 KB
/
prompt_sfz_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
# sfz prompt
# by Mike Reinhardt: https://github.com/mreinhardt/sfz-prompt.zsh
#
# Based on Lean by Miek Gieben: https://github.com/miekg/lean
#
# MIT License
PROMPT_SFZ_CHAR=${PROMPT_SFZ_CHAR-"§"}
PROMPT_SFZ_TMUX=${PROMPT_SFZ_TMUX-"™ "}
PROMPT_SFZ_PATH_UNTRUNCATED=${PROMPT_SFZ_PATH_UNTRUNCATED-""}
# subzero color palette, names correspond to terminal names but may not
# represent actual color.
prompt_sfz_colors () {
case $1 in
grey) echo "{234}";;
bgrey) echo "{060}";;
red) echo "{126}";;
bred) echo "{200}";;
green) echo "{035}";;
bgreen) echo "{048}";;
yellow) echo "{148}";;
byellow) echo "{190}";;
blue) echo "{025}";;
bblue) echo "{069}";;
magenta) echo "{092}";;
bmagenta) echo "{099}";;
cyan) echo "{074}";;
bcyan) echo "{081}";;
white) echo "{153}";;
bwhite) echo "{195}";;
esac
}
prompt_sfz_help() {
cat <<'EOF'
This is a one line prompt that tries to stay out of your face. It utilizes
the right side prompt for most information, like the CWD. The left side of
the prompt is only a '§'. The only other information shown on the left are
the jobs numbers of background jobs. When the exit code of a process isn't
zero the prompt turns red. If a process takes more then 5 (default) seconds
to run the total running time is shown in the next prompt.
Currently there is no configuration possible.
You can invoke it thus:
prompt sfz
EOF
}
# turns seconds into human readable time, 165392 => 1d 21h 56m 32s
prompt_sfz_human_time() {
local tmp=$1
local days=$(( tmp / 60 / 60 / 24 ))
local hours=$(( tmp / 60 / 60 % 24 ))
local minutes=$(( tmp / 60 % 60 ))
local seconds=$(( tmp % 60 ))
(( $days > 0 )) && echo -n "${days}d"
(( $hours > 0 )) && echo -n "${hours}h"
(( $minutes > 0 )) && echo -n "${minutes}m"
echo "${seconds}s"
}
# return git repo status indicators
prompt_sfz_git_dirty() {
# check if we're in a git repo
command git rev-parse --is-inside-work-tree &>/dev/null || return
# get set of status indicators
local git_status="$(git status --porcelain --ignore-submodules | cut -c1-2 | grep -o . | sort -u | paste -s -d'\0' - | tr -d ' ')"
# set ahead or behind upstream count
local git_ahead="$(git log --oneline @{u}.. 2> /dev/null | wc -l | tr -d '\n')"
local git_behind="$(git log --oneline ..@{u} 2> /dev/null | wc -l | tr -d '\n')"
if [[ $git_ahead -gt 0 ]]; then
local git_upstream_diff="%F$(prompt_sfz_colors bgreen)+$git_ahead"
elif [[ $git_behind -gt 0 ]]; then
local git_upstream_diff="%F$(prompt_sfz_colors bred)-$git_behind"
else
local git_upstream_diff=""
fi
[[ "${git_status}" == "" && "${git_upstream_diff}" == "" ]] && echo "" && return
prompt_sfz_git_status=""
if [[ "${git_upstream_diff}" ]]; then
prompt_sfz_git_status="${prompt_sfz_git_status} ${git_upstream_diff}"
fi
if [[ "${git_status}" ]]; then
# colorize indicators by status
local partial_git_status=$(echo "${git_status}%f" | \
sed -e "s/?/%F$(prompt_sfz_colors white)?%f/" \
-e "s/M/%B%F$(prompt_sfz_colors byellow)M%f%b/" \
-e "s/A/%B%F$(prompt_sfz_colors bgreen)A%f%b/" \
-e "s/D/%B%F$(prompt_sfz_colors bred)D%f%b/" \
-e "s/R/%F$(prompt_sfz_colors magenta)R%f/" \
-e "s/C/%F$(prompt_sfz_colors cyan)C%f/" \
-e "s/U/%B%F$(prompt_sfz_colors bblue)U%f%b/")
if [[ -n $prompt_sfz_git_status ]]; then
prompt_sfz_git_status="${partial_git_status}"
else
prompt_sfz_git_status="${prompt_sfz_git_status} ${partial_git_status}"
fi
fi
echo -n "$prompt_sfz_git_status"
}
# displays the exec time of the last command if set threshold was exceeded
prompt_sfz_cmd_exec_time() {
local stop=$EPOCHSECONDS
local start=${cmd_timestamp:-$stop}
integer elapsed=$stop-$start
(($elapsed > ${PROMPT_SFZ_CMD_MAX_EXEC_TIME:=5})) && prompt_sfz_human_time $elapsed
}
prompt_sfz_preexec() {
cmd_timestamp=$EPOCHSECONDS
# shows the current dir and executed command in the title when a process is active
print -Pn "\e]0;"
echo -nE "$PWD:t: $2"
print -Pn "\a"
}
prompt_sfz_pwd() {
local sfz_path="${PWD/$HOME/\~}"
if [[ $PROMPT_SFZ_PATH_UNTRUNCATED ]]; then
print "$sfz_path"
else
print "$sfz_path" | sed 's#\([.]*[^/]\)[^/]*/#\1/#g'
fi
}
prompt_sfz_precmd() {
vcs_info
rehash
local jobs
local prompt_sfz_jobs
unset jobs
for a (${(k)jobstates}) {
j=$jobstates[$a];i="${${(@s,:,)j}[2]}"
jobs+=($a${i//[^+-]/})
}
# print with [ ] and comma separated
prompt_sfz_jobs=""
[[ -n $jobs ]] && prompt_sfz_jobs="%F$(prompt_sfz_colors bgrey)[%F$(prompt_sfz_colors magenta)"${(j:,:)jobs}"%F$(prompt_sfz_colors bgrey)]%f "
local prompt_sfz_head
local prompt_sfz_tail
prompt_sfz_prompt_head="$prompt_sfz_jobs%F$(prompt_sfz_colors yellow)$prompt_sfz_tmux%F$(prompt_sfz_colors bgrey)$(prompt_sfz_pwd)"
prompt_sfz_prompt_tail="%(?..%B%F$(prompt_sfz_colors red)%?%f%b )%(!.%B%F$(prompt_sfz_colors bblue).%F$(prompt_sfz_colors blue))$PROMPT_SFZ_CHAR%f%b"
PROMPT="$prompt_sfz_prompt_head $prompt_sfz_prompt_tail "
export PROMPT_SFZ_COLORS=$prompt_sfz_colors
prompt_sfz_virtualenv=""
if [[ "$PROMPT_SFZ_VIRTUALENV_DISABLED" == '' && "$VIRTUAL_ENV" != '' ]]; then
prompt_sfz_virtualenv=" %f(%B%F$(prompt_sfz_colors byellow)${VIRTUAL_ENV:t}%f%b)"
fi
RPROMPT="%B%F$(prompt_sfz_colors bmagenta)$(prompt_sfz_cmd_exec_time)%f%b%F$(prompt_sfz_colors green)$vcs_info_msg_0_$(prompt_sfz_git_dirty)$prompt_sfz_virtualenv$prompt_sfz_ssh%f"
unset cmd_timestamp # reset value since `preexec` isn't always triggered
}
prompt_sfz_setup() {
prompt_opts=(cr subst percent)
zmodload zsh/datetime
autoload -Uz add-zsh-hook
autoload -Uz vcs_info
add-zsh-hook precmd prompt_sfz_precmd
add-zsh-hook preexec prompt_sfz_preexec
zstyle ':vcs_info:*' enable git
zstyle ':vcs_info:git*' formats ' %b'
zstyle ':vcs_info:git*' actionformats ' %b|%a'
if [[ "$SSH_CONNECTION" != '' ]]; then
prompt_sfz_ssh=" %F$(prompt_sfz_colors bcyan)%n@%m%f"
fi
if [[ "$TMUX" != '' ]]; then
prompt_sfz_tmux=$PROMPT_SFZ_TMUX
fi
}
prompt_sfz_setup "$@"