添加buildstatus后zsh提示换行 [英] zsh prompt wrapping to a newline after adding buildstatus

查看:42
本文介绍了添加buildstatus后zsh提示换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm attempting to add my Buildkite (ci build server) project status to the zsh prompt! I've written a ruby script that pulls the status and puts it into a colon separated file in the following format:

# .buildkite_status
project1: √
project2: x

√ 和 x 是 ansi 颜色编码.

The √ and x are ansi colour coded.

在我将 $ci_build 变量/函数添加到 RPROMPT 之前,我有一个可以正常工作的提示!

And I have a prompt that works fine until I add my $ci_build variable/function to the RPROMPT!

目前我的提示看起来像;

At the moment my prompt looks like;

~/.dotfiles »                                         ± master*:3cce1cb

在我想要的改变之后

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

我面临的问题是 ci_build 的引入现在包装了我的提示.经过一周的阅读文档和调整后,我没有任何建议.我真的很喜欢这个工作,但更喜欢它正常工作.

The problem I'm facing is the introduction of the ci_build is now wrapping my prompt. after a week of reading docs and tweaking, I'm out of suggestions. I really would love this to work, but would prefer it working correctly.

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

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

推荐答案

该问题是由您包含 build_status 输出的方式引起的:

The issue is caused by the way you include the output of 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 个字符,同时它还包含显示状态的字符和一个空格,因此实际上长了 2 个字符.

zsh assumes that $ci_build contains only escape sequences and prints out to a length of 0 characters, while it also contains the character showing the status and a space, thus being actually 2 characters longer.

由于终端中没有实际的右对齐,zsh 根据感知长度计算右提示的位置.由于它长了 2 个字符,因此计算出正确的提示换行到行尾,将光标放在下一行.

As there is no actual right-alignment in the terminal zsh calculates the position of the right prompt from its perceived length. As it is 2 characters longer then calculated the right prompt wraps over the end of line, placing the cursor on the next line.

这个问题的快速解决方法是在 %{...%} 中使用 %G 告诉 zsh 有字符那将是输出.%G 代表一个字符,对于更多的字符,您可以使用适当数量的%G 或在% 和<之间添加匹配的数字代码>G:

The quick fix for this problem is to use %G inside %{...%} to tell zsh that there are characters that will be output. %G stands for one character, for more characters you can either use the appropriate amount of %G or put the matching number between % and G:

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

<小时>

更干净的解决方法是将 ANSI 代码(可能还有您正在使用的特殊字符)从状态文件中移除,而只需使用 zsh 功能即可.

这篇关于添加buildstatus后zsh提示换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆