From 1ec8f9df6402f0f2d22a7a35528573c5aee8be94 Mon Sep 17 00:00:00 2001 From: Petar Nikolovski Date: Tue, 25 Jan 2022 20:59:06 +0100 Subject: [PATCH 1/4] Add virtualenv prompt for robbyrussell theme --- themes/robbyrussell/robbyrussell.theme.bash | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/themes/robbyrussell/robbyrussell.theme.bash b/themes/robbyrussell/robbyrussell.theme.bash index 956347dd5e..8f7ffb334b 100644 --- a/themes/robbyrussell/robbyrussell.theme.bash +++ b/themes/robbyrussell/robbyrussell.theme.bash @@ -18,8 +18,19 @@ function git_prompt_info() { echo -e "$SCM_PREFIX${bold_red}$SCM_BRANCH$SCM_STATE$SCM_SUFFIX" } +function venv_prompt() { + python_venv="" + # Detect python venv + if [[ -n "${CONDA_DEFAULT_ENV}" ]]; then + python_venv="($PYTHON_VENV_CHAR${CONDA_DEFAULT_ENV}) " + elif [[ -n "${VIRTUAL_ENV}" ]]; then + python_venv="($PYTHON_VENV_CHAR$(basename "${VIRTUAL_ENV}")) " + fi + [[ -n "${python_venv}" ]] && echo "${python_venv}" +} + function prompt_command() { - PS1="${bold_green}➜ ${bold_cyan}\W${reset_color}$(scm_prompt_info)${normal} " + PS1="$(venv_prompt)${bold_green}➜ ${bold_cyan}\W${reset_color}$(scm_prompt_info)${normal} " } PROMPT_COMMAND=prompt_command From ec85f44d70dc60338c73ba2b04f43d7797f598d5 Mon Sep 17 00:00:00 2001 From: Petar Nikolovski Date: Wed, 26 Jan 2022 21:55:00 +0100 Subject: [PATCH 2/4] Add venv prompt to base theme --- themes/base.theme.bash | 11 +++++++++++ themes/robbyrussell/robbyrussell.theme.bash | 15 ++++++--------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/themes/base.theme.bash b/themes/base.theme.bash index 9e4a2562fa..6493637539 100644 --- a/themes/base.theme.bash +++ b/themes/base.theme.bash @@ -625,3 +625,14 @@ function _save-and-reload-history() { local autosave=${1:-0} [[ $autosave -eq 1 ]] && history -a && history -c && history -r } + +function venv_prompt() { + local python_venv="" + if [[ -n "${CONDA_DEFAULT_ENV:-}" ]]; then + python_venv="${CONDA_DEFAULT_ENV}" + PYTHON_VENV_CHAR=${CONDA_PYTHON_VENV_CHAR} + elif [[ -n "${VIRTUAL_ENV:-}" ]]; then + python_venv="${VIRTUAL_ENV##*/}" + fi + [[ -n "${python_venv}" ]] && echo "${PYTHON_VENV_CHAR}${python_venv}" +} diff --git a/themes/robbyrussell/robbyrussell.theme.bash b/themes/robbyrussell/robbyrussell.theme.bash index 8f7ffb334b..23fc885d55 100644 --- a/themes/robbyrussell/robbyrussell.theme.bash +++ b/themes/robbyrussell/robbyrussell.theme.bash @@ -18,19 +18,16 @@ function git_prompt_info() { echo -e "$SCM_PREFIX${bold_red}$SCM_BRANCH$SCM_STATE$SCM_SUFFIX" } -function venv_prompt() { - python_venv="" - # Detect python venv - if [[ -n "${CONDA_DEFAULT_ENV}" ]]; then - python_venv="($PYTHON_VENV_CHAR${CONDA_DEFAULT_ENV}) " - elif [[ -n "${VIRTUAL_ENV}" ]]; then - python_venv="($PYTHON_VENV_CHAR$(basename "${VIRTUAL_ENV}")) " +function venv_parenthases() { + if [[ $(venv_prompt) != "" ]]; then + echo "($(venv_prompt)) " + else + echo "" fi - [[ -n "${python_venv}" ]] && echo "${python_venv}" } function prompt_command() { - PS1="$(venv_prompt)${bold_green}➜ ${bold_cyan}\W${reset_color}$(scm_prompt_info)${normal} " + PS1="$(venv_parenthases)${bold_green}➜ ${bold_cyan}\W${reset_color}$(scm_prompt_info)${normal} " } PROMPT_COMMAND=prompt_command From bb28d5ffbb3547298296e18b6985fd3cef1cfe7d Mon Sep 17 00:00:00 2001 From: Petar Nikolovski Date: Wed, 26 Jan 2022 22:41:31 +0100 Subject: [PATCH 3/4] Remove unnecessary else clause --- themes/robbyrussell/robbyrussell.theme.bash | 2 -- 1 file changed, 2 deletions(-) diff --git a/themes/robbyrussell/robbyrussell.theme.bash b/themes/robbyrussell/robbyrussell.theme.bash index 23fc885d55..49afb5bdea 100644 --- a/themes/robbyrussell/robbyrussell.theme.bash +++ b/themes/robbyrussell/robbyrussell.theme.bash @@ -21,8 +21,6 @@ function git_prompt_info() { function venv_parenthases() { if [[ $(venv_prompt) != "" ]]; then echo "($(venv_prompt)) " - else - echo "" fi } From 11eb0c126a774a9589b70ff68d4b8e3717c7a10d Mon Sep 17 00:00:00 2001 From: Petar Nikolovski Date: Thu, 27 Jan 2022 20:56:38 +0100 Subject: [PATCH 4/4] Add conda_or_venv_prompt function --- themes/base.theme.bash | 6 +++--- themes/robbyrussell/robbyrussell.theme.bash | 11 ++++------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/themes/base.theme.bash b/themes/base.theme.bash index 6493637539..cb7e8570a7 100644 --- a/themes/base.theme.bash +++ b/themes/base.theme.bash @@ -626,13 +626,13 @@ function _save-and-reload-history() { [[ $autosave -eq 1 ]] && history -a && history -c && history -r } -function venv_prompt() { +function conda_or_venv_prompt() { local python_venv="" if [[ -n "${CONDA_DEFAULT_ENV:-}" ]]; then - python_venv="${CONDA_DEFAULT_ENV}" + python_venv=$(condaenv_prompt) PYTHON_VENV_CHAR=${CONDA_PYTHON_VENV_CHAR} elif [[ -n "${VIRTUAL_ENV:-}" ]]; then - python_venv="${VIRTUAL_ENV##*/}" + python_venv=$(virtualenv_prompt) fi [[ -n "${python_venv}" ]] && echo "${PYTHON_VENV_CHAR}${python_venv}" } diff --git a/themes/robbyrussell/robbyrussell.theme.bash b/themes/robbyrussell/robbyrussell.theme.bash index 49afb5bdea..5edd0e67f3 100644 --- a/themes/robbyrussell/robbyrussell.theme.bash +++ b/themes/robbyrussell/robbyrussell.theme.bash @@ -13,19 +13,16 @@ GIT_THEME_PROMPT_SUFFIX="${bold_blue})" RVM_THEME_PROMPT_PREFIX="|" RVM_THEME_PROMPT_SUFFIX="|" +VIRTUALENV_THEME_PROMPT_PREFIX='(' +VIRTUALENV_THEME_PROMPT_SUFFIX=') ' + function git_prompt_info() { git_prompt_vars echo -e "$SCM_PREFIX${bold_red}$SCM_BRANCH$SCM_STATE$SCM_SUFFIX" } -function venv_parenthases() { - if [[ $(venv_prompt) != "" ]]; then - echo "($(venv_prompt)) " - fi -} - function prompt_command() { - PS1="$(venv_parenthases)${bold_green}➜ ${bold_cyan}\W${reset_color}$(scm_prompt_info)${normal} " + PS1="$(conda_or_venv_prompt)${bold_green}➜ ${bold_cyan}\W${reset_color}$(scm_prompt_info)${normal} " } PROMPT_COMMAND=prompt_command