__init__.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from __future__ import annotations
  2. from typing import TYPE_CHECKING
  3. from virtualenv.activation.via_template import ViaTemplateActivator
  4. if TYPE_CHECKING:
  5. from collections.abc import Iterator
  6. from pathlib import Path
  7. from virtualenv.create.creator import Creator
  8. class NushellActivator(ViaTemplateActivator):
  9. def templates(self) -> Iterator[str]:
  10. yield "activate.nu"
  11. @staticmethod
  12. def quote(string: str) -> str:
  13. """Nushell supports raw strings like: r###'this is a string'###.
  14. https://github.com/nushell/nushell.github.io/blob/main/book/working_with_strings.md
  15. This method finds the maximum continuous sharps in the string and then quote it with an extra sharp.
  16. """
  17. max_sharps = 0
  18. current_sharps = 0
  19. for char in string:
  20. if char == "#":
  21. current_sharps += 1
  22. max_sharps = max(current_sharps, max_sharps)
  23. else:
  24. current_sharps = 0
  25. wrapping = "#" * (max_sharps + 1)
  26. return f"r{wrapping}'{string}'{wrapping}"
  27. def replacements(self, creator: Creator, dest_folder: Path) -> dict[str, str]: # noqa: ARG002
  28. return {
  29. "__VIRTUAL_PROMPT__": "" if self.flag_prompt is None else self.flag_prompt,
  30. "__VIRTUAL_ENV__": str(creator.dest),
  31. "__VIRTUAL_NAME__": creator.env_name,
  32. "__BIN_NAME__": str(creator.bin_dir.relative_to(creator.dest)),
  33. "__TCL_LIBRARY__": getattr(creator.interpreter, "tcl_lib", None) or "",
  34. "__TK_LIBRARY__": getattr(creator.interpreter, "tk_lib", None) or "",
  35. }
  36. __all__ = [
  37. "NushellActivator",
  38. ]