diff --git a/modules/prompt/functions/prompt_steeef_setup b/modules/prompt/functions/prompt_steeef_setup index c3d6a88c..5cde90fd 100644 --- a/modules/prompt/functions/prompt_steeef_setup +++ b/modules/prompt/functions/prompt_steeef_setup @@ -12,12 +12,6 @@ # http://i.imgur.com/HyRvv.png # -function virtualenv_info { - if [[ -n "$VIRTUAL_ENV" ]]; then - print "(${VIRTUAL_ENV:t}) " - fi -} - function prompt_steeef_precmd { # Check for untracked files or updated submodules since vcs_info does not. if [[ -n $(git ls-files --other --exclude-standard 2> /dev/null) ]]; then @@ -29,6 +23,10 @@ function prompt_steeef_precmd { zstyle ':vcs_info:*:prompt:*' formats "${branch_format}" vcs_info 'prompt' + + if (( $+functions[python-info] )); then + python-info + fi } function prompt_steeef_setup { @@ -83,10 +81,13 @@ function prompt_steeef_setup { zstyle ':vcs_info:*:prompt:*' formats "${branch_format}" zstyle ':vcs_info:*:prompt:*' nvcsformats "" + # Set python-info parameters. + zstyle ':omz:module:python' virtualenv '(%v)' + # Define prompts. PROMPT=" ${_prompt_steeef_colors[3]}%n%f at ${_prompt_steeef_colors[2]}%m%f in ${_prompt_steeef_colors[5]}%~%f "'${vcs_info_msg_0_}'" -"'$(virtualenv_info)'"$ " +"'$python_info[virtualenv]'"$ " } prompt_steeef_setup "$@" diff --git a/modules/python/README.md b/modules/python/README.md index d17c4ef1..2363f87d 100644 --- a/modules/python/README.md +++ b/modules/python/README.md @@ -44,6 +44,17 @@ convenient shell functions to create, switch, and manage them. Install virtualenvwrapper and set [`$WORKON_HOME`][4] to the path where virtual environments will be stored. +### Theming + +To display the name of the current virtual enviroment in a prompt, define the +following style in the `prompt_theme_setup` function. + + # %v - virtualenv name. + zstyle ':omz:module:python' virtualenv 'virtualenv:%v' + +Then add `$python_info[virtualenv]` to `$PROMPT` or `$RPROMPT` and call +`python-info` in the `prompt_theme_preexec` hook function. + Aliases ------- @@ -59,6 +70,12 @@ Aliases - `pyzu` updates itself to the latest version. - `pyzx` uninstalls Python versions. +Functions +--------- + + - `python-info` exposes information about the Python environment via the + `$python_info` associative array. + Authors ------- diff --git a/modules/python/functions/python-info b/modules/python/functions/python-info new file mode 100644 index 00000000..0bca3c10 --- /dev/null +++ b/modules/python/functions/python-info @@ -0,0 +1,21 @@ +# +# Displays Python information. +# +# Authors: +# Sorin Ionescu +# + +local virtualenv_format +local virtualenv_formatted + +# Clean up previous $python_info. +unset python_info +typeset -gA python_info + +# Format virtualenv. +if [[ -n "$VIRTUAL_ENV" ]]; then + zstyle -s ':omz:module:python' virtualenv 'virtualenv_format' + zformat -f virtualenv_formatted "$virtualenv_format" "v:${VIRTUAL_ENV:t}" + python_info[virtualenv]="$virtualenv_formatted" +fi + diff --git a/modules/python/init.zsh b/modules/python/init.zsh index 5a922167..60d7e2e3 100644 --- a/modules/python/init.zsh +++ b/modules/python/init.zsh @@ -24,6 +24,8 @@ fi # Load virtualenvwrapper into the shell session. if [[ -n "$WORKON_HOME" ]] && (( $+commands[virtualenvwrapper.sh] )); then + # Disable the virtualenv prompt. + VIRTUAL_ENV_DISABLE_PROMPT=1 source "$commands[virtualenvwrapper.sh]" fi