Browse Source

Read Python virtualenv prompt from pyvenv.cfg

master
Arti Zirk 1 year ago
parent
commit
8e303d41f1
  1. 27
      .zshrc

27
.zshrc

@ -3,11 +3,38 @@
# so that i can autoload -Uz local functions # so that i can autoload -Uz local functions
fpath=( ~/.config/zsh/functions $fpath) fpath=( ~/.config/zsh/functions $fpath)
# Use a REAL regex engine
zmodload zsh/pcre
# Virtualenv support # Virtualenv support
function _virtual_env_prompt () { function _virtual_env_prompt () {
# new pyvenv has a seperate variable for custom prompt value # new pyvenv has a seperate variable for custom prompt value
REPLY=${VIRTUAL_ENV_PROMPT+${VIRTUAL_ENV_PROMPT}} REPLY=${VIRTUAL_ENV_PROMPT+${VIRTUAL_ENV_PROMPT}}
# Try to read the prompt name form pyvenv.cfg
if [[ -z "${REPLY}" && -f "$VIRTUAL_ENV/pyvenv.cfg" ]]; then
# Matches lines with following syntax
# prompt = 'cool prompt'
# prompt = "cool prompt"
# prompt = cool
# prompt=cool prompt
# heredoc is used here so that I don't have to
# manually escape the regex 🤮
local prompt_re
read -r prompt_re <<-'EOF'
^prompt\s*=\s*['"]?(.+?)['"]?$
EOF
# -m turns on multiline support, this makes ^ and $ anchor work in multiline string
pcre_compile -m ${prompt_re}
# And now its supper easy to get the value
if pcre_match -- "$(<$VIRTUAL_ENV/pyvenv.cfg)"
then
REPLY="(${match[1]}) "
fi
fi
# support old-school virtualenv # support old-school virtualenv
if [[ -z "${REPLY}" ]]; then if [[ -z "${REPLY}" ]]; then
REPLY=${VIRTUAL_ENV+(${VIRTUAL_ENV:t}) } REPLY=${VIRTUAL_ENV+(${VIRTUAL_ENV:t}) }

Loading…
Cancel
Save