shell-proxy.plugin.zsh 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/bash
  2. # shellcheck disable=SC1090,SC2154
  3. # Handle $0 according to the standard:
  4. # https://zdharma-continuum.github.io/Zsh-100-Commits-Club/Zsh-Plugin-Standard.html
  5. 0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
  6. 0="${${(M)0:#/*}:-$PWD/$0}"
  7. eval '
  8. proxy() {
  9. # deprecate $DEFAULT_PROXY, use SHELLPROXY_URL instead
  10. if [[ -n "$DEFAULT_PROXY" && -z "$SHELLPROXY_URL" ]]; then
  11. echo >&2 "proxy: DEFAULT_PROXY is deprecated, use SHELLPROXY_URL instead"
  12. SHELLPROXY_URL="$DEFAULT_PROXY"
  13. unset DEFAULT_PROXY
  14. fi
  15. # deprecate CONFIG_PROXY, use SHELLPROXY_CONFIG instead
  16. if [[ -n "$CONFIG_PROXY" && -z "$SHELLPROXY_CONFIG" ]]; then
  17. echo >&2 "proxy: CONFIG_PROXY is deprecated, use SHELLPROXY_CONFIG instead"
  18. SHELLPROXY_CONFIG="$CONFIG_PROXY"
  19. unset CONFIG_PROXY
  20. fi
  21. # the proxy.py script is in the same directory as this function
  22. local proxy="'"${0:h}"'/proxy.py"
  23. # capture the output of the proxy script and bail out if it fails
  24. local output
  25. output="$(SHELLPROXY_URL="$SHELLPROXY_URL" SHELLPROXY_CONFIG="$SHELLPROXY_CONFIG" "$proxy" "$1")" ||
  26. return $?
  27. # evaluate the output generated by the proxy script
  28. source <(echo "$output")
  29. }
  30. '
  31. _proxy() {
  32. local -r commands=('enable' 'disable' 'status')
  33. compset -P '*,'
  34. compadd -S '' "${commands[@]}"
  35. }
  36. compdef _proxy proxy