ZSH RPROMPT weird spacing?

天大地大妈咪最大 提交于 2019-12-20 10:58:32

问题


Here is my ZSH prompt theme

function git_prompt_info() {
  ref=$(git symbolic-ref HEAD 2> /dev/null) || return
  echo "$(parse_git_dirty)$ZSH_THEME_GIT_PROMPT_PREFIX$(current_branch)$ZSH_THEME_GIT_PROMPT_SUFFIX"
}

PROMPT='$fg[yellow]%}⚡︎ $fg[cyan]%~ $(git_prompt_info)
%{$reset_color%}→ '

ZSH_THEME_GIT_PROMPT_PREFIX="[git:"
ZSH_THEME_GIT_PROMPT_SUFFIX="]$reset_color"
ZSH_THEME_GIT_PROMPT_DIRTY="$fg[red]+"
ZSH_THEME_GIT_PROMPT_CLEAN="$fg[green]"

RPROMPT='%T'

Which looks like

When I move the $(git_prompt_info) to RPROMPT

function git_prompt_info() {
  ref=$(git symbolic-ref HEAD 2> /dev/null) || return
  echo "$(parse_git_dirty)$ZSH_THEME_GIT_PROMPT_PREFIX$(current_branch)$ZSH_THEME_GIT_PROMPT_SUFFIX"
}

PROMPT='%T $fg[yellow]%}⚡︎ $fg[cyan]%~
%{$reset_color%}→ '

ZSH_THEME_GIT_PROMPT_PREFIX="[git:"
ZSH_THEME_GIT_PROMPT_SUFFIX="]$reset_color"
ZSH_THEME_GIT_PROMPT_DIRTY="$fg[red]+"
ZSH_THEME_GIT_PROMPT_CLEAN="$fg[green]"

RPROMPT='$(git_prompt_info)'

it looks like

See the spacing on the right? Also the arrow starts in the wrong place?

How can I fix this?


回答1:


I believe $fg[color] contains something like \e[32m? If so, it must be enclosed in %{…%} to indicate that this sequence has no width. But much better if you forget about the whole thing and use %F{color} for foreground, %K{color} for background and %f/%k to cancel them in place of $reset_color. You must do

setopt promptsubst
setopt promptpercent

in order for this to work (you likely already do have this).

That gap is the width of colors, and they are the reason why you have wrong cursor position. Problem here is that zsh can’t query terminal with the question “Hey, I outputted some text, what is its width?” instead having to calculate width on its own.



来源:https://stackoverflow.com/questions/14049870/zsh-rprompt-weird-spacing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!