# vim:ft=zsh fdm=marker # $Id: prompt_jde_setup,v 1.15 2004/10/27 03:03:59 jde Exp $ # "jde" prompt theme -- a multiline theme of great tweaking zmodload -i zsh/parameter || return 1 ################################################################################ ### HELP {{{ ################################################################################ prompt_jde_help () { setopt localoptions nocshnullcmd noshnullcmd [[ $ZSH_VERSION < 3.1.6-dev-20 ]] && print 'Requires 3.1.6-dev-19 plus zsh-workers/10168.'$'\n' <<-\EOF This prompt provides a lot of information attractively. Observe: user@host login tty new mail counts uptime loadavg 5,10,15 $PWD v v v v v v v v v v --| jde@zigzag : pts/468 : IN:3 m:22 p:58 |--| 25d03:13 : 0.10 0.07 0.01 |---| ~ |-- --| ?0 : !1004 : % |- -| 23:41 : 04-08-20 |-- ^ ^ ^ ^ | history # time date last exitcode This prompt is tested on Linux and OpenBSD; on the latter, it is required to have kernfs mounted on /kern for some features to work properly, although there are simple (slower) workarounds if you edit the script. EOF [[ $(read -Ek 1 "?${(%):-%S[press return]%s}") == [Qq] ]] && print -nP '\r%E' && return <<-\EOF The prompt is fully colorized, but as of now you have to edit the script to change the colors. (I.e., it is not themable.) Additionally, the scheme uses the alternate charset available with vt100-type terminals (including nearly every xterm flavor) to draw lines and angles. With some combinations of terminals and programs, this may cause problems; the feature can be turned off with: prompt jde -a or prompt jde noaltchar In this case, standard ascii characters will be used instead. (The example above is the ascii mode.) You can re-enable line drawing with: prompt jde altchar EOF [[ $(read -Ek 1 "?${(%):-%S[press return]%s}") == [Qq] ]] && print -nP '\r%E' && return <<-\EOF The mail count feature will read the mailboxes identified in the zsh $PROMPT_JDE_MAILBOXES array parameter, which should have the form: (TAG:/path/to/mailbox BOX2:/alt/box) The script will read the mailboxes identified therein, and use the "tag" as the key displayed next to the count of new mail. As of now, the script only deals with maildir-style mailboxes. It will silently ignore any non-maildir identified in $PROMPT_JDE_MAILBOXES, including directories containing other mailboxes (which are supported by the zsh implementation of mail checking). When new mail arrives in any of the tracked mailboxes, the prompt will change color briefly (one line). If you do not want this feature, start the prompt with prompt jde -m or prompt jde nomailcount With this invocation, the prompt theme will work normally, but will omit the mail count widget. To reenable this feature, use: prompt jde mailcount EOF [[ $(read -Ek 1 "?${(%):-%S[press return]%s}") == [Qq] ]] && print -nP '\r%E' && return <<-\EOF When the screen is resized to the point where the "widgets" get sqeezed, the script will attempt to shrink things appropriately. First, the center widget on the first line (containing uptime and load average info) will disappear. If yet more room is needed, the $PWD element will be truncated on the left-hand side. The top center (uptime) widget, if visible, will always try to remain centered. EOF } #############################################################################}}} ### UPTIME {{{ ################################################################################ prompt_jde_uptime () { setopt localoptions noksharrays local -i upSeconds secs mins hours days case "${OSTYPE:l}" in linux*|cygwin*) # optimization. -d. means delimiter is the decimal. Clever? read -d. upSeconds 0 )); then echo -n "${days}d" fi printf '%.2d:%.2d' $hours $mins } #############################################################################}}} ### MAILCOUNTS {{{ ################################################################################ # Count new mail in mailboxes. The resulting counts are put into assoc arrays # PROMPT_JDE_MAILCOUNTS and PROMPT_JDE_MAILCOUNTS_OLD (holding former results. # TODO: support mbox mailboxes. # TODO: What do do with directory-of-mailboxes? Just ignore I guess. # TODO: Is there some smarter way of determining the keys? It is very basic now. prompt_jde_mailcounts () { [[ $PROMPT_JDE_MAILCOUNT == no ]] && return setopt localoptions nullglob local -h line maildir files count key typeset -gA PROMPT_JDE_MAILCOUNTS PROMPT_JDE_MAILCOUNTS_OLD PROMPT_JDE_MAILCOUNTS_OLD=() #${${(s::)${${(s:?:)x}[1]:t}}[1]} for line in $PROMPT_JDE_MAILBOXES; do maildir=${${(s.:.)line}[2]} key=${${(s.:.)line}[1]} [[ -d $maildir/new ]] || continue files=($maildir/new/*) count=${#files} PROMPT_JDE_MAILCOUNTS_OLD[$key]=$PROMPT_JDE_MAILCOUNTS[$key] PROMPT_JDE_MAILCOUNTS[$key]=$count done } #############################################################################}}} ### LOADAVG {{{ ################################################################################ prompt_jde_loadavg () { setopt localoptions noksharrays local -h loadavg case "${OSTYPE:l}" in linux*|cygwin*) local -h one five fifteen rest # read /proc/loadavg as a speedup over $(uptime|sed...) read one five fifteen rest >) with the length equal to # the 2nd line of PS1 (the part between $grbeg and # $grend). Then we pad the string with plenty of spaces. "%\${#:-$ps1line2}> >" # The following prints the number of words in %_ "$invtext \${(w)#\${(%):-%_}}" # The following prints the last word of %_ #"$normtext \${\${(%):-%_}[(w)-1]}" "$normtext %1_" # DUH!! # Here is the big space pad needed to ensure we get to # the right side of PS1's 2nd line. Should I be more # precise, e.g. using ${(l...)}? Nah. " " "%<<" # end the truncation "$pr_shift_in" "$grend" "$pr_shift_out" "$rs" $' ' ) PS2="${(j::)ps2}" } #############################################################################}}} ### WINCH {{{ ################################################################################ #TODO: this is not getting called prompt_jde_winch () { setopt localoptions noksharrays # Delete ourself from TRAPWINCH if not using our precmd insert. [[ $functions[precmd] = *prompt_jde_precmd* ]] && prompt_jde_ps1 || functions[TRAPWINCH]="${functions[TRAPWINCH]//prompt_jde_winch}" } #############################################################################}}} ### SETUP {{{ ################################################################################ prompt_jde_setup () { # zsh options we require: prompt_* prompt_opts=( cr subst percent ) setopt localoptions noksharrays typeset -g PROMPT_JDE_ALTCHAR PROMPT_JDE_MAILCOUNT : ${PROMPT_JDE_ALTCHAR:=yes} : ${PROMPT_JDE_MAILCOUNT:=yes} # A few extra niceties ... repeat 1 case "$1:l" in (off|disable) functions[precmd]="${functions[precmd]//prompt_jde_precmd}" functions[preexec]="${functions[preexec]//prompt_jde_preexec}" functions[TRAPWINCH]="${functions[TRAPWINCH]//prompt_jde_winch}" [[ $prompt_theme[1] = jde ]] && PS1=${${(f)PS1}[-1]} return 1 ;; (on|enable) shift [[ $prompt_theme[1] = jde ]] && break ;& (-a|noaltchar) PROMPT_JDE_ALTCHAR=no ;; (altchar) PROMPT_JDE_ALTCHAR=yes ;; (-m|nomailcount) PROMPT_JDE_MAILCOUNT=no ;; (mailcount) PROMPT_JDE_MAILCOUNT=yes ;; (*) # nothing ;; esac [[ -z $PROMPT_JDE_MAILBOXES ]] && PROMPT_JDE_MAILCOUNT=no prompt_jde_ps1 # Paste our special commands into precmd and TRAPWINCH functions[precmd]="${functions[precmd]//prompt_*_precmd} prompt_jde_precmd" functions[preexec]="${functions[preexec]//prompt_*_preexec \"\$1\"} prompt_jde_preexec \"\$1\"" functions[TRAPWINCH]="${functions[TRAPWINCH]//prompt_jde_winch} prompt_jde_winch" return 0 } # }}} [[ -o kshautoload ]] || prompt_jde_setup "$@"