1
0
mirror of https://github.com/artizirk/dotfiles.git synced 2024-11-25 01:10:59 +02:00

Read Python virtualenv prompt from pyvenv.cfg

This commit is contained in:
Arti Zirk 2023-07-12 19:02:52 +03:00
parent 01ef149ff2
commit 8e303d41f1

27
.zshrc
View File

@ -3,11 +3,38 @@
# so that i can autoload -Uz local functions
fpath=( ~/.config/zsh/functions $fpath)
# Use a REAL regex engine
zmodload zsh/pcre
# Virtualenv support
function _virtual_env_prompt () {
# new pyvenv has a seperate variable for custom prompt value
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
if [[ -z "${REPLY}" ]]; then
REPLY=${VIRTUAL_ENV+(${VIRTUAL_ENV:t}) }