提示打印控制字符而不是颜色

问题描述

我正在研究此混搭提示脚本

autoload colors && colors
autoload -Uz add-zsh-hook

if (( $+commands[git] ))
then
  git="$commands[git]"
else
  git="/usr/bin/git"
fi

git_branch() {
  echo $($git symbolic-ref HEAD 2>/dev/null | awk -F/ {'print $NF'})
}

git_dirty() {
  if $($git status -s &> /dev/null)
  then
    if [[ $($git status --porcelain) == "" ]]
    then
      psvar[1]="on %{$fg_bold[green]%}$(git_prompt_info)%{$reset_color%}"
    else
      psvar[1]="on %{$fg_bold[red]%}$(git_prompt_info)%{$reset_color%}"
    fi
  fi
}

git_prompt_info () {
 ref=$($git symbolic-ref HEAD 2>/dev/null) || return
# echo "(%{\e[0;33m%}${ref#refs/heads/}%{\e[0m%})"
 echo "${ref#refs/heads/}"
}

# This assumes that you always have an origin named `origin`,and that you only
# care about one specific origin. If this is not the case,you might want to use
# `$git cherry -v @{upstream}` instead.
need_push () {
  if [ $($git rev-parse --is-inside-work-tree 2>/dev/null) ]
  then
    number=$($git cherry -v origin/$(git symbolic-ref --short HEAD) 2>/dev/null | wc -l | bc)

    if [[ $number != 0 ]]
    then
      psvar[2]=" with %{$fg_bold[magenta]%}$number unpushed%{$reset_color%}"
    fi
  fi
}

NEWLINE=$'\n'

base_prompt="%{$fg[cyan]%}%n%{$reset_color%}%{$fg[cyan]%}@%{$reset_color%}%{$fg[cyan]%}%m%{$reset_color%}%{$fg[red]%}:%{$reset_color%}%{$fg[cyan]%}%0~%{$reset_color%}%{$fg[red]%}|%{$reset_color%}"
post_prompt="%{$fg[cyan]%}=>%{$reset_color%}  "

export PROMPT="${base_prompt}%1v %2v${NEWLINE} ${post_prompt}"

add-zsh-hook precmd git_dirty
add-zsh-hook precmd need_push

一切看上去都不错,但是当它运行时会创建一个

root@myBox:~/code/dot-files|on %{^[[01;31m%}master%{^[[00m%}

psvar [1]的上下文正在打印控件字符而不是对其进行格式化。我确定某处有些愚蠢的报价,我在做什么错了?

谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)