添加buildstatus

时间:2015-06-10 06:59:23

标签: zsh zshrc buildkite

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

# .buildkite_status
project1: √
project2: x

√和x是ansi颜色编码。

我有一个提示工作正常,直到我将$ ci_build变量/函数添加到RPROMPT!

目前我的提示似乎;

~/.dotfiles »                                         ± master*:3cce1cb

在我想要改变之后

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

我面临的问题是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 $2}')
  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

1 个答案:

答案 0 :(得分:2)

问题是由您添加build_status

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

根据 zsh 手册

  

%{...%}

     

包含一个字符串作为文字转义序列。大括号内的字符串不应更改光标位置

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

由于终端中没有实际的右对齐 zsh 根据其感知长度计算右提示的位置。因为它长了2个字符,所以计算出右边的提示包裹在行尾,将光标放在下一行。

此问题的快速解决方法是使用%G内的%{...%}告诉 zsh 有输出的字符。 %G代表一个字符,对于更多字符,您可以使用适当数量的%G或将匹配的数字放在%G之间:

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

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