Displaying rbenv version in zsh on the right - tooltip is not refreshing

I have the following code in my .zshrc:

local ruby_version=''
if which rvm-prompt &> /dev/null; then
  ruby_version="$(rvm-prompt i v g)"
else
  if which rbenv &> /dev/null; then
    ruby_version="$(rbenv version | sed -e "s/ (set.*$//")"
  fi
fi

      

And I have this code in my RPS1 prompt:

RPS1='${PR_GREEN}$(virtualenv_info)%{$reset_color%} ${PR_RED}${ruby_version}%{$reset_color%}'

      

(For brevity, I have not shown the code that sets the PR_ colors or defines the virtual environment — both work.)

When a new shell is created (new window or new tab in iTerm2) the Ruby information is correct. However, if I switch to a project that uses a different Ruby, as defined in the file .ruby-version

, the Ruby information displayed in the right prompt is not updated. If I regenerate my file the .zshrc

correct prompt is updated.

Do I need to put the code that defines the Ruby version in functions? By the way, I have it setopt promptsubst

in my .zshrc.

What am I missing that is preventing the correct hint from updating when changing directories?

+3


source to share


3 answers


You shouldn't use this code

local ruby_version=''
if which rvm-prompt &> /dev/null; then
  ruby_version="$(rvm-prompt i v g)"
else
  if which rbenv &> /dev/null; then
    ruby_version="$(rbenv version | sed -e "s/ (set.*$//")"
  fi
fi

      

directly in zshrc: it doesn't update the ruby_version variable when switching to another project. You can change it to

function ruby_version()
{
    if which rvm-prompt &> /dev/null; then
      rvm-prompt i v g
    else
      if which rbenv &> /dev/null; then
        rbenv version | sed -e "s/ (set.*$//"
      fi
    fi
}

      



and change ${ruby_version}

in your invitation to $(ruby_version)

. Or if you are sure that you do not need to check this at every prompt (it will slow things down), you can use

function _update_ruby_version()
{
    typeset -g ruby_version=''
    if which rvm-prompt &> /dev/null; then
      ruby_version="$(rvm-prompt i v g)"
      rvm-prompt i v g
    else
      if which rbenv &> /dev/null; then
        ruby_version="$(rbenv version | sed -e "s/ (set.*$//")"
      fi
    fi
}
chpwd_functions+=(_update_ruby_version)

      

it will update ruby_version only when the current directory changes. Also note that your code is misleading: local ruby_version=''

which fits directly into zshrc is equivalent typeset -g ruby_version=''

or just plain ruby_version=''

, which defines a global but not exported variable. There are no local variable files in zsh (other than the startup files, which actually represent functions).

+5


source


use this one:

export ruby_version
if
  which rvm-prompt &> /dev/null
then
  ruby_version='$(rvm-prompt i v g)'
elif
  which rbenv &> /dev/null
then
  ruby_version='$(rbenv version | sed -e "s/ (set.*$//")'
else
  ruby_version=''
fi

RPS1='${PR_GREEN}$(virtualenv_info)%{$reset_color%} ${PR_RED}'"${ruby_version}"'%{$reset_color%}'

      



this will inject variable code ruby_version

intoRPS1

+1


source


It looks like you need to use a hook precmd

that runs every time you execute commands, like switching folders. Unlike how you did it, this is only done once, when you start a new instance.

precmd () {

  local ruby_version=''
  if which rvm-prompt &> /dev/null; then
    ruby_version="$(rvm-prompt i v g)"
  else
    if which rbenv &> /dev/null; then
      ruby_version="$(rbenv version | sed -e "s/ (set.*$//")"
    fi
  fi

  #you can use RPROMPT
  RPS1='${PR_GREEN}$(virtualenv_info)%{$reset_color%} ${PR_RED}${ruby_version}%{$reset_color%}'
}

      

Don't forget to override this function, create it only if it hasn't been created elsewhere. B As it is very likely that it is already defined somewhere in your .zshrc

or other zsh-related files, especially if you are using something like oh-my-zsh or something else that shows information about git (or some other version) in the tooltip or something like that.

Some more light on this matter .

0


source







All Articles