From 70cfff448d15933882526d3b12411b6a6b16abf0 Mon Sep 17 00:00:00 2001 From: Jeonguk Choi <91456421+cjeonguk@users.noreply.github.com> Date: Tue, 16 Jul 2024 01:56:44 +0900 Subject: [PATCH] feat(conda-env): add plugin for conda prompt display (#10619) Co-authored-by: Jeonguk Choi <91456421+devj121@users.noreply.github.com> --- lib/prompt_info_functions.zsh | 1 + plugins/conda-env/README.md | 44 ++++++++++++++++++++++++++ plugins/conda-env/conda-env.plugin.zsh | 9 ++++++ 3 files changed, 54 insertions(+) create mode 100644 plugins/conda-env/README.md create mode 100644 plugins/conda-env/conda-env.plugin.zsh diff --git a/lib/prompt_info_functions.zsh b/lib/prompt_info_functions.zsh index 29aca9b48..722ae58c0 100644 --- a/lib/prompt_info_functions.zsh +++ b/lib/prompt_info_functions.zsh @@ -20,6 +20,7 @@ function chruby_prompt_info \ jenv_prompt_info \ azure_prompt_info \ tf_prompt_info \ + conda_prompt_info \ { return 1 } diff --git a/plugins/conda-env/README.md b/plugins/conda-env/README.md new file mode 100644 index 000000000..ccf48a392 --- /dev/null +++ b/plugins/conda-env/README.md @@ -0,0 +1,44 @@ +# conda-env + +The plugin displays information of the created virtual container of conda and allows background theming. + +To use it, add `conda-env` to the plugins array of your zshrc file: +``` +plugins=(... conda-env) +``` + +The plugin creates a `conda_prompt_info` function that you can use in your theme, which displays the +basename of the current `$CONDA_DEFAULT_ENV`. + +You can use this prompt function in your themes, by adding it to the `PROMPT` or `RPROMPT` variables. See [Example](#example) for more information. + +## Settings + +It uses two variables to control how the information is shown: + +- `ZSH_THEME_CONDA_PREFIX`: sets the prefix of the CONDA_DEFAULT_ENV. +Defaults to `[`. + +- `ZSH_THEME_CONDA_SUFFIX`: sets the suffix of the CONDA_DEFAULT_ENV. +Defaults to `]`. + +## Example + +```sh +ZSH_THEME_CONDA_PREFIX='conda:%F{green}' +ZSH_THEME_CONDA_SUFFIX='%f' +RPROMPT='$(conda_prompt_info)' +``` + +## `CONDA_CHANGEPS1` + +This plugin also automatically sets the `CONDA_CHANGEPS1` variable to `false` to avoid conda changing the prompt +automatically. This has the same effect as running `conda config --set changeps1 false`. + +You can override this behavior by adding `unset CONDA_CHANGEPS1` in your `.zshrc` file, after Oh My Zsh has been +sourced. + +References: + +- https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#determining-your-current-environment +- https://conda.io/projects/conda/en/latest/user-guide/configuration/use-condarc.html#precedence diff --git a/plugins/conda-env/conda-env.plugin.zsh b/plugins/conda-env/conda-env.plugin.zsh new file mode 100644 index 000000000..c710c952f --- /dev/null +++ b/plugins/conda-env/conda-env.plugin.zsh @@ -0,0 +1,9 @@ +function conda_prompt_info(){ + [[ -n ${CONDA_DEFAULT_ENV} ]] || return + echo "${ZSH_THEME_CONDA_PREFIX=[}${CONDA_DEFAULT_ENV:t:gs/%/%%}${ZSH_THEME_CONDA_SUFFIX=]}" +} + +# Has the same effect as `conda config --set changeps1 false` +# - https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#determining-your-current-environment +# - https://conda.io/projects/conda/en/latest/user-guide/configuration/use-condarc.html#precedence +export CONDA_CHANGEPS1=false