-
Notifications
You must be signed in to change notification settings - Fork 937
[Bugfix] virtualenv prompt displaying #1128
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thx @ymage . The code LGTM.
Related to #1079 (comment)
Yes, it reverts #1079 and it seems to me more logical to use true/false value for this var. |
This change seems right to me, although the rest seems like a minefield to me: |
Well, As @shengje said, VIRTUAL_ENV_DISABLE_PROMPT seems to expect a true/false value. Other idea : we could rely on another variable and leave VIRTUAL_ENV_DISABLE_PROMPT on its own ... |
If a user wants to not show that segment, he could simply remove that segment. But we want to interact with other tools that set Could you describe your workflow with virtualenv, so we (or at least I) get a better picture? How you use this variable? |
My issue was I was unable to display the segment whatever the value set for VIRTUAL_ENV_DISABLE_PROMPT since the other fix. I was using powerlevel9k with zplug as zsh framework. I switched to zplugin. But it didn't fix anything. But I am open if you have some good idea for me :) |
Unfortunately not yet. I am asking all this questions, because I want to figure out if we need to change the way we look at this variable. As you described your workflow, you seem to set the variable by hand. But I am not sure if this is the right thing to do. There might be users that use a plugin that sets that variable automatically. In this case, we should interpret the value. There are multiple cases we should look at:
So in the first case we are free to use any value, because we can tell that value to the user. But in the second case we are not free. To end this argument, I'd like to hear some other voices. I can't tell if this is right or wrong. |
I just saw that oh my zsh virtualenv plugin set VIRTUAL_ENV_DISABLE_PROMPT=1 |
Maybe we should change the code to something like (from the # Early exit; $virtualenv_path must always be set.
[[ -z "$virtualenv_path" ]] && return
# Check if VIRTUAL_ENV_DISABLE_PROMPT is set to false, or is a numerical value
if [[ "$VIRTUAL_ENV_DISABLE_PROMPT" == "false" ]] || (( VIRTUAL_ENV_DISABLE_PROMPT )); then
p9k::prepare_segment "$0" "" $1 "$2" $3 "${VIRTUAL_ENV:t}"
fi Could you check if this would work with all combinations (like |
I can't find p9k::prepare_segment function/method ? |
But this did the trick : [[ -z "$virtualenv_path" ]] && return
# Check if VIRTUAL_ENV_DISABLE_PROMPT is set to false, or is a numerical value
if [[ "$VIRTUAL_ENV_DISABLE_PROMPT" == "false" ]] || (( VIRTUAL_ENV_DISABLE_PROMPT )); then
"$1_prompt_segment" "$0" "$2" "blue" "$DEFAULT_COLOR" "$(basename "$virtualenv_path")" 'PYTHON_ICON'
fi |
Awesome! @josselinauguste and @shengje could you confirm this works? |
I confirm it works great fix ! |
Okay. I digged some more into that topic and I finally understand how that virtualenv is set and what Code HistoryInterestingly that condition took several turns, true/false, zero, non-zero, we had them all (ae035e5 , 5c412b4 ). But it was in P9K since the very first commit. |
If I get it right, it means disable the abilty to use wrapper variables to manage segment displaying and let it only set via theme configuration. Right ? |
There are three aspects:
export VIRTUAL_ENV_DISABLE_PROMPT=${VIRTUAL_ENV_DISABLE_PROMPT:-1} |
I understand. Even if I found pretty sad we have to deal with other framework mess today ... And probably tomorrow. :( |
I mean I am not sure it's the theme job to set such a variable ... |
Well. On one hand I agree with you, but on the other hand, if we don't do this, I sense a lot of complaints coming towards us.. So I think it is better to set that variable.. |
Not sure. |
Not quite. This code you mentioned works in the wrong direction. It reads |
I tried raw virtualenv w/activate script. Can you share some context in which it is an issue ? |
Here is a test for that segment. I hope that makes it a bit clearer what I am trying to say.. #!/usr/bin/env zsh
#vim:ft=zsh ts=2 sw=2 sts=2 et fenc=utf-8
# Required for shunit2 to run correctly
setopt shwordsplit
SHUNIT_PARENT=$0
function setUp() {
export TERM="xterm-256color"
__P9K_HOME="${PWD}"
# Load Powerlevel9k
source powerlevel9k.zsh-theme
# Test specific
# Backup old virtualenv
__OLD_VIRTUAL_ENV="${VIRTUAL_ENV}"
}
function tearDown() {
unset VIRTUAL_ENV
[[ -n "${__OLD_VIRTUAL_ENV}" ]] && VIRTUAL_ENV="${__OLD_VIRTUAL_ENV}"
unset __OLD_VIRTUAL_ENV
}
function mockVirtualenv() {
VIRTUAL_ENV="$1"
# Taken from https://github.com/pypa/virtualenv/blob/685b1af0405e0c94411951389310960ff7ac8e1e/virtualenv_embedded/activate.sh
# With added forced fail.
if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT-}" ] ; then
_OLD_VIRTUAL_PS1="${PS1-}"
if [ "x__VIRTUAL_PROMPT__" != x ] ; then
PS1="__VIRTUAL_PROMPT__${PS1-}"
else
PS1="(`basename \"$VIRTUAL_ENV\"`) ${PS1-}"
fi
export PS1
fail "Executing unnecessary code!"
fi
}
function testVirtualenvDoesNotExecuteUnnecessaryCodeWithVirtualEnvDisablePromptTruthy() {
local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(virtualenv)
local VIRTUAL_ENV_DISABLE_PROMPT="true"
# Activate virtualenv
mockVirtualenv "mv/super/venv"
assertEquals "Unable to set VIRTUAL_ENV_DISABLE_PROMPT=true" "%K{004} %F{000}venv %k%F{004}%f " "$(build_left_prompt)"
VIRTUAL_ENV_DISABLE_PROMPT="1"
# Activate virtualenv
mockVirtualenv "mv/super/venv"
assertEquals "Unable to set VIRTUAL_ENV_DISABLE_PROMPT=1" "%K{004} %F{000}venv %k%F{004}%f " "$(build_left_prompt)"
VIRTUAL_ENV_DISABLE_PROMPT="12"
# Activate virtualenv
mockVirtualenv "mv/super/venv"
assertEquals "Unable to set VIRTUAL_ENV_DISABLE_PROMPT=12" "%K{004} %F{000}venv %k%F{004}%f " "$(build_left_prompt)"
VIRTUAL_ENV_DISABLE_PROMPT="xx"
# Activate virtualenv
mockVirtualenv "mv/super/venv"
assertEquals "Unable to set VIRTUAL_ENV_DISABLE_PROMPT=xx" "%K{004} %F{000}venv %k%F{004}%f " "$(build_left_prompt)"
}
function testVirtualenvDoesNotExecuteUnnecessaryCodeWithVirtualEnvDisablePromptFalsy() {
local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(virtualenv)
local VIRTUAL_ENV_DISABLE_PROMPT="false"
# Activate virtualenv
mockVirtualenv "mv/super/venv"
assertEquals "Unable to set VIRTUAL_ENV_DISABLE_PROMPT=false" "%K{004} %F{000}venv %k%F{004}%f " "$(build_left_prompt)"
VIRTUAL_ENV_DISABLE_PROMPT="0"
# Activate virtualenv
mockVirtualenv "mv/super/venv"
assertEquals "Unable to set VIRTUAL_ENV_DISABLE_PROMPT=0" "%K{004} %F{000}venv %k%F{004}%f " "$(build_left_prompt)"
}
function testVirtualenvDoesNotExecuteUnnecessaryCodeWithVirtualEnvDisablePromptUnset() {
local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(virtualenv)
# Activate virtualenv
mockVirtualenv "mv/super/venv"
assertEquals "Unable to set VIRTUAL_ENV_DISABLE_PROMPT=0" "%K{004} %F{000}venv %k%F{004}%f " "$(build_left_prompt)"
}
source shunit2/shunit2
|
Thank you @dritter for the time you took to clarify. I think I better understand your point and the issue to solve. My point is I am not sure this is the right way to fix this because from my point of view it's not the theme responsibility to do it. |
Well It can. And some frameworks may do that. But others probably not. And users need to know that variable or the impact of not setting that variable. And that is my point. BUT I want to end this argue. Having looked at the activate code, I think that the performance impact is not that big and hope that it does not interfere with P9K. So I am willing to merge this PR as is. |
Thank you @dritter |
Thx @ymage . |
Fix #1127