我使用官方的Git Bash提示支持将当前分支显示为提示符的一部分。
我的问题是,使用python -m venv 激活( source bin/activate )并不会将虚拟环境名称(atlassian-watchdog)显示为Bash提示符的一部分:
nlykkei:~/projects/atlassian-watchdog (master *)$
我强烈地感觉到它失败了,因为我在~/.bashrc中使用D5而不是PS1,但是GIT_PS1_SHOWCOLORHINTS只使用PROMPT_COMMAND。
是否有任何方法将PROMPT_COMMAND与一起使用,以便在它处于活动状态时将环境名称添加到提示符中?
~/..git提示.:
# If you would like a colored hint about the current dirty state, set
# GIT_PS1_SHOWCOLORHINTS to a nonempty value. The colors are based on
# the colored output of "git status -sb" and are available only when
# using __git_ps1 for PROMPT_COMMAND or precmd.bin/激活:
if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then
_OLD_VIRTUAL_PS1="${PS1:-}"
if [ "x(atlassian-watchdog) " != x ] ; then
PS1="(atlassian-watchdog) ${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~/..bashrc:
# git prompt
source ~/.git-prompt.sh
GIT_PS1_SHOWCOLORHINTS=1
PROMPT_COMMAND='__git_ps1 "\u:\w" "\\\$ "'发布于 2020-09-24 06:46:55
解决方案是将bin/activate的相关部分与~/git-prompt.sh相结合。
这里,__git_ps1_venv()是一个封装__git_ps1()并修改__git_ps1()的第一个参数以包含虚拟环境的函数。
PS1变量应该设置为提示符的格式。
~/.bashrc:
##################
# Prompt
##################
__git_ps1_venv() {
local pre="$1"
local post="$2"
if [ -n "${VIRTUAL_ENV}" ] && [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ]; then
if [ "`basename \"$VIRTUAL_ENV\"`" = "__" ] ; then
# special case for Aspen magic directories
# see http://www.zetadev.com/software/aspen/
pre="[`basename \`dirname \"$VIRTUAL_ENV\"\``] ${pre}"
else
pre="(`basename \"$VIRTUAL_ENV\"`) ${pre}"
fi
fi
__git_ps1 "${pre}" "${post}"
}
PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\W\[\033[00m\]\$ '
if [[ -r ~/.git-prompt.sh ]]; then
. ~/.git-prompt.sh
GIT_PS1_SHOWCOLORHINTS=1
GIT_PS1_SHOWDIRTYSTATE=1
GIT_PS1_SHOWSTASHSTATE=1
GIT_PS1_SHOWUNTRACKEDFILES=1
GIT_PS1_SHOWUPSTREAM="verbose name"
PROMPT_COMMAND='__git_ps1_venv "'"${PS1%\\\$ }"'" "\\\$ "'
fihttps://unix.stackexchange.com/questions/570419
复制相似问题