ys.zsh 的 Python 虚拟环境显示
起笔自
所属文集:
杂记
共计
4407
个字符
落笔于
oh-my-zsh 里的主题,我最喜欢是 ys,它简洁优雅,没有非 ASCII 符号。
但唯一的问题是 Python 虚拟环境提示符不能正常显示,在 poetry 创建的虚拟环境下,它总是会把 (.venv)
输出到每个命令的结果之后。
一番搜索之下先是找到了 https://github.com/ohmyzsh/ohmyzsh/issues/5192 里,把它的代码复制过来。但并不能成功,还是和之前一样,不能正常显示虚拟环境。
于是我把目光投到 .venv/bin/activate
里,发现了如下的代码:
if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then _OLD_VIRTUAL_PS1="${PS1:-}" if [ "x(.venv) " != x ] ; then PS1="(.venv) ${PS1:-}" else if [ "`basename \"$VIRTUAL_ENV\"`" = "__" ] ; then # special case for Aspen magic directories # see http://www.zetadev.com/software/aspen/ PS1="[`basename \`dirname \"$VIRTUAL_ENV\"\``] $PS1" else PS1="(`basename \"$VIRTUAL_ENV\"`)$PS1" fi fi export PS1 fi
-z
是一个布尔运算符,如果后面跟的字符串为空则返回 True。所以只需要设置名称为 VIRTUAL_ENV_DISABLE_PROMPT
的环境变量就可以屏蔽这里的输出。
而回过头来看 ys-modified.zsh-theme
里的代码,发现也涉及到了这个变量:
# Virtualenv: current working virtualenv local virtualenv_info='$(ys_py_prompt_virtualenv)' ys_py_prompt_virtualenv() { local virtualenv_path="$VIRTUAL_ENV" if [[ -n $virtualenv_path && -n $VIRTUAL_ENV_DISABLE_PROMPT ]]; then echo -n "%{$fg[white]%}(`basename $virtualenv_path`) " else echo -n "" fi }
-n
也是一个布尔运算符,如果后面跟的字符串不为空则返回 True。故而这里也是需要 VIRTUAL_ENV_DISABLE_PROMPT
不为空才输出的。
解决方案
在 ~/.zshrc
里增加如下代码即可:
# Display python virtualenv VIRTUAL_ENV_DISABLE_PROMPT="true"
ys-fix.zsh-theme
以下是全部代码,以免需要时再去 GitHub 下载。
# Clean, simple, compatible and meaningful. # Tested on Linux, Unix and Windows under ANSI colors. # It is recommended to use with a dark background and the font Inconsolata. # Colors: black, red, green, yellow, *blue, magenta, cyan, and white. # # http://ysmood.org/wp/2013/03/my-ys-terminal-theme/ # Mar 2013 ys # Machine name. function box_name { [ -f ~/.box-name ] && cat ~/.box-name || echo $HOST } # Directory info. local current_dir='${PWD/#$HOME/~}' # VCS YS_VCS_PROMPT_PREFIX1=" %{$fg[white]%}on%{$reset_color%} " YS_VCS_PROMPT_PREFIX2=":%{$fg[cyan]%}" YS_VCS_PROMPT_SUFFIX="%{$reset_color%}" YS_VCS_PROMPT_DIRTY=" %{$fg[red]%}x" YS_VCS_PROMPT_CLEAN=" %{$fg[green]%}o" # Git info. local git_info='$(git_prompt_info)' ZSH_THEME_GIT_PROMPT_PREFIX="${YS_VCS_PROMPT_PREFIX1}git${YS_VCS_PROMPT_PREFIX2}" ZSH_THEME_GIT_PROMPT_SUFFIX="$YS_VCS_PROMPT_SUFFIX" ZSH_THEME_GIT_PROMPT_DIRTY="$YS_VCS_PROMPT_DIRTY" ZSH_THEME_GIT_PROMPT_CLEAN="$YS_VCS_PROMPT_CLEAN" # HG info local hg_info='$(ys_hg_prompt_info)' ys_hg_prompt_info() { # make sure this is a hg dir if [ -d '.hg' ]; then echo -n "${YS_VCS_PROMPT_PREFIX1}hg${YS_VCS_PROMPT_PREFIX2}" echo -n $(hg branch 2>/dev/null) if [ -n "$(hg status 2>/dev/null)" ]; then echo -n "$YS_VCS_PROMPT_DIRTY" else echo -n "$YS_VCS_PROMPT_CLEAN" fi echo -n "$YS_VCS_PROMPT_SUFFIX" fi } # Virtualenv: current working virtualenv local virtualenv_info='$(ys_py_prompt_virtualenv)' ys_py_prompt_virtualenv() { local virtualenv_path="$VIRTUAL_ENV" if [[ -n $virtualenv_path && -n $VIRTUAL_ENV_DISABLE_PROMPT ]]; then echo -n "%{$fg[white]%}(`basename $virtualenv_path`) " else echo -n "" fi } # Prompt format: \n # (virtualenv) USER at MACHINE in DIRECTORY on git:BRANCH STATE [TIME] \n $ PROMPT=" %{$terminfo[bold]$fg[blue]%}#%{$reset_color%} \ ${virtualenv_info}\ %{$fg[cyan]%}%n \ %{$fg[white]%}at \ %{$fg[green]%}$(box_name) \ %{$fg[white]%}in \ %{$terminfo[bold]$fg[yellow]%}${current_dir}%{$reset_color%}\ ${hg_info}\ ${git_info} \ %{$fg[white]%}[%*] %{$terminfo[bold]$fg[red]%}$ %{$reset_color%}" if [[ "$USER" == "root" ]]; then PROMPT=" %{$terminfo[bold]$fg[blue]%}#%{$reset_color%} \ ${virtualenv_info}\ %{$bg[yellow]%}%{$fg[cyan]%}%n%{$reset_color%} \ %{$fg[white]%}at \ %{$fg[green]%}$(box_name) \ %{$fg[white]%}in \ %{$terminfo[bold]$fg[yellow]%}${current_dir}%{$reset_color%}\ ${hg_info}\ ${git_info} \ %{$fg[white]%}[%*] %{$terminfo[bold]$fg[red]%}$ %{$reset_color%}" fi