jdx / mise

dev tools, env vars, task runner
https://mise.jdx.dev
MIT License
10.4k stars 297 forks source link

Bash prompt modification for Python virtualenvs? #623

Open rsyring opened 1 year ago

rsyring commented 1 year ago

I currently have the following set in .bashrc so that my prompt gets updated when I have a virtualenv activated:

#### for vex
function virtualenv_prompt() {
    if [ -n "$VIRTUAL_ENV" ]; then
        echo "(${VIRTUAL_ENV##*/})"
    fi
}
function docker_prompt() {
    if [ -n "$DOCKER_HOST" ]; then
        _host=${DOCKER_HOST/.level12.net}
        _host=${_host/'ssh://'}
        echo -e "(\U1F433 $_host)"
    fi
}
eval "$(vex --shell-config bash)"

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="$(docker_prompt)$(virtualenv_prompt)\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac

But when using rtx's virtualenv activation the prompt is not modified when virtualenvs are active. I'm assuming this is an order of operations issue? Is this something that can be fixed by rtx? If not, is there a better way to modify the prompt so that it works with the way RTX activates the virtualenv?

jdx commented 1 year ago

I'm not really sure why this doesn't work since it sets the VIRTUAL_ENV env var, I would think PS1 would be able to read it

fabge commented 10 months ago

the VIRTUAL_ENV var is set after .zshrc is initialized. using the python autoactivate virtual environment feature is currently not compatible with showing the virtual environment in the prompt in a straightforward way. e.g. the a vanilla ohmyzsh installation (with virtual environment information) will not work.

jdx commented 2 days ago

ah in that case it should be fixed by calling _mise_hook_cmd or whatever that is before rendering PS1