twolfson / sexy-bash-prompt

Bash prompt with colors, git statuses, and git branches.
MIT License
1.14k stars 154 forks source link

Conda environment name lost #90

Closed Georacer closed 4 years ago

Georacer commented 4 years ago

Hi there!

I really nice this utility, the new line before the command is a genius move!

Sadly I found a shortcoming. I'm regularly using conda to manage my development environment. When one types conda activate <envname> the environment is "activated" and its name is normally displayed before the prompt: (env_name) user@computer: <dir>$

The current configuration of sexy-bash-prompt doesn't allow for that. I get no indication of which environment I have activated.

Is there a way to add this?

Thanks!

Georacer commented 4 years ago

Found a possible solution, after a few hours of sed hacking:

# Find conda environment
function get_conda_env() {
    conda_path="$(which conda)"
    if [[ "$conda_path" != "" ]]; then
        conda_env="$(conda info 2> /dev/null | sed -e '/^[[:blank:]]*active environment/!d' | sed -r 's/^[[:blank:]]+active environment : ([[:alnum:]]+).*$/\1/g')"
        if [[ "$conda_env" == "None" ]]; then
            echo ""
        else
            echo "($conda_env) "
        fi
    else
        echo ""
    fi
}
Georacer commented 4 years ago

The only problem is that the base command conda info takes a second to return, so each new shell is less snappy at startup.

Perhaps a more hacky but more performing way would be to capture the user's conda activate <env_name> command and use that as info source? Would that be possible?

rpdelaney commented 4 years ago

I think the plethora of tools like this makes supporting them prohibitive. Curious if Todd will have a different opinion though.

twolfson commented 4 years ago

Oooh, this issue was introduced by our move to PROMPT_COMMAND in 1.0.0, which was necessary for exit code support

I've used a similar setup that extends PS1 with virtualenvwrapper (currently assuming this) as well as a custom chruby detection:

https://github.com/twolfson/dotfiles/blob/2017.1113.1132/.bashrc#L371-L372

Unsure if I have time to dig around on this tonight. Going to poke for maybe a few more minutes

If you don't hear from me, then assume I'll take a deeper look by Tuesday night

twolfson commented 4 years ago

Alright, so it looks like virutalenvwrapper/virtualenv does PS1 extension as I expected:

Since that's such an established library, we really should figure out how to support PS1 shells =/ Maybe there's something with enabling exit code coloring via an opt-in, maybe something else

I do know that one solution would be user's extend their own PROMPT_COMMAND but that seems backwards to me =/

Going to double check I didn't miss anything obvious like PS1=content$PS1 (feel like this would overbuild our PS1 every time)

twolfson commented 4 years ago

Alllllright, got a real quick proof of concept working but the summarized solution is:

https://github.com/twolfson/sexy-bash-prompt/blob/cf802b2dc2476431d9c0f53b3ab67d5c3ae6401e/.bash_prompt#L260-L268

Selection_223

I don't believe I have time to fully resolve this at the moment but for now I think it's fair to prioritize shell extension over exit code support

As a result, going to rollback 1.0.0 PROMPT_COMMAND changes as patch version 1.0.1. This also dovetails nicely in that the fix will look more like the 0.30.0 version than the 1.0.0 version

twolfson commented 4 years ago

Alright, patched/reverted 1.0.1 has been released. I'll look into a proper reimplementation soon

No hard timeline since I don't want to rush it and ideally will have some local testing, I'm guessing tonight or next few days for a prototype and 1-2 weeks for PR + PR approval

Technically speaking, this issue is now resolved -- we're just missing exit code support once again, so closing the issue as resolved and reopening the exit code one

Georacer commented 4 years ago

Okaayy... all of this went COMPLETELY over my head; I don't have the necessary background to digest this. But I'm happy that I triggered an interesting walkabout.

However, a bit irrelevant and just for the record, I found that the necessary information that I was looking for was in an environment variable called CONDA_PROMPT_MODIFIER. I could have grabbed it from there.

twolfson commented 4 years ago

Heh, as long as you understand that if you reinstall the latest version that no workaround is needed, then we're all good =)

And yes, it looks like CONDA_PROMPT_MODIFIER is what you would have needed

Fwiw, PS1 is the default shell prompt that everyone sees and PROMPT_COMMAND is commands that are run before every prompt is shown

https://github.com/conda/conda/blob/50093e14c6a96c7cd4f6c4570cc09bc5b763ede4/conda/activate.py#L848-L863

Georacer commented 4 years ago

Fwiw, PS1 is the default shell prompt that everyone sees and PROMPT_COMMAND is commands that are run before every prompt is shown

https://github.com/conda/conda/blob/50093e14c6a96c7cd4f6c4570cc09bc5b763ede4/conda/activate.py#L848-L863

Aha, interesting... so conda uses the same mechanism as you (and I guess other people too) to modify the command prompt. The more you know...

twolfson commented 4 years ago

As a heads up, exit code support has been added back in 1.1.0 with aforementioned PS1 patch. Let me know if it breaks your use case once again or not (pretty confident we're good now)

Georacer commented 4 years ago

Works just dandy! Thanks!

In detail, the environment name works when I enable it. Any easy way to test the exit error support?

twolfson commented 4 years ago

Any easy way to test the exit error support?

Using a command like false should do the trick. Also using a keyboard interrupt on any running command (e.g. time cat -> Ctrl+C)

Georacer commented 4 years ago

Yes, the $ sign become red when erroring out withing a conda environment.