agkozak / agkozak-zsh-prompt

A fast, asynchronous Zsh prompt with color ASCII indicators of Git, exit, SSH, virtual environment, and vi mode status. Framework-agnostic and customizable.
MIT License
290 stars 26 forks source link

Add virtualenv indicator #26

Closed gumikola closed 4 years ago

gumikola commented 4 years ago

Hi. I wanna switch from Pure theme completely to agkozak-zsh-prompt but stopping me from this is a lack of prompt virtualenv. Can you add this indicator in the future?

agkozak commented 4 years ago

It looks as if that should not be at all hard to accomplish. Give me a couple of days and I'll throw something together and you can try it out for me.

agkozak commented 4 years ago

Try out what I've got on the venv branch. I added a simple indicator for virtualenv and conda. If it works for you, I'll continue to work on it a bit. (Note that it won't work out of the box if you're using an advanced custom prompt, although it can be made to work, of course.)

gumikola commented 4 years ago

Work like a charm! Miss only docs how to change position because I prefer indicator after git status despite this is still very useful. Thank you.

image

agkozak commented 4 years ago

I'm so glad you like it, and thanks so much for encouraging me to add this needed feature.

The trick about the positioning of the prompt elements is this: the Git status is the only asynchronous one, meaning that it will always appear just a moment after everything else. For that reason, I'll probably never make a simple setting that will have it pop up between the path and the virtualenv indicator -- the effect of the prompt being redrawn just looks to ugly to me.

But give the following settings a try. It may turn out that you're only working on really fast machines -- in that case, you may not mind the redrawing effect much:

# Exit status 
AGKOZAK_CUSTOM_PROMPT='%(?..%B%F{red}(%?%)%f%b )'
# Command execution time (probably moving to this position in next version of
# agkozak-zsh-prompt)
AGKOZAK_CUSTOM_PROMPT+='%(9V.%9v .)'
# Path
AGKOZAK_CUSTOM_PROMPT+='%B%F{blue}%2v%f%b'
# Git status (followed by newline)
AGKOZAK_CUSTOM_PROMPT+='%(3V.%F{243}%3v%f.)'
# virtualenv/conda indicator
AGKOZAK_CUSTOM_PROMPT+=$'%(10V. %F{${AGKOZAK_COLORS_VENV:-green}}[%10v]%f.)\n'
# Prompt character
AGKOZAK_CUSTOM_PROMPT+='%F{magenta}%(4V.❮.❯)%f '

# Right prompt is empty
AGKOZAK_CUSTOM_RPROMPT=''

You'll see that the prompt string %10v is the one that contains the virtualenv name.

I'll keep working on the new version of the prompt. It might not come out for a few weeks, so you can keep using this branch. If I make any changes to how it works, I'll let you know here. Let me know if you have any more suggestions.

agkozak commented 4 years ago

I think I've got it working for pipenv, too.

gumikola commented 4 years ago

With poetry works too. Thanks for the effort. The position of indicator is not important.

agkozak commented 4 years ago

It looks as if poetry appends a hash and a python version to the name of the virtualenv directory, e.g.

[poetry-demo-uyC2R89H-py3.7]

I ran into a similar problem with pipenv. I've committed a fix so that the garble doesn't show.

agkozak commented 4 years ago

I've also got the prompt handling situations when

poetry config virtualenvs.in-project true

has been set.

gumikola commented 4 years ago

Work very well. Thank you. I think can you close the ticket and merge to master :)

agkozak commented 4 years ago

Thanks again for your help in improving the prompt. I will update the documentation and add a demo for the new feature and release a new version of the prompt in the next few days.

agkozak commented 4 years ago

All right, I just released this feature in v3.7.0 on the master branch. I added an array AGKOZAK_VIRTUALENV_CHARS, which can specify the characters to display around the virtual environment indicator (default: ( '[' ']' ).