添加 buildstatus 后 zsh 提示换行

zsh prompt wrapping to a newline after adding buildstatus

我正在尝试将我的 Buildkite(ci 构建服务器)项目状态添加到 zsh 提示符!我写了一个 ruby 脚本来提取状态并将其放入冒号分隔的文件中,格式如下:

# .buildkite_status
project1: √
project2: x

√ 和 x 是 ansi 颜色编码。

在我将 $ci_build variable/function 添加到 RPROMPT 之前,我的提示一直有效!

目前我的提示是这样的;

~/.dotfiles »                                         ± master*:3cce1cb

而我想要的改变后

~/.dotfiles »                                         ± master*:3cce1cb √

我遇到的问题cing 是 ci_build 的引入现在正在包装我的提示。经过一周的阅读文档和调整后,我没有任何建议。我真的很希望它能正常工作,但更希望它能正常工作。

这是问题的图片: https://www.dropbox.com/s/ufj82ipd7bm0o30/Screenshot%202015-06-11%2016.52.11.png?dl=0

zsh.rc

build_status() {
  current_directory=$(basename $PWD)
  var=$(cat ~/.buildkite_status | grep \^$current_directory: | awk -F':' '{print }')
  echo -n $var | tr '\n' ' '
}

local git_formats="%{${fg_bold[yellow]}%}± %b%c%u:%.7i%{${reset_color}%}"
zstyle ':vcs_info:git*' enable git
zstyle ':vcs_info:git*' check-for-changes true
zstyle ':vcs_info:git*' get-revision true
zstyle ':vcs_info:git*' stagedstr "+"
zstyle ':vcs_info:git*' unstagedstr "*"
zstyle ':vcs_info:git*' formats "$git_formats"
zstyle ':vcs_info:git*' actionformats "%a $git_formats"

precmd() {
  vcs_info
  build_status
}

zle-keymap-select() { zle reset-prompt; }
zle -N zle-keymap-select

VI_MODE_INDICATOR="%{$fg_bold[red]%}<%{$fg[red]%}<<%{$reset_color%}"
vi_mode_prompt_info() {
  echo "${${KEYMAP/vicmd/$VI_MODE_INDICATOR}/(main|viins)/}"
}

local cwd='%{${fg_bold[green]}%}$(prompt_pwd)%{${reset_color}%}'
local usr='%{${fg[yellow]}%}$(user_hostname)%{${reset_color}%} '
local char='%(?,%F{cyan}»,%F{red}»)%f '
local git='${vcs_info_msg_0_}$(git_stash) '
local git_author='$(git author > /dev/null || echo "$(git author) ")'
local vi_mode='$(which vi_mode_prompt_info &> /dev/null && vi_mode_prompt_info) '
local bg_job='%{${fg_bold[black]}%}$(prompt_bg_job)%{${reset_color}%} '
local ci_build='%{$(build_status)%} '

PROMPT=$cwd$usr$char
RPROMPT=$vi_mode$bg_job$git_author$git$ci_build

问题是由您包含 build_status:

输出的方式引起的
local ci_build='%{$(build_status)%} '

根据 zsh 手册

%{...%}

Include a string as a literal escape sequence. The string within the braces should not change the cursor position

zsh 假定 $ci_build 仅包含转义序列并打印出 0 个字符的长度,同时它还包含显示状态的字符和 space,因此实际上长了 2 个字符。

由于终端中没有实际的右对齐 zsh 根据其感知长度计算右提示的位置。因为它比计算的要长 2 个字符,所以正确的提示会换行到行尾,将光标放在下一行。

这个问题的快速修复是在%{...%}里面使用%G告诉zsh有字符要输出。 %G代表一个字符,更多字符可以使用适量的%G或者在%G之间输入匹配数字:

local ci_build='%{$(build_status)%2G} '

更清晰的修复方法是将 ANSI 代码(可能还有您正在使用的特殊字符)保留在状态文件之外,并为此使用 zsh 功能。