Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autocompletions for Python installs via Pyton.org Mac installer #222

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 36 additions & 3 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,43 @@ using Python interpreters in a few known situations, in the following order:
1. asdf_ Python plugin is installed and has built the specified Python version.
2. Pyenv_ is installed and has built the specified Python version.
3. Pythonz_ is installed and has built the specified Python version.
4. Homebrew_ keg-only versioned Python executable (e.g., 3.8) found at:
``/usr/local/opt/python@3.8/bin/python3.8``
4. Python.org_ Mac installation of specified Python version (e.g., 3.10) found
at: ``/Library/Frameworks/Python.framework/Versions``.
5. Homebrew_ keg-only versioned Python executable (e.g., 3.8) found at:
``/usr/local/opt/python@3.8/bin/python3.8``.

For asdf_, Pyenv_, and Pythonz_ , in addition to passing option flags such as
``-p python3.8`` or ``-p python3.9.0a4``, you can even get away with specifying
just the version numbers, such as ``-p 3.8`` or ``-p 3.9.0a4``.
just the version numbers, such as ``-p 3.8`` or ``-p 3.9.0a4``. Python.org_
versions should be specified with Major.Minor version numbers, such as
``-p 3.10``.

If you would like to get autocompletions for ``vf new -p`` and ``vf upgrade -p``
with Python versions installed via asdf_, pyenv_, or Python.org_'s Mac installer,
execute the following interactively. For asdf_::

set -Ux VIRTUALFISH_PYVERSION_COMPLETION "asdf"

For pyenv_::

set -Ux VIRTUALFISH_PYVERSION_COMPLETION "pyenv"

For Python.org_ installations::

set -Ux VIRTUALFISH_PYVERSION_COMPLETION "pyorg"

After reloading your shell (e.g. via ``exec fish``) numbers of Python versions
installed via asdf_, pyenv_, or Python.org_ will be autocompleted. Because this
is using fish's `universal variables`_, you only need to execute this once and
there is no need to add this line to your ``config.fish`` file. However, if you
would like these autocompletions set up automatically on a new machine, you
could add the following to your config (adjust by picking either asdf_, pyenv_
or pyorg)::

if test -z "$VIRTUALFISH_PYVERSION_COMPLETION"
set -Ux VIRTUALFISH_PYVERSION_COMPLETION "asdf"/"pyenv"/"pyorg"
exec fish
end

.. _configuration_variables:

Expand Down Expand Up @@ -149,3 +180,5 @@ you want those changes to take effect for the current shell session.
.. _asdf: https://asdf-vm.com/
.. _Pyenv: https://github.com/pyenv/pyenv
.. _Pythonz: https://github.com/saghul/pythonz
.. _Python.org: https://www.python.org/downloads/macos/
.. _universal variables: https://fishshell.com/docs/current/tutorial.html#universal-variables
80 changes: 56 additions & 24 deletions virtualfish/virtual.fish
Original file line number Diff line number Diff line change
Expand Up @@ -134,36 +134,38 @@ function __vfsupport_find_python --description "Search for and return Python pat
set -l python
set -l python_arg $argv[1]
set -l py_version (string replace "python" "" $python_arg)
set -l pyorg_path "/Library/Frameworks/Python.framework/Versions/$py_version/bin/python$py_version"
set -l brew_path "/usr/local/opt/python@$py_version/bin/python$py_version"
set -l asdf_path
if begin; type -q "asdf"; and contains "python" (asdf plugin list); end
set asdf_path (asdf where python $py_version)/bin/python
end
set -l pyenv_path
if type -q "pyenv"
set pyenv_path (pyenv root)/versions/"$py_version"/bin/python
end
set -l pythonz_path
if type -q "pythonz"
set pythonz_path (pythonz locate $py_version)
end

# Executable on PATH (python3/python3.8) or full interpreter path
if set -l py_path (command -s $python_arg)
set python "$py_path"
# Use `asdf` Python plugin, if found and provided version is available
else if type -q "asdf"
set -l asdf_plugins (asdf plugin list)
if contains python $asdf_plugins
set -l asdf_path (asdf where python $py_version)/bin/python
if command -q "$asdf_path"
set python "$asdf_path"
end
end
# Use Pyenv, if found and provided version is available
else if type -q "pyenv"
if test -n "$PYENV_ROOT"
set pyenv_path "$PYENV_ROOT"/versions/"$py_version"/bin/python
else
# If $PYENV_ROOT hasn't been set, assume versions are stored in ~/.pyenv
set pyenv_path "$HOME"/.pyenv/versions/"$py_version"/bin/python
end
if command -q "$pyenv_path"
set python "$pyenv_path"
end
else if command -q "$asdf_path"
set python "$asdf_path"
# Use pyenv, if found and provided version is available
else if command -q "$pyenv_path"
set python "$pyenv_path"
# Use Pythonz, if found and provided version is available
else if type -q "pythonz"
set -l pythonz_path (pythonz locate $py_version)
if command -q "$pythonz_path"
set python "$pythonz_path"
end
else if command -q "$pythonz_path"
set python "$pythonz_path"
# Use Python versions from the Python.org installer. Note: This only
# works when Major.Minor version numbers were provided (e.g. 3.10),
# Major.Minor.Micro will fail (e.g. 3.10.3)
else if command -q "$pyorg_path"
set python "$pyorg_path"
# Version number in Homebrew keg-only versioned Python formula
else if command -q "$brew_path"
set python "$brew_path"
Expand Down Expand Up @@ -793,6 +795,20 @@ function __vfsupport_setup_autocomplete --on-event virtualfish_did_setup_plugins
return 1
end

function __vfcompletion_pyorg_versions
# Optional autocompletions for versions installed with Python.org
# Mac installer. Used for `vf new -p` and `vf upgrade -p` if
# VIRTUALFISH_PYVERSION_COMPLETION set to 'pyorg'
set -l pyorg_dir /Library/Frameworks/Python.framework/Versions
for path in $pyorg_dir/*
set py_version (basename $path)
# Using regex to filter out directories named as Major.Minor Python versions (e.g. 3.10)
if string match -q -r '^[0-9][.][0-9]{1,2}$' $py_version
echo $py_version
end
end
end

# add completion for subcommands
for sc in (functions -a | sed -n '/__vf_/{s///g;p;}')
set -l helptext (functions "__vf_$sc" | grep -m1 "^function" | sed -E "s|.*'(.*)'.*|\1|")
Expand All @@ -803,6 +819,22 @@ function __vfsupport_setup_autocomplete --on-event virtualfish_did_setup_plugins
complete -x -c vf -n '__vfcompletion_using_command connect' -a "(vf ls)"
complete -x -c vf -n '__vfcompletion_using_command rm' -a "(vf ls)"
complete -x -c vf -n '__vfcompletion_using_command upgrade' -a "(vf ls)"
# Optional: Autocomplete `vf new -p` and `vf upgrade -p` with Python versions installed via
# asdf, pyenv, or Python.org's Mac installer. To use, interactively execute
# `set -Ux VIRTUALFISH_PYVERSION_COMPLETION <"asdf"/"pyenv"/"pyorg">` and reload your shell
if set -q VIRTUALFISH_PYVERSION_COMPLETION
if test $VIRTUALFISH_PYVERSION_COMPLETION = "asdf"
complete -x -c vf -n '__vfcompletion_using_command new' -s p -l python -a "(asdf list python 2> /dev/null | sed -e 's/^[[:space:]]*//')"
complete -x -c vf -n '__vfcompletion_using_command upgrade' -s p -l python -a "(asdf list python 2> /dev/null | sed -e 's/^[[:space:]]*//')"
else if test $VIRTUALFISH_PYVERSION_COMPLETION = "pyenv"
complete -x -c vf -n '__vfcompletion_using_command new' -s p -l python -a "(pyenv versions --bare)"
complete -x -c vf -n '__vfcompletion_using_command upgrade' -s p -l python -a "(pyenv versions --bare)"
else if test $VIRTUALFISH_PYVERSION_COMPLETION = "pyorg"
complete -x -c vf -n '__vfcompletion_using_command new' -s p -l python -a "(__vfcompletion_pyorg_versions)"
complete -x -c vf -n '__vfcompletion_using_command upgrade' -s p -l python -a "(__vfcompletion_pyorg_versions)"
end
end

end

function __vfsupport_get_default_python --description "Return Python interpreter defined in variables, if any"
Expand Down