ZSH主题无法从函数中更新

3

我正在尝试编写一个ZSH主题,根据Git状态(如果在git存储库中)显示特定字符。我以前在Bash中实现了这个主题,但移植到ZSH时遇到了一些问题。这可能是我的问题,但如果有人能帮助就太好了。

我的主题的相关部分:

local jobs="%{$terminfo[bold]$fg[cyan]%}[%{$fg[magenta]%}%j%{$terminfo[bold]$fg[cyan]%}]%{$reset_color%}"
local git_branch='$(git_prompt_info)%{$reset_color%}'
local current_dir="%{$terminfo[bold]$fg[orange]%}%~%{$reset_color%}"

if [[ $UID -eq 0 ]]; then
    local user_host="%{$terminfo[bold]$fg[red]%}%n@%m%{$reset_color%}"
    local user_symbol="#"
else
    local user_host="%{$terminfo[bold]$fg[green]%}%n@%{$fg[red]%}%m%{$reset_color%}"
    local user_symbol="$"
fi

function git_has_unstaged() {
  if ! $(git diff-files --quiet --ignore-submodules --); then
      echo -n "%*! ";
  fi;
}

function git_has_uncommitted() {
  if [ ! $(git diff --quiet --ignore-submodules --cached; echo "${?}") = "0" ]; then
      echo -n "%*+ ";
  fi;
}

function git_has_untracked() {
  if [ -n "$(git ls-files --others --exclude-standard)" ]; then
      echo -n "%*? ";
  fi;
}

function git_has_stashed() {
  if $(git rev-parse --verify refs/stash &>/dev/null); then
      echo -n "%*$ ";
  fi;
}

function git_status_symbols() {
  echo -n "%{$terminfo[bold]$fg[blue]%}[";
  if [ $(git rev-parse --is-inside-work-tree &>/dev/null; echo "${?}") = "0" ]; then
    #Now is it Git repo
    if [ "$(git rev-parse --is-inside-git-dir 2> /dev/null)" = "false" ]; then
      # check if the current directory is in .git before running git checks
      # Ensure the index is up to date.
      git update-index --really-refresh -q &>/dev/null;
      git_has_uncommitted;
      git_has_unstaged;
      git_has_untracked;
      git_has_stashed;
    fi;
  fi;
  echo -n "]%{$reset_color%}";
}

PROMPT="╭─${user_host} ${current_dir} ${jobs} ${git_branch} $(git_status_symbols)
╰─%B${user_symbol}%b "
ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg[yellow]%}‹"
ZSH_THEME_GIT_PROMPT_SUFFIX="›%{$reset_color%}"

我的问题是,提示符中的git_status_symbols值不会根据文件系统和git更新而更新。例如:
╭─theonlyjohnny@phoenix ~/configs [0] ‹bored› []
╰─$ git status
On branch bored
Your branch is up to date with 'origin/bored'.

nothing to commit, working tree clean
╭─theonlyjohnny@phoenix ~/configs [0] ‹bored› []
╰─$ touch ./t
╭─theonlyjohnny@phoenix ~/configs [0] ‹bored› []
╰─$ git_has_untracked
%*? %                                                                                                                                                                                                                                                                                                               ╭─theonlyjohnny@phoenix ~/configs [0] ‹bored› []
╰─$ zsh
╭─theonlyjohnny@phoenix ~/configs [0] ‹bored› [1:50:37? ]
╰─$
╭─theonlyjohnny@phoenix ~/configs [0] ‹bored› [1:52:14? ]
╰─$ rm t
╭─theonlyjohnny@phoenix ~/configs [0] ‹bored› [1:52:16? ]
╰─$ git_has_untracked
╭─theonlyjohnny@phoenix ~/configs [0] ‹bored› [1:52:22? ]
╰─$ zsh
╭─theonlyjohnny@phoenix ~/configs [0] ‹bored› []
╰─$

正如您所见,虽然我的git_has_untracked函数已经正确地回显了一个问号,但提示符在我启动新的shell之前没有反映出来。删除未跟踪的文件后,git_has_untracked函数不再有任何回显。有趣的是,时间仍在更新。创建一个新的shell后,提示符再次正确。
感谢任何帮助!谢谢
1个回答

4
这是因为您的 PROMPT 变量包含命令替换 $(git_status_symbols)。这个计算只在赋值时发生一次,而不会在每次扩展 PROMPT 时发生。
最简单的解决方法是使用 precmd 填充 psvar 数组,然后使用形式为 %v 的方式将该值插入到提示符中。例如:
precmd () { psvar[1]=$(git_status_symbols); }
PROMPT="╭─${user_host} ${current_dir} ${jobs} ${git_branch} %1v
╰─%B${user_symbol}%b "

此外,请注意zsh具有自己的vcs支持(针对Git和其他工具),你可以使用它来代替编写自定义代码。请注意不一定需要这样做,具体情况可视个人而定。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接