Score:0

Can't see python virtual environment name in bash prompt when PROMPT_COMMAND is being used to modify PS1

bt flag

I shortened my bash prompt by appending the following code block in ~/.bashrc file.

get_PS1(){
    local pwdmaxlen=30
    local trunc_symbol="\[$(tput setaf 1)$(tput bold)\]..."
    
    if [[ "${#PWD}" -gt "$pwdmaxlen" ]]; then
        local right_chunk="\[$(tput setaf 4)$(tput bold)\]${PWD:$((${#PWD}-$pwdmaxlen)):${#PWD}}\[$(tput sgr0)\]"
        PS1="${trunc_symbol}${right_chunk} \$ "
    else
        PS1="\[$(tput setaf 4)$(tput bold)\]\$(pwd) \$\[$(tput sgr0)\] "
    fi
}

PROMPT_COMMAND=get_PS1

When it is active, I mean after exec bash, if I create a virtual environment with virtualenv venv and subsequently activate it with source venv/bin/activate, (venv) should have shown up at the beginning of bash prompt. There's no way to understand that I'm running a virtual environment, although which python does in fact, show that I'm inside a virtual environment.

From what I understand, this happens only when I use PROMPT_COMMAND variable. It doesn't matter, what type of modification I did inside get_PS1 function, It'll always behave this way. I know the PROMPT_DIRTRIM way, but that's not what I'm looking for.

Bash version is 5.0.17(1)-release. I'm running Kubuntu 20.04.3 LTS.

By the way, I learned this code block from here.

Thanks. Let me know if any other info is required.

Score:0
bt flag

It can be done with the assistance of VIRTUAL_ENV variable. When a virtual environment eg. env is activated, it's path is stored in VIRTUAL_ENV and can be seen with echo $VIRTUAL_ENV.

Now format, filter and store the virtual environment name with this:

local virt_env=`printf "($(echo $VIRTUAL_ENV | awk -F "/" '{print $NF}'))"`;

After that, append the virt_env variable to PS1 when required. After adding new logic the get_PS1 will look like this:

get_PS1(){
    local pwdmaxlen=30
    local trunc_symbol="\[$(tput setaf 1)$(tput bold)\]..."
    local virt_env=`printf "($(echo $VIRTUAL_ENV | awk -F "/" '{print $NF}'))"`;

    if [[ "${#PWD}" -gt "$pwdmaxlen" ]]; then
        local right_chunk="\[$(tput setaf 4)$(tput bold)\]${PWD:$((${#PWD}-$pwdmaxlen)):${#PWD}}\[$(tput sgr0)\]";
        if [[ -n "$VIRTUAL_ENV" ]]; then
            PS1="${virt_env} ${trunc_symbol}${right_chunk} \$ ";
        else
            PS1="${trunc_symbol}${right_chunk} \$ ";
        fi
    else
        if [[ -n "$VIRTUAL_ENV" ]]; then
            PS1="${virt_env} \[$(tput setaf 4)$(tput bold)\]\$(pwd) \$\[$(tput sgr0)\] ";
        else
            PS1="\[$(tput setaf 4)$(tput bold)\]\$(pwd) \$\[$(tput sgr0)\] ";
        fi
    fi
}

note: the if-else code blocks could probably be improved, but it did the job for me.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.