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

Allow option to display the $virtualenv differently, with more of $VIRTUAL_ENV as context #5889

Open
marvingreenberg opened this issue Mar 27, 2024 · 7 comments
Labels
✨ enhancement A new feature implementation.

Comments

@marvingreenberg
Copy link

Feature Request

Is your feature request related to a problem? Please describe.

The $virtualenv tag in python projects pretty much always says (.venv) since the .venv dir is always called that. The problem is that switching directories and things you can get into a situation where you are using the wrong venv. And various IDEs automatically set venv (Im looking at you vscode) so the shells that get opened may not be what you expect.

~/g/website-support-scripts feat/WEB-8765-migrate-script… 2?  is 📦 v0.0.2 [ v16.19.0][🐍 v3.12.2(.venv)]
10:23:18 ❯ echo $VIRTUAL_ENV
/Users/marvingreenberg/git/tws-stack-deployment/.venv

Here, my shell is using the venv from some other project... Not what is wanted.

Describe the solution you'd like

A setting to modify how $virtualenv behaves, instead of just displaying .venv (or whatever the .venv dir is?)

  • Add option virtualenv_qualified_dir : true that would display, e.g., (tws-stack-deployment/.venv)
  • Truncate to x character, maybe configurable, or have qualified_dir_len: 9 (default 0) e.g., (tws-…ment/.venv)
  • Only display qualification when the virtual env seems ambiguous so just say (.venv)
    when VIRTUAL_ENV and current dir (or subdir) seems consistent with the PWD

Or some combination of one or more of those. The last one is not immediately obvious exactly how to check
if the VIRTUAL_ENV is "consistent". With the first and third implemented, implementing the second is much less important and maybe even undesirable? Obviously more thought to the option name(s) to be consistent with existing styles/practice

Describe alternatives you've considered

I know (I think) I can write a custom tag (sic) to format the appropriate info from the VIRTUAL_ENV env var (although I tried a few iteraions unsuccessfully, so far). But this seems to be a reasonable useful feature to have builtin.

@marvingreenberg marvingreenberg added the ✨ enhancement A new feature implementation. label Mar 27, 2024
@marvingreenberg
Copy link
Author

So I got a custom command working (almost). The part in the [python] section isn't supported - I guess I would have to break out the top level format string, which seems complicated.

[custom.venv]
description = "Show qualified virtualenv dir, if not IN the dir tree where it is resides"
# from directory ../project-name/, . ~/some/other/.venv/bin/activate -- displays 'other/.venv'
# from directory .../other/src, .../other, etc. , -- displays just .venv
command = """
    qual=$(basename $(dirname "$VIRTUAL_ENV"));
    venv=$(basename "$VIRTUAL_ENV");
    pat=".*/${qual}/*(/.*)?$";
    [[ "$PWD" =~ $pat ]] && printf "($venv)" || printf "($qual/$venv)"
    """
when = '''[[ -n "$VIRTUAL_ENV" ]]'''
format = '[$output](blue)'

[python]
format = '\[[${symbol}${pyenv_prefix}(${version})(\(${custom.venv}\))]($style)\]'

@jankatins
Copy link
Contributor

Maybe as an alternative solution: use --prompt "name" to set a different name?

[22:01:57] λ  python -m venv .venv --prompt "whatever"

~/projects/whatever on  feature/pagination (c2c6c249)  v3.12.2 (venv:whatever)  took 5s
[22:02:10] λ

The first venv: comes from my format definition:

[python]
format = "[$symbol$version( \\(venv:$virtualenv\\))]($style) "

@marvingreenberg
Copy link
Author

Interesting. But what really motivated this was several times vscode somehow getting the venv wrong, or just changing directories. I'm sort of fine with what I already did. But I think it is a generally useful idea. I'm happy to close it if you think it's pointless, or not useful enough. As an aside, is there a way to do something like my custom command, or do I have to just rewrite the top level format string.

Btw, starship is killer.

@marvingreenberg
Copy link
Author

And here's an example of how my (badly integrated) custom venv looks...

~/g/sample-python (0bdef92) 3?  [🐍 v3.12.2]
08:53:46 ❯ . ./.venv/bin/activate

~/g/sample-python (0bdef92) 3?  [🐍 v3.12.2](.venv)
08:53:59 ❯ cd ../tws-stack-deployment

~/g/tws-stack-deployment develop 2$ 1?  [🐍 v3.12.2](sample-python/.venv)
08:54:20 ❯ . .venv/bin/activate

~/g/tws-stack-deployment develop 2$ 1?  [🐍 v3.12.2](.venv)
08:54:26 ❯ cd -
~/git/sample-python

~/g/sample-python (0bdef92) 3?  [🐍 v3.12.2](tws-stack-deployment/.venv)

Could keep all the config the same (just change the behavior of $virtualenv) or maybe add a flag option like warn_by_qualifying_virtualenv_with_directory_when_it_seems_like_it_might_be_wrong_venv: true

Again, starship is a great package, I'm fine if you prefer to "close won't fix". If I can figure out the proper way to include it in the [python] configuration, I'll add it to that issue for examples of custom scripts.

@marvingreenberg
Copy link
Author

marvingreenberg commented Mar 28, 2024

And, BTW, here was my motivator -- I guess I should debug why my vscode config is broke

image

I don't know where it is getting its "memory" of the correct venv to use, and it isn't always wrong...

@NodeJSmith
Copy link

NodeJSmith commented Apr 23, 2024

Just want to +1 this, stumbled upon this after dealing with the same issue as @marvingreenberg. I'd love the ability to show a bit more information about the virtualenv - I name all of mine .venv, so just showing that isn't really enough for me to know that I'm in the absolutely wrong virtual environment for absolutely no reason. Also, agreed with the above, starship is killer

Edit: Maybe another potential solution would be showing the value in a certain environment variable, such as VENV_NAME or VENV_PROMPT or STARSHIP_VENV_{NAME/PROMPT} - that would let users set this on their own using whatever logic they want

@NodeJSmith
Copy link

@marvingreenberg BTW your config works well enough for me, thank you for sharing it, I can finally stop wasting time finding a way to warn myself when I'm in the wrong one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ enhancement A new feature implementation.
Projects
None yet
Development

No branches or pull requests

3 participants