[Pkg-zsh-devel] Bug#792703: zsh: please improve newuser.zshrc.recommended and add new prompt_debian_setup

Carsten Hey carsten at debian.org
Thu Aug 13 10:02:10 UTC 2015


Hi,

I attached a new prompt_debian_setup and an RCS format diff between the
former one and this one.

The zstyle pattern now is :prompt:debian:${HOST-}:${USERNAME-}:${TERM-}:
in order to implement a variant of the suggested terminal blacklist in
future.  The final : allows additional future extensions, if needed.

Additional configurable things would make the prompt more complex, thus
I skipped implementing them for now (the suggested ones and additional
ones can be implemented later).  Franks other suggestions and some minor
changes are applied to the new prompt_debian_setup.

I think a recommended Debian .zshrc should also work on other systems,
at least in future releases of other Linux distributions.  Therefore,
submitting this prompt_debian_setup upstream would be great, but only
a Debian zsh maintainer would be able to say "We plan to add this prompt
to the Debian zsh package and enable it in the recommended .zshrc for
new users.  Please consider applying this patch.".  According to the
channel topic, the zsh 5.0.9 release is expected to happen soonish.


Carsten
-------------- next part --------------
# debian prompt theme

prompt_debian_help () {
    cat <<'EOF'
This prompt color-scheme-able via zstyle.  If the variabale TERM is set to
'dumb', colors are disabled, though.  You can invoke the prompt thus:

  prompt debian
  TERM=dumb prompt debian

To configure the prompt's colors, this command can be used (don't forget to
run 'prompt debian' again to activate the changes):

  zstyle ':prompt:debian:*' STYLE COLOR

You need to replace STYLE with 'dircolor', 'usercolor', 'usercolor-root',
'exitcolor' or 'exitcolor-root'.  Valid values for COLOR include the numbers
from 0 to 7, 'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan' and
'white'.

Displaying version control information via vcs_info and displaying the name of
a Debian chroot is supported by this prompt, but only the latter is enabled by
default.  To enable vcs_info with the user-context 'prompt_debian', run:

  add-zsh-hook precmd prompt_debian_precmd_vcs_info

To disable vcs_info even if the vcs_info command is run, and to enable
it again, zstyle can be used as shown in the two lines below.
The other two lines enable checks for uncommited changes.

  zstyle ':vcs_info:*' enable NONE
  zstyle ':vcs_info:*' enable ALL
  zstyle ':vcs_info:*' check-for-staged-changes true
  zstyle ':vcs_info:*' check-for-changes true

To add additional information to the prompt, e.g., the history event number or
the number of background jobs and the shell level, this commands can be used.
The content of the 'nvcsformats' style is only shown if vcs_info is used, but
no version control system repository was found.

  PROMPT='%B!%!%b '"$PROMPT"
  zstyle ':vcs_info:*' nvcsformats '%B%F{green}%(1j.j%j .)%(2L.L%L .)%b%f'
EOF
}

prompt_debian_precmd_vcs_info () {
    setopt localoptions no_xtrace
    vcs_info prompt_debian || return $?
}

prompt_debian_setup () {
    setopt localoptions no_errexit no_errreturn

    prompt_opts=(cr subst percent)

    local zstyle_pattern=":prompt:debian:${HOST-}:${USERNAME-}:${TERM-}:"

    local dir_max_length='60'
    local dir_max_elements='5'
    local wrap_at_char='-25'

    local p_exit='${${options[printexitvalue]#on}:+%0(?..%? )}'
    local p_dir="%${dir_max_length}<...<%$(( dir_max_elements + 1
                                          ))(~:.../:)%${dir_max_elements}~%<<"
    local p_space_or_newline="%${wrap_at_char}(l: :${prompt_newline})"
    local p_vcsinfo='${vcs_info_msg_0_-}'
    local p_reset='%b%f%k'
    local p_2nd_newline="%1(l:%${wrap_at_char}(l..${prompt_newline}):)"

    if [[ ${TERM-} == dumb ]]; then
        PROMPT="$p_exit\${debian_chroot:+(\$debian_chroot)}%n@%m $p_dir"
        PROMPT+="$p_space_or_newline$p_vcsinfo$p_reset$p_2nd_newline%# "
    else
        # color config:
        local exitcolor exitcolor_root usercolor usercolor_root dircolor
        zstyle -s "$zstyle_pattern" exitcolor      exitcolor ||
                                                   exitcolor='red'
        zstyle -s "$zstyle_pattern" exitcolor-root exitcolor_root ||
                                                   exitcolor_root='green'
        zstyle -s "$zstyle_pattern" usercolor      usercolor ||
                                                   usercolor='green'
        zstyle -s "$zstyle_pattern" usercolor-root usercolor_root ||
                                                   usercolor_root='red'
        zstyle -s "$zstyle_pattern" dircolor       dircolor ||
                                                   dircolor='blue'

        PROMPT="%B%{%0(#:%F{$exitcolor_root}:%F{$exitcolor})%}$p_exit"
        PROMPT+="%{%F{$dircolor}%}\${debian_chroot:+(}"
        PROMPT+="%{%0(#:%F{$usercolor_root}:%F{$usercolor})%}\${debian_chroot-}"
        PROMPT+="%{%F{$dircolor}%}\${debian_chroot:+)}"
        PROMPT+="%{%0(#:%F{$usercolor_root}:%F{$usercolor})%}%n@%m "
        PROMPT+="%{%F{$dircolor}%}$p_dir%b%f"
        PROMPT+="$p_space_or_newline$p_vcsinfo$p_reset$p_2nd_newline%B%#%b "
    fi

    # Only try to set $debian_chroot if it is unset.
    if [[ ! ${debian_chroot+set} && -r /etc/debian_chroot ]]
    then debian_chroot=$(</etc/debian_chroot) || unset debian_chroot
    fi

    # Autoload and configure vcs_info:
    # The format strings 'formats' and 'actionformats' should end with a space
    # to fit into the prompt's design.
    autoload -Uz vcs_info
    if [[ ${TERM-} == dumb ]]; then
        zstyle ':vcs_info:*:prompt_debian:*' formats       '[%s|%b%c%u] '
        zstyle ':vcs_info:*:prompt_debian:*' actionformats '[%s-%a|%b%c%u] '
        zstyle ':vcs_info:(sv[nk]|bzr):prompt_debian:*' branchformat '%b:%r'
        zstyle ':vcs_info:*:prompt_debian:*' stagedstr     '(S)'
        zstyle ':vcs_info:*:prompt_debian:*' unstagedstr   '(U)'
    else
        local   left='%F{red}['
        local middle='%F{red}|'
        local   dash='%F{red}-'
        local  right='%F{red}]'
        local  adapt='%F{green}%c%u'

        zstyle ':vcs_info:*:prompt_debian:*' formats \
               "%B${left}${adapt}%s${middle}${adapt}%b${right}%%b%f "
        zstyle ':vcs_info:*:prompt_debian:*' actionformats \
               "%B${left}${adapt}%s${dash}%a${middle}${adapt}%b${right}%%b%f "
        zstyle ':vcs_info:(sv[nk]|bzr):prompt_debian:*' branchformat \
               '%b%f:%F{blue}%r'
        zstyle ':vcs_info:*:prompt_debian:*' stagedstr   '%F{blue}'
        zstyle ':vcs_info:*:prompt_debian:*' unstagedstr '%F{cyan}'
    fi
}

prompt_debian_setup "$@"
-------------- next part --------------
d25 1
a25 1
  add-zsh-hook precmd prompt_debian_precmd_vcs_info
d42 1
a42 1
  zstyle ':vcs_info:*' nvcsformats '%B%F{green}%(1j.j%j .)%(2L.L%L .)%b%f'
d46 2
a47 2
prompt_debian_precmd_vcs_info () {
    setopt localoptions no_xtrace
a51 2
    setopt localoptions no_errexit no_errreturn

d54 1
a54 1
    local zstyle_pattern=":prompt:debian:${HOST-}:${USERNAME-}:${TERM-}:"
d56 2
a57 2
    local dir_max_length='60'
    local dir_max_elements='5'
d61 2
a62 2
    local p_dir="%${dir_max_length}<...<%$(( dir_max_elements + 1
                                          ))(~:.../:)%${dir_max_elements}~%<<"
d69 1
a69 1
        PROMPT="$p_exit\${debian_chroot:+(\$debian_chroot)}%n@%m $p_dir"
d73 8
a80 11
        local exitcolor exitcolor_root usercolor usercolor_root dircolor
        zstyle -s "$zstyle_pattern" exitcolor      exitcolor ||
                                                   exitcolor='red'
        zstyle -s "$zstyle_pattern" exitcolor-root exitcolor_root ||
                                                   exitcolor_root='green'
        zstyle -s "$zstyle_pattern" usercolor      usercolor ||
                                                   usercolor='green'
        zstyle -s "$zstyle_pattern" usercolor-root usercolor_root ||
                                                   usercolor_root='red'
        zstyle -s "$zstyle_pattern" dircolor       dircolor ||
                                                   dircolor='blue'
d87 1
a87 1
        PROMPT+="%{%F{$dircolor}%}$p_dir%b%f"
d89 8
d101 1
a101 1
    then debian_chroot=$(</etc/debian_chroot) || unset debian_chroot
d107 1
d116 5
a120 5
        local   left='%F{red}['
        local middle='%F{red}|'
        local   dash='%F{red}-'
        local  right='%F{red}]'
        local  adapt='%F{green}%c%u'
d127 3
a129 3
               '%b%f:%F{blue}%r'
        zstyle ':vcs_info:*:prompt_debian:*' stagedstr   '%F{blue}'
        zstyle ':vcs_info:*:prompt_debian:*' unstagedstr '%F{cyan}'


More information about the Pkg-zsh-devel mailing list