如何在ZSH提示中输出当前历史事件编号(%!或%h)-1?

时间:2012-08-30 10:58:42

标签: type-conversion command-prompt interpolation zsh prompt

在我的ZSH提示中,我希望能够输出当前的历史事件编号(%!%h)-1

如果当前历史事件编号为!256,我想将其减去一,并在我的提示中输出结果(即!255)。

以下是它现在的样子以及我希望它如何:

Screenshot from my current ZSH prompt

以下是我当前的ZSH主题(与此问题相关的代码位于 previous_history_event_number()函数中,该函数由 return_code_enabled = 声明触发:

# ------------------------------------------------------------------------------
#          FILE:  hced.zsh-theme
#   DESCRIPTION:  oh-my-zsh theme file.
#                 (Credits to authors of blinks.zsh-theme and dieter.zsh-theme
#                  from which themes I've taken useful bits.)
#        AUTHOR:  hced
#       VERSION:  0.0.1
#    SCREENSHOT:
# ------------------------------------------------------------------------------

function _prompt_char() {
  if $(git rev-parse --is-inside-work-tree >/dev/null 2>&1); then
    echo "%{%F{blue}%}±%{%f%k%b%}"
  else
    echo ' '
  fi
}

ZSH_THEME_GIT_PROMPT_PREFIX=" [%{%B%F{blue}%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{%f%k%b%K{black}%B%F{green}%}]"
ZSH_THEME_GIT_PROMPT_DIRTY=" %{%F{red}%}*%{%f%k%b%}"
ZSH_THEME_GIT_PROMPT_CLEAN=""

PROMPT='%{%f%k%b%}
%{%K{black}%B%F{green}%}%n%{%B%F{blue}%}@%{%B%F{cyan}%}%m%{%B%F{green}%} %{%b%F{yellow}%K{black}%}%~%{%B%F{green}%}$(git_prompt_info)%E%{%f%k%b%}
%{%K{black}%}$(_prompt_char)%{%K{black}%} %#%{%f%k%b%} '

#RPROMPT='!%{%B%F{cyan}%}%!%{%f%k%b%}'

function previous_history_event_number () {
    prev_hist_num=("%!"-1)
    declare -i prev_hist_num
    echo "$prev_hist_num gave exit code: "
}

# elaborate exitcode on the right when >0
return_code_enabled="%(?..%{$fg[red]%}$(previous_history_event_number)%?%{$reset_color%})"
return_code_disabled=
return_code=$return_code_enabled

RPS1='${return_code}  !%{%B%F{cyan}%}%!%{%f%k%b%}'

function accept-line-or-clear-warning () {
    if [[ -z $BUFFER ]]; then
        time=$time_disabled
        return_code=$return_code_disabled
    else
        time=$time_enabled
        return_code=$return_code_enabled
    fi
    zle accept-line
}
zle -N accept-line-or-clear-warning
bindkey '^M' accept-line-or-clear-warning

免责声明:我不是程序员,所以previous_history_event_number ()函数中的代码现在非常(非常)无能为力。

1 个答案:

答案 0 :(得分:3)

如果在提示中添加%! atom时显示当前历史记录事件编号,则可以使用(%)修饰符将其转换为变量:

V='%!'
integer HISTORY_EVENT_NUMBER=${(%)V}-1