activate.fish 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # This file must be used using `source bin/activate.fish` *within a running fish ( http://fishshell.com ) session*.
  2. # Do not run it directly.
  3. function _bashify_path -d "Converts a fish path to something bash can recognize"
  4. set fishy_path $argv
  5. set bashy_path $fishy_path[1]
  6. for path_part in $fishy_path[2..-1]
  7. set bashy_path "$bashy_path:$path_part"
  8. end
  9. echo $bashy_path
  10. end
  11. function _fishify_path -d "Converts a bash path to something fish can recognize"
  12. echo $argv | tr ':' '\n'
  13. end
  14. function deactivate -d 'Exit virtualenv mode and return to the normal environment.'
  15. # reset old environment variables
  16. if test -n "$_OLD_VIRTUAL_PATH"
  17. # https://github.com/fish-shell/fish-shell/issues/436 altered PATH handling
  18. if test (string sub -s 1 -l 1 $FISH_VERSION) -lt 3
  19. set -gx PATH (_fishify_path "$_OLD_VIRTUAL_PATH")
  20. else
  21. set -gx PATH $_OLD_VIRTUAL_PATH
  22. end
  23. set -e _OLD_VIRTUAL_PATH
  24. end
  25. if test -n __TCL_LIBRARY__
  26. if test -n "$_OLD_VIRTUAL_TCL_LIBRARY";
  27. set -gx TCL_LIBRARY "$_OLD_VIRTUAL_TCL_LIBRARY";
  28. set -e _OLD_VIRTUAL_TCL_LIBRARY;
  29. else;
  30. set -e TCL_LIBRARY;
  31. end
  32. end
  33. if test -n __TK_LIBRARY__
  34. if test -n "$_OLD_VIRTUAL_TK_LIBRARY";
  35. set -gx TK_LIBRARY "$_OLD_VIRTUAL_TK_LIBRARY";
  36. set -e _OLD_VIRTUAL_TK_LIBRARY;
  37. else;
  38. set -e TK_LIBRARY;
  39. end
  40. end
  41. if test -n "$_OLD_VIRTUAL_PYTHONHOME"
  42. set -gx PYTHONHOME "$_OLD_VIRTUAL_PYTHONHOME"
  43. set -e _OLD_VIRTUAL_PYTHONHOME
  44. end
  45. if test -n "$_OLD_FISH_PROMPT_OVERRIDE"
  46. and functions -q _old_fish_prompt
  47. # Set an empty local `$fish_function_path` to allow the removal of `fish_prompt` using `functions -e`.
  48. set -l fish_function_path
  49. # Erase virtualenv's `fish_prompt` and restore the original.
  50. functions -e fish_prompt
  51. functions -c _old_fish_prompt fish_prompt
  52. functions -e _old_fish_prompt
  53. set -e _OLD_FISH_PROMPT_OVERRIDE
  54. end
  55. set -e VIRTUAL_ENV
  56. set -e VIRTUAL_ENV_PROMPT
  57. if test "$argv[1]" != 'nondestructive'
  58. # Self-destruct!
  59. functions -e pydoc
  60. functions -e deactivate
  61. functions -e _bashify_path
  62. functions -e _fishify_path
  63. end
  64. end
  65. # Unset irrelevant variables.
  66. deactivate nondestructive
  67. set -gx VIRTUAL_ENV __VIRTUAL_ENV__
  68. # https://github.com/fish-shell/fish-shell/issues/436 altered PATH handling
  69. if test (string sub -s 1 -l 1 $FISH_VERSION) -lt 3
  70. set -gx _OLD_VIRTUAL_PATH (_bashify_path $PATH)
  71. else
  72. set -gx _OLD_VIRTUAL_PATH $PATH
  73. end
  74. set -gx PATH "$VIRTUAL_ENV"'/'__BIN_NAME__ $PATH
  75. if test -n __TCL_LIBRARY__
  76. if set -q TCL_LIBRARY;
  77. set -gx _OLD_VIRTUAL_TCL_LIBRARY $TCL_LIBRARY;
  78. end
  79. set -gx TCL_LIBRARY '__TCL_LIBRARY__'
  80. end
  81. if test -n __TK_LIBRARY__
  82. if set -q TK_LIBRARY;
  83. set -gx _OLD_VIRTUAL_TK_LIBRARY $TK_LIBRARY;
  84. end
  85. set -gx TK_LIBRARY '__TK_LIBRARY__'
  86. end
  87. # Prompt override provided?
  88. # If not, just use the environment name.
  89. if test -n __VIRTUAL_PROMPT__
  90. set -gx VIRTUAL_ENV_PROMPT __VIRTUAL_PROMPT__
  91. else
  92. set -gx VIRTUAL_ENV_PROMPT (basename "$VIRTUAL_ENV")
  93. end
  94. # Unset `$PYTHONHOME` if set.
  95. if set -q PYTHONHOME
  96. set -gx _OLD_VIRTUAL_PYTHONHOME $PYTHONHOME
  97. set -e PYTHONHOME
  98. end
  99. function pydoc
  100. python -m pydoc $argv
  101. end
  102. if test -z "$VIRTUAL_ENV_DISABLE_PROMPT"
  103. # Copy the current `fish_prompt` function as `_old_fish_prompt`.
  104. functions -c fish_prompt _old_fish_prompt
  105. function fish_prompt
  106. # Run the user's prompt first; it might depend on (pipe)status.
  107. set -l prompt (_old_fish_prompt)
  108. printf '(%s) ' $VIRTUAL_ENV_PROMPT
  109. string join -- \n $prompt # handle multi-line prompts
  110. end
  111. set -gx _OLD_FISH_PROMPT_OVERRIDE "$VIRTUAL_ENV"
  112. end