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

Carsten Hey carsten at debian.org
Sat Aug 8 14:30:31 UTC 2015


A new prompt_debian_setup that no longer tries to mimic adam1 is
attached.

I think "#setopt no_beep" and "#stty -ixon" should also be added to
newuser.zshrc.recommended, but maybe we should concentrate on the
prompt for now.
-------------- 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_vcsinfo

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{2}%(1j.j%j .)%(2L.L%L .)%b%f'
EOF
}

prompt_debian_precmd_vcsinfo () {
    setopt localoptions noxtrace
    vcs_info prompt_debian || return $?
}

prompt_debian_setup () {
    prompt_opts=(cr subst percent)

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

    local pwd_max_length=60
    local pwd_max_subdirs=5
    local wrap_at_char='-25'

    local p_exit='${${options[printexitvalue]#on}:+%0(?..%? )}'
    local p_pwd="%${pwd_max_length}<...<%$(( pwd_max_subdirs + 1
                                          ))(~:.../:)%${pwd_max_subdirs}~%<<"
    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_pwd"
        PROMPT+="$p_space_or_newline$p_vcsinfo$p_reset$p_2nd_newline%# "
    else
        # color config:
        local dircolor= usercolor= usercolor_root= exitcolor= exitcolor_root=
        zstyle -s "$zstyle_pattern" dircolor       dircolor
        zstyle -s "$zstyle_pattern" usercolor      usercolor
        zstyle -s "$zstyle_pattern" usercolor-root usercolor_root
        zstyle -s "$zstyle_pattern" exitcolor      exitcolor
        zstyle -s "$zstyle_pattern" exitcolor-root exitcolor_root
        : "${dircolor:=4}" "${usercolor:=2}" "${usercolor_root:=1}"
        : "${exitcolor:=1}" "${exitcolor_root:=2}"

        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_pwd%b%f"
        PROMPT+="$p_space_or_newline$p_vcsinfo$p_reset$p_2nd_newline%B%#%b "

        ##TODO uncomment if setopt transientrprompt works in a prompt function
        ## Ease copying multi-line commands with the mouse.
        ## <http://www.zsh.org/mla/workers/2015/msg01767.html>
        #local rps2color=$dircolor
        #setopt transient_rprompt
        #PS2='${${options[singlelinezle]#off}:+%_> }'
        #RPS2=${RPS2:-"%B<%{%F{$rps2color}%}%^%f%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 || :)
    fi

    # Autoload and configure vcs_info:
    # The format strings 'formats' and 'actionformats' should end with a space
    # to fit into the prompt's design.
    # Colors: 1:=red, 2:=green, 3:=yellow, 4:=blue, 5:=magenta, 6:=cyan, ...
    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{1}['
        local middle='%F{1}|'
        local   dash='%F{1}-'
        local  right='%F{1}]'
        local  adapt='%F{2}%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{4}%r'
        zstyle ':vcs_info:*:prompt_debian:*' stagedstr   '%F{4}'
        zstyle ':vcs_info:*:prompt_debian:*' unstagedstr '%F{6}'
    fi
}

prompt_debian_setup "$@"
-------------- 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_vcsinfo

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{2}%(1j.j%j .)%(2L.L%L .)%b%f'
EOF
}

prompt_debian_precmd_vcsinfo () {
    setopt localoptions noxtrace
    vcs_info prompt_debian || return $?
}

prompt_debian_setup () {
    prompt_opts=(cr subst percent)

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

#   local enable_exit_status=1
    local pwd_max_length=60
    local pwd_max_subdirs=5
    local wrap_at_char='-25'

    local p_exit='${${options[printexitvalue]#on}:+%0(?..%? )}'
    local p_pwd="%${pwd_max_length}<...<%$(( pwd_max_subdirs + 1
                                          ))(~:.../:)%${pwd_max_subdirs}~%<<"
    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}):)"

#   # Disable features disabled via zstyle:
#   if (( ! pwd_max_subdirs || ! pwd_max_length )); then
#       if (( pwd_max_subdirs ))
#       then p_pwd="%$(( pwd_max_subdirs + 1 ))(~:.../:)%${pwd_max_subdirs}~"
#       elif (( pwd_max_length ))
#       then p_pwd="%${pwd_max_length}<...<%~%<<"
#       else p_pwd='%~'
#       fi
#   fi
#   (( ! wrap_at_char )) && p_space_or_newline=' ' && p_2nd_newline=
#   (( enable_exit_status )) || p_exit=

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

        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_pwd%b%f"
        PROMPT+="$p_space_or_newline$p_vcsinfo$p_reset$p_2nd_newline%B%#%b "

        ##TODO uncomment if setopt transientrprompt works in a prompt function
        ## Ease copying multi-line commands with the mouse.
        ## <http://www.zsh.org/mla/workers/2015/msg01767.html>
        #local rps2color=$dircolor
        #setopt transient_rprompt
        #PS2='${${options[singlelinezle]#off}:+%_> }'
        #RPS2=${RPS2:-"%B<%{%F{$rps2color}%}%^%f%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 || :)
    fi

    # Autoload and configure vcs_info:
    # The format strings 'formats' and 'actionformats' should end with a space
    # to fit into the prompt's design.
    # Colors: 1:=red, 2:=green, 3:=yellow, 4:=blue, 5:=magenta, 6:=cyan, ...
    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{1}['
        local middle='%F{1}|'
        local   dash='%F{1}-'
        local  right='%F{1}]'
        local  adapt='%F{2}%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{4}%r'
        zstyle ':vcs_info:*:prompt_debian:*' stagedstr   '%F{4}'
        zstyle ':vcs_info:*:prompt_debian:*' unstagedstr '%F{6}'
    fi
}

prompt_debian_setup "$@"


More information about the Pkg-zsh-devel mailing list