Skip to content

Commit

Permalink
Support fish shell
Browse files Browse the repository at this point in the history
  • Loading branch information
Yamashita Yuu committed Aug 15, 2013
1 parent 403f4eb commit 93b5368
Showing 7 changed files with 128 additions and 14 deletions.
5 changes: 5 additions & 0 deletions completions/pyenv.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function __fish_pyenv
pyenv commands
end

complete -f -c pyenv -a '(__fish_pyenv)'
23 changes: 15 additions & 8 deletions libexec/pyenv-commands
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion libexec/pyenv-completions
Original file line number Diff line number Diff line change
@@ -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 "$@"
14 changes: 14 additions & 0 deletions libexec/pyenv-fish-rehash
Original file line number Diff line number Diff line change
@@ -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"
52 changes: 52 additions & 0 deletions libexec/pyenv-fish-shell
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env bash
#
# Summary: Set or show the shell-specific Python version
#
# Usage: pyenv shell <version>
# 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.
#
# <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
2 changes: 1 addition & 1 deletion libexec/pyenv-help
Original file line number Diff line number Diff line change
@@ -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() {
44 changes: 40 additions & 4 deletions libexec/pyenv-init
Original file line number Diff line number Diff line change
@@ -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 <<EOS
case "$shell" in
fish )
commands=(`pyenv-commands --fish`)
cat <<EOS
;function pyenv;
set -e command;
set command \$argv[1];
if [ (count \$argv) -gt 0 ];
set -e argv[1];
end;
switch "\$command";
case ${commands[*]};
eval (pyenv "fish-\$command" \$argv);
case '*';
command pyenv "\$command" \$argv;
end;
end;
EOS
;;
* )
commands=(`pyenv-commands --sh`)
IFS="|"
cat <<EOS
pyenv() {
typeset command
command="\$1"
@@ -103,3 +137,5 @@ pyenv() {
esac
}
EOS
;;
esac

0 comments on commit 93b5368

Please sign in to comment.