lyze / posh-git-sh

Bash/ZSH version of the posh-git command prompt

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

conda environment suffix is lost

rcassani opened this issue · comments

Using conda, when an environment different to root is used, the name of the environment is prepended in the system command:
(environment-name) user@linux ~ $
This behavior is lost when using posh-git-sh is in use.
Regardless the active conda environment, the command always prompt looks this way:
user@linux ~ $
The Git Status information still displayed correctly

I need more details on how you invoked posh-git-sh. I'm guessing you used__posh_git_ps1 in PROMPT_COMMAND? If so, then things are working as intended. The function contract promises to overwrite PS1.

I did a cursory search of the internet, and it looks like conda clobbers PS1. That means you'll need to configure conda so that it doesn't do that, and manually build PS1 yourself in PROMPT_COMMAND. You will want to look into using __posh_git_echo.

I was using :
PROMPT_COMMAND='__posh_git_ps1 "\u@\h:\w" "\\\$ ";'$PROMPT_COMMAND

As far as I understand, __posh_git_ps1 does overwrite PS1 however it does it with "fixed" data, User \u , Host \h, Working directory \w, and $, then the changes made in PS1 by the activate script (called by conda) are not taken into account by __posh_git_ps1 to generate the new PS1.

In the pull request I did, the last PS1 (entire) is used as argument for __posh_git_ps1, unfortunately, it may already have a git status string attached, therefore it's needed to remove any previous git status string before adding the new one git status string

For this use case, you should do something like

PROMPT_COMMAND='__posh_git_ps1 "$(get_conda_env_somehow)\u@\h:\w" "\\\$ ";'$PROMPT_COMMAND

where get_conda_env_somehow needs to be implemented to print the conda environment name (or none).

Right, that will work as well.
Thanks

I wrote down the implementation for obtaining the current conda environment:

#!/bin/bash
# Parses the output of `conda-env list` to obtain the current conda environment in $PATH
__get_current_conda_env ()
{
  cce=`conda-env list | awk '/*/ {print $1 ":" $3}'`
  cce_path="${cce#*':'}"
  cce_name="${cce%':'*}" 
  # If the /bin directory of the current environment is in $PATH return current environment name
  if [[ "$PATH" == ?(*:)"$cce_path/bin"?(:*) ]]; then
	echo "($cce_name) "
  else
	echo ""
  fi
}

Then the PROMPT_COMMAND as

PROMPT_COMMAND='__posh_git_ps1 "$(__get_current_conda_env)\u@\h:\w" "\\\$ ";'$PROMPT_COMMAND

While this does the trick, it has a minor drawback as only executing conda-env list takes ~300ms, this time is perceived as a delay at each time the prompt is generated.

Perhaps you can open a feature request with conda to export the current environment name as a shell environment variable?

I know this is an old issue but since I came across it today, and it took me quite some time to find a good solution, I'll leave this here:

As I found out here, conda exports, among other things, CONDA_PROMPT_MODIFIER which is exactly what I needed. My PROMPT_COMMAND is now

PROMPT_COMMAND='__posh_git_ps1 "$CONDA_PROMPT_MODIFIER\u@\h:\w " "\$ ";'$PROMT_COMMAND

and that works as expected.