Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add active environment name to PROMPT env var (Windows Support) #2116

Closed
gtalarico opened this issue May 1, 2018 · 7 comments
Closed

Comments

@gtalarico
Copy link
Contributor

gtalarico commented May 1, 2018

On unix/fish, when a shell is started using pipenv shell, the $VIRTUAL_ENV variable can be used to provide a visual indicator on the shell. For Example, if the users prompt is (~/folder) $
once the shell is activated using pipenv shell the prompt becomes:
(~/folder) (pipenvfolder-1234) $
or similar. This currently works well with zsh/fish, etc and in general in the unix world is fairly easy to customize prompt.

On windows however, `%PROMPT% variable is used instead, and the variable is used by both cmd.exe and cmder console to add the virtual environment to the user's prompt.

Upon activating an environment, the environment's name is prepended to the $PROMPT var.
As far as I know, this is not customizable on cmd.exe, and is not easily customizable with cmder neither.

cmd.exe behavior with virtualenv / virtualenv-wrapper

Active Environment is shown on prompt

image

cmder behavior with virtualenv / virtualenv-wrapper

Active Environment is shown on prompt

image

cmd and cmder with pipenv shell - No prompt indicator

Environment IS NOT shown anywhere on the prompt

image

image

Proposed solution

For the pipenv environment to be shown by default on the built-in cmd.exe and other windows-friendly consoles, pipenv could prepend the shell environment name to the PROMPT variable in the same way virtualenv does.

Virtualenv stores that old prompt in _OLD_VIRTUAL_PROMPT so it can restore the original on deactivate, but pipenv would not need that since the process is destroyed.

Working example on cmd.exe

image

Working example on cmder

image

I would be happy to work on a PR if the proposal is accepted.

PS: Pipenv environment works perfectly out of the box on my fish shell
image

@gtalarico gtalarico changed the title Feature: Add active environment name to PROMPT Var Feature: Add active environment name to PROMPT env var (Windows Support) May 1, 2018
@uranusjr
Copy link
Member

uranusjr commented May 2, 2018

I agree with this (it’s an oversight Windows support is not added). Work on it! You might not need to mess with environment variables yourself, but can leverage existing scripts to do it. The activation is done in core.activate_virtualenv; try adding cases for cmd and Powershell first and see if that’s enough.

@gtalarico
Copy link
Contributor Author

Sounds good. I will see what I can do.

In the meantime, I have a really simple and practical workaround, which is to add
export PROMPT="(pipenv-projectname)$P$G" to the project's .env file so the prompt is set when the pipenv shell is loaded

image

@uranusjr
Copy link
Member

uranusjr commented May 2, 2018

I actually wrote some Lua to do this in Cmder :p I don’t have my Windows machine around now, but can probably add a page to wiki about this.

@techalchemy
Copy link
Member

FYI we have discussed this in the past and rejected it because cross platform support is hard, but if you can come up with something we’d review

@gtalarico
Copy link
Contributor Author

Can you guys point me to the discussion so I don't make the same mistake and propose the same thing :)

Do we not use the same activate script from virtualenvs bat to activate on windows?
Here is how the windows specific prompt is handled on embedded virtualenv
https://github.com/pypa/virtualenv/blob/263283be88061b965ead9ebff47c37021d7d3f62/virtualenv_embedded/activate.bat#L12

@uranusjr
Copy link
Member

uranusjr commented May 2, 2018

The catch is that you cannot assume CMD (there’s Powershell, and as you know Fish etc. also work), and contrary to POSIX, shell detection on Windows is much more difficult.

@uranusjr
Copy link
Member

This should be fixed with the above PR merge. (Please correct me if I’m wrong.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants