Coloured virtualenv name in prompt ($PS1) #509

Closed
wants to merge 3 commits into
from

Conversation

Projects
None yet
5 participants

nicopernas commented Dec 2, 2013 edited by BrownTruck

  • A more visual way to know if the virtual environment is currently activated.
  • The virtual environment name at the beginning of the prompt ($PS1) will appear in color.
  • By default the color green is used. This can be changed using
    'export VIRTUAL_ENV_PS1_COLOR=$COLOR_CODE' before sourcing activate script.
  • For bash only.
Contributor

xen0n commented Dec 27, 2013

Hello, this feature definitely seems interesting! However, besides the Bash-only limitation there is the obvious case of non-color-capable terminals... Admittedly these are rare in modern day's use cases, but it's better IMO to take care of those users as well.

FYI, bash does its color capability detection in /etc/bash/bashrc for colorful PS1 but unfortunately for reducing environment pollution the result is abandoned, so we have to duplicate the code in activate scripts if we want to do the same thing. This actually makes me a little bit hesitant to write the patch... so what's your opinion here?

I was looking into something similar because I wanted a different PS1 as well (with color). I think a better solution is to let the user set their own custom PS1, but stored in their .bash_profile (or equivalent) e.g.:

in .bash_profile:

VIRTUAL_ENV_PS1="${PS1%"\\$ "}\[\033[01;31m\]( VIRTUAL_ENV_NAME )\[\033[00m\]\$ "

Then we'd modify activate to replace VIRTUAL_ENV_NAME with something like

#Perhaps create a get_virtual_env_name() function to handle all the logic, but for example:
VIRTUAL_ENV_NAME="$(basename "$VIRTUAL_ENV")"

if [ -n "$VIRTUAL_ENV_PS1" -a -n "$VIRTUAL_ENV_NAME" ]; then
    PS1="${VIRTUAL_ENV_PS1//VIRTUAL_ENV_NAME/$VIRTUAL_ENV_NAME}"
fi

export PS1

If that has support, I'm willing to create a pull request with the bash version.

Contributor

xen0n commented Jan 17, 2014

I did something like that already for colorful git-prompts, but in /etc/profile for all accounts including root. Color capability detection is done by duplicating the bash code. But actually considering the required knowledge to write ANSI control code by hand and the manual edits, this "enhancement" might just as well be a documentation one...

What do you mean? I don't see any way to create a custom prompt - for example to stick the VIRTUAL_ENV_NAME at the end of PS1, instead of the beginning of PS1

Contributor

xen0n commented Jan 17, 2014

I see; what you want is the possibility to fully customize the PS1 to your liking. However the original PR's focus is on providing a sensible default PS1 that is colorful.

What I meant is, in order to hand-customize PS1 one has to at least reference some material on ANSI control sequence and the like, if he/she wants to have total control on the PS1's appearance. But why force people to do it the hard way if what they ever want is only a colored default value? And it's already beyond virtualenv's scope to teach people on customizing PS1's.

However, the possibility to do the full customization is decent indeed as you suggested; I seriously want my virtualenv name to be inside the PS1 string :D

Member

Ivoz commented Jan 17, 2014

Can't you just set VIRTUAL_ENV_DISABLE_PROMPT and then detect VIRTUAL_ENV when setting your PS1 string and set it accordingly?

@xen0n Yes, the original PR is about color - I was offering an alternative solution. Of course, they are not mutually exclusive solutions. We could, for example, switch to color by default and allow a custom PS1 to fix problems when color is not supported.

@Ivoz How would that work? PS1 is set in your (e.g.) .bash_profile and VIRTUAL_ENV is only set later.

What @chrishiestand mentions here is a great idea but i just wanted a simple way to colorise the venv name in the bash prompt. I agree that both solutions are not exclusive but i think they should be in separated pull requests against original virtualenv repo. I guess is up to @xen0n to decide what to use, right?

Regarding @xen0n's first comment about color detection I guess we'd have to have in mind how many non-color-capable terminals are out there. Following on @Ivoz comment, if it was up to me I would check for an user env var like VIRTENV_DONT_SET_COLOR_PROMPT to disable color capabilities. By default colorised prompt would be enabled. We can document this to be absolutely clear.

nicopernas added some commits Dec 2, 2013

@nicopernas nicopernas Colored virtualenv name in prompt ($PS1)
  - A more visual way to know if the virtual environment is currently
  activated.
  - The virtual environment name at the beginning of the prompt ($PS1) will
  appear in color.
  - By default the color green is used. This can be changed using
  'export VIRTUAL_ENV_PS1_COLOR=$COLOR_CODE' before sourcing activate script.
  - For bash only.
5d86d6d
@nicopernas nicopernas Add way to disable color prompt at venv creation time
When the virtual environment is created - by default - will create a color
prompt. Add way to disable color prompt by setting VIRTUAL_ENV_DISABLE_COLOR
environment variable before creating the venv.

Usage:

# create no-color prompt
$ export VIRTUAL_ENV_DISABLE_COLOR=1
$ virtualenv venv

Signed-off-by: Nicolás Pernas Maradei <nicopernas@gmail.com>
2d344cc
@nicopernas nicopernas Document color prompt feature
Signed-off-by: Nicolás Pernas Maradei <nicopernas@gmail.com>
08d3342

Hello!

As part of an effort to ease the contribution process and adopt a more standard workflow pip has switched to doing development on the master branch. However, this Pull Request was made against the develop branch so it will need to be resubmitted against master. Unfortunately, this pull request does not cleanly merge against the current master branch.

If you do nothing, this Pull Request will be automatically closed by @BrownTruck since it cannot be merged.

If this pull request is still valid, please rebase it against master (or merge master into it) and resubmit it against the master branch, closing and referencing the original Pull Request.

If you choose to rebase/merge and resubmit this Pull Request, here is an example message that you can copy and paste:

A more visual way to know if the virtual environment is currently activated.
The virtual environment name at the beginning of the prompt ($PS1) will appear in color.
By default the color green is used. This can be changed using 
'export VIRTUAL_ENV_PS1_COLOR=$COLOR_CODE' before sourcing activate script.
For bash only.

---

*This was migrated from pypa/virtualenv#509 to reparent it to the ``master`` branch. Please see original pull request for any previous discussion.*

This Pull Request was closed because it cannot be automatically reparented to the master branch and it appears to have bit rotted.

Please feel free to re-open it or re-submit it if it is still valid and you have rebased it onto master or merged master into it.

BrownTruck closed this May 26, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment