Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZSH RPROMPT weird spacing?

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

this

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

enter image description here

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

How can I fix this?

like image 692
ahmedelgabri Avatar asked Dec 27 '12 05:12

ahmedelgabri


1 Answers

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.

like image 92
ZyX Avatar answered Oct 26 '22 14:10

ZyX