prompt items fit docu
[zsh.git] / .zsh / functions / prompt_ganneff_setup
1 # -*- mode:sh -*-
2
3 # This prompt is based on work of other people.
4 #
5 # The prompt itself is based on various others:
6 # - The "design" is taken from Phil!'s ZSH prompt, as found on
7 # http://aperiodic.net/phil/prompt/
8 #
9 # - The tech using zstyle is inspired by the prompt as used by
10 # the grml-live system <http://grml.org>, see
11 # http://git.grml.org/?p=grml-etc-core.git;a=summary for details
12 # on theirs.
13 # A good number of their support functions are also taken, though
14 # they got renamed from grml_* to ganneff_* to have a single namespace
15 # here.
16 #
17 # - The winch function as seen in the prompt theme "bart", delivered
18 # with zsh
19
20 prompt_ganneff_help () {
21 cat <<__EOF0__
22 prompt ganneff
23
24 This is the prompt as used by (who would have guessed) Ganneff. By
25 default it is a two-line prompt. You can find its latest version at
26 http://git.ganneff.de/cgi-bin/gitweb.cgi?p=zsh.git;a=summary
27
28 The prompt itself is based on various others:
29 - The "design" is taken from Phil!'s ZSH prompt, as found on
30 http://aperiodic.net/phil/prompt/
31
32 - The technic using zstyle is inspired by the prompt as used by
33 the grml-live system <http://grml.org>, see
34 http://git.grml.org/?p=grml-etc-core.git;a=summary for details
35 on theirs.
36
37 The prompt integrates with zsh's prompt themes system and uses the
38 zstyle system for its configuration. It is configurable as much as
39 seems to make sense. In particular, these aspects are customisable:
40
41 - The items used in the prompt and their order (e.g. you can
42 remove \`user' from the list of activated items, which will
43 cause the user name to be omitted from the prompt string).
44
45 - The attributes used with the items are customisable via strings
46 used before and after the actual item.
47
48 - An itemset for "small terminals" can be provided. That is, if the
49 length of the upper line exceeds the terminal width, various
50 items get removed from the prompt to (hopefully) make it still
51 look good. Obviously this only works down to a limit.
52
53 The available items are: at, battery, change-root, date, history,
54 host, jobs, newline, path, percent, rc, rc-always, shell-level,
55 time, user, vcs, ulcorner, llcorner, urcorner, lrcorner, line, pts,
56 privileges, openparentheses, closeparentheses, openbracket,
57 closebracket, openbraces, closebraces, openanglebracket,
58 closeanglebracket, colon, pipe, space, flexline
59
60 Most of those should be self-explanatory, some may need more:
61 line - Draws a single line character
62 XYcorner - Draws a corner. The chars to replace XY are
63 X=u for upper, l for lower
64 Y=l for left, r for right
65 flexline - Same as line, but flexible length to fill remaining space
66 Only works in a two-line prompt in the upper line, that
67 is, a newline MUST appear in the setup!
68
69 Should they not fit your need, there is an easy way to have the
70 prompt include basically anything you want. Just define your own
71 items. For that you simply define them inside the
72 ':prompt:ganneff:extra:' namespace.
73
74 Example: To have the prompt include the value of the variable
75 \$FOOBAR, in red and have the variable updated by the function
76 jj_foobar, use the following:
77
78 zstyle ':prompt:ganneff:extra:foobar' pre '${PR_RED}'
79 zstyle ':prompt:ganneff:extra:foobar' post '${PR_NO_COLOR}'
80 zstyle ':prompt:ganneff:extra:foobar' token '$FOOBAR'
81 zstyle ':prompt:ganneff:extra:foobar' precmd jj_foobar
82
83 Now you can add the token \`foobar' to the list of items and voila,
84 your own stuff appears.
85
86 Note that the function part only works in zsh 4.3.5 or later (I've
87 been to lazy to replace add-zsh-hook with something for the older
88 versions).
89
90
91 The configuration of the predefined items can also be changed using
92 zsh's \`zstyle' mechanism. The context, that is used while looking
93 up styles is:
94
95 ':prompt:ganneff:<left-or-right>:<full-or-small>:<subcontext>'
96
97 Here <left-or-right> is either \`left' or \`right', signifying
98 whether the style should affect the left or the right prompt.
99
100 <full-or-small> is either \`full' or \`small' and only valid in the
101 item selection, signifying wether the item list should affect the
102 full length or the "small terminal" prompt. As "small terminal"
103 prompt does not display the right side prompt, <full-or-small> has
104 no effect on the right side prompt.
105
106 <subcontext> is either \`setup' or 'items:<item>', where \`<item>'
107 is one of the available items.
108
109 The styles/settings available under ':prompt:ganneff:':
110
111 - vcs_info (boolean): If \`true' (the default), use \`vcs_info'.
112
113 - set_vcs_info_defaults (boolean): If \`true' (the default),
114 various vcs_info settings will be done. If unset or \`false'
115 it is assumed that the user has already done this.
116
117 The default setup is:
118 zstyle ':vcs_info:*' max-exports 1
119 zstyle ':vcs_info:*' use-prompt-escapes
120 zstyle ':vcs_info:*' use_simple
121 zstyle ':vcs_info:*' stagedstr "!"
122 zstyle ':vcs_info:*' unstagedstr "?"
123 zstyle ':vcs_info:*' check-for-changes true
124 zstyle ':vcs_info:(sv[nk]|bzr):*' branchformat \
125 "\${PR_YELLOW}%b%{\${PR_RED}%}:\${PR_YELLOW}%r\${PR_NO_COLOR}"
126 zstyle ':vcs_info:*' formats \
127 "(\${PR_YELLOW}%s\${PR_NO_COLOR})-[\${PR_GREEN}%b\${PR_NO_COLOR}]%m%u%c "
128 zstyle ':vcs_info:*' actionformats \
129 "(\${PR_YELLOW}%s\${PR_NO_COLOR})-[\${PR_GREEN}%b\${PR_NO_COLOR}-\${PR_RED}(%a)\${PR_NO_COLOR}]%m%u%c "
130
131 - colors (boolean): If \`true' (the default), use colors. b/w
132 otherwise.
133
134 - nicelines (boolean): If \`true' (the default), use special
135 magic to draw nice lines and corners. Might not work on every
136 terminal. If false, draws using plain ascii characters.
137 Linux console also seems to identify itself as if it could,
138 but then doesn't do drawing right (not in our way at least).
139
140 - battery (string): If \`none' then no battery function at all.
141 If either \`ibam' or \`acpi' then use those tools to gather
142 the current battery level.
143
144 - use-rprompt (boolean): If \`true' (the default), print the
145 right side prompt.
146
147 - items (list): The list of items used in the prompt. If \`vcs' is
148 present in the list, the theme's code invokes \`vcs_info'
149 accordingly. Default (left): rc change-root user at host path vcs
150 percent; Default (right): sad-smiley
151
152 Note that both, nicelines and colors are forced to false in case the
153 TERM variable is set to dumb.
154
155 Available styles in 'items:<item>' are: pre, post, token and precmd.
156 Pre and Post are strings that are inserted before (pre) and after
157 (post) the item in question. Token is the token itself that gets
158 inserted into PS1. Thus, the following would cause the
159 user name to be printed in red instead of the default blue:
160
161 zstyle ':prompt:ganneff:*:items:user' pre '\${PR_RED}'
162
163 and
164
165 zstyle ':prompt:ganneff:*:items:date' token '%D{%Y-%m-%d [d:%j/w:%V]}'
166
167 will output a very different date token compared with the default
168 definition of '%D{%Y-%m-%d}'.
169
170 Note, that in the above example the \`post' style may remain at its
171 default value, because its default value is '\${PR_NO_COLOR}', which
172 turns the foreground text attribute off (which is exactly, what is
173 still required with the new \`pre' value).
174
175 Possible values for the colors: \${PR_XXX} and \${PR_BOLD_XXX} with
176 XXX replaced with one of: RED GREEN YELLOW BLUE MAGENTA CYAN WHITE BLACK.
177 \${PR_NO_COLOR} resets color.
178 Using the \${PR_XXX} values instead of zsh's internal \%F{xxx} allows
179 the usage of the colors style to turn of prompt colors easily.
180
181 Last note: You really want to have "setopt promptsubst", otherwise you
182 won't like this prompt.
183 __EOF0__
184 }
185
186 prompt_ganneff_setup () {
187 emulate -L zsh
188 setopt nolocaltraps
189
190 # We forcefully overwrite colors and nicelines setting for a dumb
191 # terminal.
192 if [[ "$TERM" == "dumb" ]]; then
193 zstyle ':prompt:ganneff' colors false
194 zstyle ':prompt:ganneff' nicelines false
195 fi
196
197 # See if we can and should use extended characters to look nicer.
198 if zstyle -t ':prompt:ganneff' nicelines; then
199 typeset -A altchar
200 set -A altchar ${(s..)terminfo[acsc]}
201 # Some stuff to help us draw nice lines
202 PR_SET_CHARSET="%{$terminfo[enacs]%}"
203 PR_SHIFT_IN="%{$terminfo[smacs]%}"
204 PR_SHIFT_OUT="%{$terminfo[rmacs]%}"
205 PR_HBAR=${altchar[q]:--}
206 PR_ULCORNER=${altchar[l]:--}
207 PR_LLCORNER=${altchar[m]:--}
208 PR_LRCORNER=${altchar[j]:--}
209 PR_URCORNER=${altchar[k]:--}
210 else
211 PR_SET_CHARSET=""
212 PR_SHIFT_IN=""
213 PR_SHIFT_OUT=""
214 PR_HBAR="-"
215 PR_ULCORNER=""
216 PR_LLCORNER=""
217 PR_LRCORNER=""
218 PR_URCORNER=""
219 fi
220
221 if zstyle -t ':prompt:ganneff' colors; then
222 if [[ -n "${BLUE}" ]] && [[ -n "${YELLOW}" ]]; then
223 # Two defined, so we assume someone already defined themself
224 # the colors. We just use them and not setup our own.
225 for color in RED GREEN YELLOW BLUE MAGENTA CYAN WHITE BLACK GREY; do
226 eval PR_$color='%{${(P)color}%}'
227 eval PR_BOLD_$color='%{$terminfo[bold]${(LP)color}%}'
228 done
229 PR_NO_COLOR="%{${reset_color}%}"
230 else
231 # Seems like colors are not defined, so set them up
232 # This duplicates docolors from 01_Terminfo.zsh, but is here to have
233 # prompt_ganneff_setup not require anything else from my dotfiles
234 if autoload -Uz colors && colors 2>/dev/null ; then
235 for COLOR in RED GREEN YELLOW BLUE MAGENTA CYAN WHITE BLACK GREY; do
236 eval PR_$COLOR='%{$fg_no_bold[${(L)COLOR}]%}'
237 eval PR_BOLD_$COLOR='%{$fg_bold[${(L)COLOR}]%}'
238 done
239 eval PR_NO_COLOR='%{$reset_color%}'
240 else
241 PR_BLUE=$'%{\e[1;34m%}'
242 PR_RED=$'%{\e[1;31m%}'
243 PR_GREEN=$'%{\e[1;32m%}'
244 PR_CYAN=$'%{\e[1;36m%}'
245 PR_WHITE=$'%{\e[1;37m%}'
246 PR_MAGENTA=$'%{\e[1;35m%}'
247 PR_YELLOW=$'%{\e[1;33m%}'
248 PR_NO_COLOR=$'%{\e[0m%}'
249 fi
250 fi
251 else
252 PR_BLUE=''
253 PR_RED=''
254 PR_GREEN=''
255 PR_CYAN=''
256 PR_WHITE=''
257 PR_MAGENTA=''
258 PR_YELLOW=''
259 PR_NO_COLOR="%{${reset_color}%}"
260 fi
261
262 # Easy things first.
263 # The secondary prompt, printed when the shell needs more
264 # information to complete a command. %_ displays any shell constructs
265 # or quotation marks which are currently being processed.
266 if zstyle -t ':prompt:ganneff' nicelines; then
267 PS2='${PR_CYAN}${PR_SHIFT_IN}${PR_HBAR}${PR_BLUE}${PR_HBAR}${PR_SHIFT_OUT}\
268 (${PR_GREEN}%_${PR_BLUE})\
269 ${PR_SHIFT_IN}${PR_HBAR}${PR_CYAN}${PR_HBAR}${PR_SHIFT_OUT}${PR_NO_COLOR} '
270 else
271 PS2='-(%_)- '
272 fi
273 # selection prompt used within a select loop.
274 PS3='?# '
275 # the execution trace prompt (setopt xtrace). default: '+%N:%i>'
276 PS4='+%N:%i:%_> '
277
278 if zstyle -t ':prompt:ganneff' vcs_info && \
279 is439 && autoload -Uz vcs_info && vcs_info; then
280 if zstyle -t ':prompt:ganneff' set_vcs_info_defaults; then
281 # gather version control information for inclusion in a prompt
282 # we will only be using one variable, so let the code know now.
283 zstyle ':vcs_info:*' max-exports 1
284 zstyle ':vcs_info:*' use-prompt-escapes
285 zstyle ':vcs_info:*' use_simple
286 zstyle ':vcs_info:*' stagedstr "!"
287 zstyle ':vcs_info:*' unstagedstr "?"
288 zstyle ':vcs_info:*' check-for-changes true
289
290 # change vcs_info formats for the prompt
291 zstyle ':vcs_info:(sv[nk]|bzr):*' branchformat "${PR_YELLOW}%b%{${PR_RED}%}:${PR_YELLOW}%r${PR_NO_COLOR}"
292 zstyle ':vcs_info:*' formats "(${PR_YELLOW}%s${PR_NO_COLOR})-[${PR_GREEN}%b${PR_NO_COLOR}]%m%u%c "
293 zstyle ':vcs_info:*' actionformats "(${PR_YELLOW}%s${PR_NO_COLOR})-[${PR_GREEN}%b${PR_NO_COLOR}-${PR_RED}(%a)${PR_NO_COLOR}]%m%u%c "
294 fi
295 fi
296
297 # These maps define default tokens and pre-/post-decoration for items to be
298 # used within the themes. All defaults may be customised in a context sensitive
299 # matter by using zsh's `zstyle' mechanism.
300 typeset -gA ganneff_prompt_pre_default \
301 ganneff_prompt_post_default \
302 ganneff_prompt_token_default
303
304 ganneff_prompt_pre_default=(
305 at ''
306 battery ' '
307 change-root ''
308 date '${PR_BLUE}'
309 history '${PR_GREEN}'
310 host '${PR_RED}'
311 jobs '${PR_CYAN}'
312 newline ''
313 path '${PR_MAGENTA}'
314 percent ''
315 rc '${PR_RED}'
316 rc-always ''
317 shell-level '${PR_RED}'
318 time '${PR_BLUE}'
319 user '${PR_BLUE}'
320 vcs ''
321 ulcorner '${PR_SHIFT_IN}'
322 llcorner '${PR_SHIFT_IN}'
323 urcorner '${PR_SHIFT_IN}'
324 lrcorner '${PR_SHIFT_IN}'
325 line '${PR_SHIFT_IN}'
326 pts ''
327 privileges ''
328 openparentheses ''
329 closeparentheses ''
330 openbracket ''
331 closebracket ''
332 openbraces ''
333 closebraces ''
334 openanglebracket ''
335 closeanglebracket ''
336 pipe ''
337 space ''
338 colon ''
339 flexline '${PR_SHIFT_IN}'
340 )
341
342
343 ganneff_prompt_post_default=(
344 at ''
345 battery ''
346 change-root ''
347 date '${PR_NO_COLOR}'
348 history '${PR_NO_COLOR}'
349 host '${PR_NO_COLOR}'
350 jobs '${PR_NO_COLOR}'
351 newline ''
352 path '${PR_NO_COLOR}'
353 percent ''
354 rc '${PR_NO_COLOR}'
355 rc-always ''
356 shell-level '${PR_NO_COLOR}'
357 time '${PR_NO_COLOR}'
358 user '${PR_NO_COLOR}'
359 vcs ''
360 ulcorner '${PR_SHIFT_OUT}'
361 llcorner '${PR_SHIFT_OUT}'
362 urcorner '${PR_SHIFT_OUT}'
363 lrcorner '${PR_SHIFT_OUT}'
364 line '${PR_SHIFT_OUT}'
365 pts ''
366 privileges ''
367 openparentheses ''
368 closeparentheses ''
369 openbracket ''
370 closebracket ''
371 openbraces ''
372 closebraces ''
373 openanglebracket ''
374 closeanglebracket ''
375 pipe ''
376 space ''
377 colon ''
378 flexline '${PR_SHIFT_OUT}'
379 )
380
381 ganneff_prompt_token_default=(
382 at '@'
383 battery 'PERCENT'
384 change-root 'debian_chroot'
385 date '%D{%Y-%m-%d}'
386 history '{#%!}'
387 host '%m'
388 jobs '[%j running job(s)] '
389 newline $'\n'
390 path '%40<..<%~%<<'
391 percent '% # '
392 rc '%(?..%? )'
393 rc-always '%?'
394 shell-level '%(2L.%L.)'
395 time '%D{%H:%M:%S}'
396 user '%n'
397 vcs '0'
398 ulcorner '${PR_ULCORNER}'
399 llcorner '${PR_LLCORNER}'
400 urcorner '${PR_URCORNER}'
401 lrcorner '${PR_LRCORNER}'
402 line '${PR_HBAR}'
403 pts '%y'
404 privileges '%#'
405 openparentheses '('
406 closeparentheses ')'
407 openbracket '['
408 closebracket ']'
409 openbraces '{'
410 closebraces '}'
411 openanglebracket '<'
412 closeanglebracket '>'
413 pipe '|'
414 space ' '
415 colon ':'
416 flexline 'PR_FLEXLINE'
417 )
418
419 PR_FLEXLINE=""
420 add-zsh-hook precmd prompt_ganneff_precmd
421 # Call the winch function once to ensure the length gets calculated
422 # correctly
423 prompt_ganneff_winch
424 local pr_battery
425 zstyle -s ':prompt:ganneff' battery pr_battery
426 if [[ $pr_battery != "none" ]]; then
427 add-zsh-hook precmd prompt_ganneff_battery
428 prompt_ganneff_battery
429 fi
430
431 functions[TRAPWINCH]="${functions[TRAPWINCH]//prompt_ganneff_winch}
432 prompt_ganneff_winch"
433 }
434
435 prompt_ganneff_precmd () {
436 emulate -L zsh
437 setopt nolocaltraps
438
439 zstyle -t ':prompt:ganneff' vcs_info && vcs_info
440 local -a left_items right_items
441
442 left_items=(ulcorner line openparantheses user at host colon pts closeparantheses line history
443 line shell-level line flexline openparentheses path closeparentheses line urcorner newline
444 llcorner line rc openparentheses time closeparentheses line vcs line change-root pipe space)
445 PR_PS1=""; nomore=0
446 ganneff_prompt_addto PS1 full "${left_items[@]}"
447 if zstyle -T ":prompt:ganneff:right:setup" use-rprompt; then
448 right_items=(pipe line openparentheses date closeparentheses line lrcorner)
449 ganneff_prompt_addto RPS1 full "${right_items[@]}"
450 fi
451
452 # Now a kind-of-hack to reduce the prompt when we run out of space.
453 promptsize=${#PR_PS1}
454 if [[ ${promptsize} -lt ${TERMWIDTH} ]]; then
455 # the q here should be ${PR_HBAR}, but for some reason zsh didn't like me,
456 # so meh
457 if zstyle -t ':prompt:ganneff' nicelines && \
458 [[ "$TERM" != dumb ]] && [[ "$TERM" != "linux" ]]; then
459 PR_FLEXLINE="${(l.(($TERMWIDTH - $promptsize ))..q.)}"
460 else
461 PR_FLEXLINE="${(l.(($TERMWIDTH - $promptsize ))..-.)}"
462 fi
463 else
464 # Small size prompt is needed
465 left_items=(ulcorner line openparentheses user at host closeparentheses line openparentheses
466 path closeparentheses newline
467 llcorner line rc openparentheses time closeparentheses line vcs line pipe space)
468 ganneff_prompt_addto PS1 small "${left_items[@]}"
469 # And with a small prompt, we don't show the right size at all.
470 RPS1=""
471 fi
472 }
473
474 prompt_ganneff_length () {
475 (( TERMWIDTH = ${COLUMNS} - 1 ))
476 }
477
478 prompt_ganneff_winch () {
479 emulate -L zsh
480 setopt nolocaltraps noksharrays unset
481
482 # Delete ourself from TRAPWINCH if not using our precmd insert.
483 [[ $precmd_functions = *prompt_ganneff_precmd* ]] && prompt_ganneff_length ||
484 functions[TRAPWINCH]="${functions[TRAPWINCH]//prompt_ganneff_winch}"
485 }
486
487 ganneff_typeset_and_wrap () {
488 emulate -L zsh
489 local target="$1"
490 local new="$2"
491 local left="$3"
492 local right="$4"
493
494 if (( ${+parameters[$new]} )); then
495 typeset -g "${target}=${(P)target}${left}${(P)new}${right}"
496 fi
497 }
498
499 ganneff_prompt_addto () {
500 emulate -L zsh
501 local target="$1"
502 local size="$2"
503 local lr it apre apost new v prefunc
504 local -a items
505 shift
506 shift
507
508 [[ $target == PS1 ]] && lr=left || lr=right
509 zstyle -a ":prompt:ganneff:${lr}:${size}:setup" items items || items=( "$@" )
510 typeset -g "${target}="
511 for it in "${items[@]}"; do
512 zstyle -s ":prompt:ganneff:${lr}:items:$it" pre apre \
513 || zstyle -s ":prompt:ganneff:extra:$it" pre apre \
514 || apre=${ganneff_prompt_pre_default[$it]}
515 zstyle -s ":prompt:ganneff:${lr}:items:$it" post apost \
516 || zstyle -s ":prompt:ganneff:extra:$it" post apost \
517 || apost=${ganneff_prompt_post_default[$it]}
518 zstyle -s ":prompt:ganneff:${lr}:items:$it" token new \
519 || zstyle -s ":prompt:ganneff:extra:$it" token new \
520 || new=${ganneff_prompt_token_default[$it]}
521
522 # Now check if there is the wish for a precmd function and if so,
523 # run it
524 zstyle -s ":prompt:ganneff:extra:$it" precmd prefunc
525 if [[ -n "${prefunc}" ]]; then
526 ${prefunc} || true
527 fi
528
529 typeset -g "${target}=${(P)target}${apre}"
530 # Store the expanded value in PR_PS1, as we use that for length calculations
531 [[ $it == "newline" ]] && nomore=1
532 if [[ $nomore -eq 0 ]] && [[ $it != "flexline" ]] && PR_PS1+=${(e%)new}
533 case $it in
534 battery)
535 ganneff_typeset_and_wrap $target $new '' ''
536 ;;
537 change-root)
538 ganneff_typeset_and_wrap $target $new '(' ')'
539 ;;
540 flexline)
541 typeset -g "${target}=${(P)target}\${${new}}"
542 ;;
543 ganneff-chroot)
544 if [[ -n ${(P)new} ]]; then
545 typeset -g "${target}=${(P)target}(CHROOT)"
546 fi
547 ;;
548 vcs)
549 v="vcs_info_msg_${new}_"
550 if (( ${+parameters[$v]} )) && [[ -n "${(P)v}" ]]; then
551 typeset -g "${target}=${(P)target}${(P)v}"
552 fi
553 ;;
554 *) typeset -g "${target}=${(P)target}${new}" ;;
555 esac
556 typeset -g "${target}=${(P)target}${apost}"
557 done
558 }
559
560 prompt_ganneff_battery() {
561 zstyle -s ':prompt:ganneff' battery pr_battery
562 case $pr_battery in
563 ibam)
564 local ACPIDATA=$(ibam --percentbattery 2>/dev/null || echo "")
565 PERCENT=${${ACPIDATA[(f)1]}[(w)-2]}
566 ;;
567 acpi)
568 PERCENT="${${"$(acpi 2>/dev/null)"}/(#b)[[:space:]]#Battery <->: [^0-9]##, (<->)%*/${match[1]}}"
569 ;;
570 *)
571 PERCENT=""
572 ;;
573 esac
574
575 if [[ -z "$PERCENT" ]] ; then
576 PERCENT="No battery or $pr_battery not present "
577 else
578 if [[ "$PERCENT" -lt 20 ]] ; then
579 PERCENT="warning: ${PERCENT}%%"
580 else
581 PERCENT="${PERCENT}%%"
582 fi
583 fi
584 }
585
586 is439(){
587 [[ $ZSH_VERSION == 4.3.<9->* || $ZSH_VERSION == 4.<4->* \
588 || $ZSH_VERSION == <5->* ]] && return 0
589 return 1
590 }
591
592 prompt_ganneff_setup "$@"