在zsh提示符中扩展

我正在努力使用zsh提示符,尝试将我的bash提示符转换为zsh hooks.

此代码应显示当前的kerberos票证并相应更新,但颜色不起作用,更新也不起作用:

# initialize advanced prompt
autoload -U promptinit && promptinit
# allow functions in prompt
setopt prompt_subst

#initialize colors
autoload -U colors && colors

# add the add-zsh-hook command
autoload add-zsh-hook

# Configure History
setopt hist_ignore_all_dups
setopt inc_append_history
export HISTIGNORE="history"

source $HOME/.zsh/prompt_krb.zsh

update_krb_env() {
if [ "`which klist 2>/dev/null`" != "" ]; then

    # got kerberos support installed
    ticket=`klist 2>/dev/null | grep "Principal:" | sed 's/.*Principal:\ //'`;

    klist -s
    if [ $? -eq 0 ]; then
        __KRB_PROMPT="%{$fg_no_bold['green']%}krb id \"$ticket\"";
    else
        if [ "$ticket" != "" ]; then
            __KRB_PROMPT="%{$fg_no_bold['orange']%}expired krb id \"$ticket\"";
        else
            __KRB_PROMPT="%{$fg_no_bold['yellow']%}no krb id";
        fi
    fi
else
    __KRB_PROMPT="%{$fg_no_bold['yellow']%}no krb id";
fi

_KRB_PROMPT="${__KRB_PROMPT}%{$reset_color%}";
}

krb_hook_preexec() {
    case "$2" in
        kinit*|kdestroy*)
            __EXECUTED_KRB_CMD=1;
        ;;
    esac
}

krb_hook_postexec() {
    if [ -n "$__EXECUTED_KRB_CMD" ]; then
        update_krb_env;
        unset __EXECUTED_KRB_CMD;
    fi
}

add-zsh-hook preexec krb_hook_preexec
add-zsh-hook precmd krb_hook_postexec
update_krb_env;

get_krb() {
    echo -n $__KRB_PROMPT;
}

PROMPT="$(get_krb) >"

我一直在研究多个教程,尝试构建代码,但我似乎错过了一些东西.

最佳答案
命令替换和变量在双引号内扩展,所以在

PROMPT="$(get_krb) >"

$(get_krb)在该赋值时展开,而您希望通过prompt_subst在每个提示符处展开它.所以:

PROMPT='$(get_krb) >'

转载注明原文:在zsh提示符中扩展 - 代码日志