lyze / posh-git-sh

Bash/ZSH version of the posh-git command prompt
GNU General Public License v3.0
381 stars 102 forks source link

break Python virtualenv display #57

Open HoLengZai opened 3 years ago

HoLengZai commented 3 years ago

Hi,

When you activate a Python virtualenv, posh-git-sh got the priority and I don't see the name of my virtual env on the prompt. It is a bit annoying because I don't know if I am in my virtualenv or not.

I don't have this issue using the original powershell post-git version

toto@computer_name:~ $ mkdir -p ~/toto_test; cd ~/toto_test
toto@computer_name:~/toto_test $ git init
toto@computer_name:~/toto_test $ python -m venv .venv
toto@computer_name:~/toto_test $ source ./.venv/bin/activate

I should get something like that:

(.venv) toto@computer_name: ~/toto_test [main =]$

I got this instead

toto@computer_name: ~/toto_test [main =]$
n-peugnet commented 3 years ago

My workaround for this issue is to use __posh_git_echo function instead of __posh_git_ps1 in $PROMPT_COMMAND and manually removing the previous git information using sed, based on a delimiter character I added to my $PS1 previously (here >):

function prompt_command {
    PS1="$(echo $PS1 | sed 's/>.*/>/') $(__posh_git_echo)\n$ "
}
PROMPT_COMMAND=prompt_command;$PROMPT_COMMAND

My PS1 is defined like this (notice the > at the end):

PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w> '

Another advantage of this technique is that it keeps your previous PS1 customizations (if any) but keep in mind that it will break if the delimiter character appears before in the PS1, for instance in the path. It should not appear too often if you carefully choose your delimiter.

lyze commented 3 years ago

There are also some other neat suggestions in this SO question that may help. Virtualenv modifies the shell prompt, so some elbow grease is required to make sure only at most one tool modifies PROMPT_COMMAND.

UlfTheFirst commented 6 months ago

Been a while, but this tool is still very nice. I have come up with a workaround for this problem. Just use an additional function in the RC to 'enhance' the prompt. Use the following:

getBaseDir () {
    base_dir=""
    if [[ -n "$VIRTUAL_ENV" ]]; then
      base_dir+="("
      base_dir+=$(basename $VIRTUAL_ENV)
      base_dir+=")"
    fi
    echo $base_dir
}

PROMPT_COMMAND='__posh_git_ps1 "$(getBaseDir)\[\033[01;32m\] ➞ \[\033[00m\]" "" "" " ";'$PROMPT_COMMAND

My prompt only consists of an arrow. In order to additionally display the ENV call getBaseDir in the first set of quotes and everything should be fine.