From 93b536863f9d4c18c04c33e96f97b5e06b01975f Mon Sep 17 00:00:00 2001 From: Yamashita Yuu Date: Thu, 15 Aug 2013 21:27:52 +0900 Subject: [PATCH] Support fish shell --- completions/pyenv.fish | 5 ++++ libexec/pyenv-commands | 23 +++++++++++------ libexec/pyenv-completions | 2 +- libexec/pyenv-fish-rehash | 14 +++++++++++ libexec/pyenv-fish-shell | 52 +++++++++++++++++++++++++++++++++++++++ libexec/pyenv-help | 2 +- libexec/pyenv-init | 44 ++++++++++++++++++++++++++++++--- 7 files changed, 128 insertions(+), 14 deletions(-) create mode 100644 completions/pyenv.fish create mode 100755 libexec/pyenv-fish-rehash create mode 100755 libexec/pyenv-fish-shell diff --git a/completions/pyenv.fish b/completions/pyenv.fish new file mode 100644 index 00000000..a1675bae --- /dev/null +++ b/completions/pyenv.fish @@ -0,0 +1,5 @@ +function __fish_pyenv + pyenv commands +end + +complete -f -c pyenv -a '(__fish_pyenv)' diff --git a/libexec/pyenv-commands b/libexec/pyenv-commands index 14e8e6af..70f9bcfa 100755 --- a/libexec/pyenv-commands +++ b/libexec/pyenv-commands @@ -8,14 +8,15 @@ set -e # Provide pyenv completions if [ "$1" = "--complete" ]; then echo --sh + echo --fish echo --no-sh exit fi -if [ "$1" = "--sh" ]; then - sh=1 +if [ "$1" = "--sh" ] || [ "$1" = "--fish" ]; then + sh="${1##--}-" shift -elif [ "$1" = "--no-sh" ]; then +elif [ "$1" = "--no-sh" ] || [ "$1" = "--no-fish" ]; then nosh=1 shift fi @@ -26,15 +27,21 @@ shopt -s nullglob for command in "${path}/pyenv-"*; do command="${command##*pyenv-}" if [ -n "$sh" ]; then - if [ ${command:0:3} = "sh-" ]; then - echo ${command##sh-} + if [[ "${command}" == "${sh}"* ]]; then + echo "${command##${sh}}" fi elif [ -n "$nosh" ]; then - if [ ${command:0:3} != "sh-" ]; then - echo ${command##sh-} + if [ "${command:0:3}" != "sh-" ] && [ "${command:0:5}" != "fish-" ]; then + echo "$command" fi else - echo ${command##sh-} + if [ "${command:0:3}" = "sh-" ]; then + echo "${command##sh-}" + elif [ "${command:0:5}" = "fish-" ]; then + echo "${command##fish-}" + else + echo "${command}" + fi fi done done diff --git a/libexec/pyenv-completions b/libexec/pyenv-completions index c6785caa..385abcc7 100755 --- a/libexec/pyenv-completions +++ b/libexec/pyenv-completions @@ -10,7 +10,7 @@ if [ -z "$COMMAND" ]; then exit 1 fi -COMMAND_PATH="$(command -v "pyenv-$COMMAND" || command -v "pyenv-sh-$COMMAND")" +COMMAND_PATH="$(command -v "pyenv-$COMMAND" || command -v "pyenv-sh-$COMMAND" || command -v "pyenv-fish-$COMMAND")" if grep -i "^# provide pyenv completions" "$COMMAND_PATH" >/dev/null; then shift exec "$COMMAND_PATH" --complete "$@" diff --git a/libexec/pyenv-fish-rehash b/libexec/pyenv-fish-rehash new file mode 100755 index 00000000..0f8b0043 --- /dev/null +++ b/libexec/pyenv-fish-rehash @@ -0,0 +1,14 @@ +#!/usr/bin/env bash +set -e +[ -n "$PYENV_DEBUG" ] && set -x + +# Provide pyenv completions +if [ "$1" = "--complete" ]; then + exec pyenv-rehash --complete +fi + +# When pyenv shell integration is enabled, delegate to pyenv-rehash, +# then tell the shell to empty its command lookup cache. +pyenv-rehash +# FIXME: support fish +#echo "hash -r" diff --git a/libexec/pyenv-fish-shell b/libexec/pyenv-fish-shell new file mode 100755 index 00000000..6c4376d4 --- /dev/null +++ b/libexec/pyenv-fish-shell @@ -0,0 +1,52 @@ +#!/usr/bin/env bash +# +# Summary: Set or show the shell-specific Python version +# +# Usage: pyenv shell +# pyenv shell --unset +# +# Sets a shell-specific Python version by setting the `PYENV_VERSION' +# environment variable in your shell. This version overrides local +# application-specific versions and the global version. +# +# should be a string matching a Python version known to pyenv. +# The special version string `system' will use your default system Python. +# Run `pyenv versions' for a list of available Python versions. + +set -e +[ -n "$PYENV_DEBUG" ] && set -x + +# Provide pyenv completions +if [ "$1" = "--complete" ]; then + echo --unset + echo system + exec pyenv-versions --bare +fi + +versions=("$@") + +if [ -z "$versions" ]; then + if [ -z "$PYENV_VERSION" ]; then + echo "pyenv: no shell-specific version configured" >&2 + exit 1 + else + echo "echo \"\$PYENV_VERSION\"" + exit + fi +fi + +if [ "$versions" = "--unset" ]; then + echo "set -e PYENV_VERSION" + exit +fi + +# Make sure the specified version is installed. +if pyenv-prefix "${versions[@]}" >/dev/null; then + OLDIFS="$IFS" + IFS=: PYENV_VERSION="${versions[*]}" + IFS="OLDIFS" + echo "setenv PYENV_VERSION \"${PYENV_VERSION}\"" +else + echo "false" + exit 1 +fi diff --git a/libexec/pyenv-help b/libexec/pyenv-help index d6db968d..abce46ca 100755 --- a/libexec/pyenv-help +++ b/libexec/pyenv-help @@ -17,7 +17,7 @@ set -e command_path() { local command="$1" - command -v pyenv-"$command" || command -v pyenv-sh-"$command" || true + command -v pyenv-"$command" || command -v pyenv-sh-"$command" || command -v pyenv-fish-"$command" || true } extract_initial_comment_block() { diff --git a/libexec/pyenv-init b/libexec/pyenv-init index fd2e2c2f..44eb4cfa 100755 --- a/libexec/pyenv-init +++ b/libexec/pyenv-init @@ -56,6 +56,9 @@ if [ -z "$print" ]; then ksh ) profile='~/.profile' ;; + fish ) + profile='~/.config/fish/config.fish' + ;; * ) profile='your profile' ;; @@ -73,21 +76,52 @@ fi mkdir -p "${PYENV_ROOT}/"{shims,versions} -echo 'export PATH="'${PYENV_ROOT}'/shims:${PATH}"' +case "$shell" in +fish ) + echo 'setenv PATH "'${PYENV_ROOT}'/shims"' '$PATH' ';' + ;; +* ) + echo 'export PATH="'${PYENV_ROOT}'/shims:${PATH}"' + ;; +esac case "$shell" in bash | zsh ) echo "source \"$root/completions/pyenv.${shell}\"" ;; +fish ) + echo ". \"$root/completions/pyenv.${shell}\";" + ;; esac if [ -z "$no_rehash" ]; then echo 'pyenv rehash 2>/dev/null' fi -commands=(`pyenv-commands --sh`) -IFS="|" -cat <