console.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. """Functions to communicate to the user via console."""
  2. from __future__ import annotations
  3. from typing import List, Optional
  4. from rich.console import Console
  5. from rich.prompt import Prompt
  6. from rich.status import Status
  7. # Console for pretty printing.
  8. _console = Console()
  9. def deprecate(msg: str) -> None:
  10. """Print a deprecation warning.
  11. Args:
  12. msg: The deprecation message.
  13. """
  14. _console.print(f"[yellow]DeprecationWarning: {msg}[/yellow]")
  15. def log(msg: str) -> None:
  16. """Takes a string and logs it to the console.
  17. Args:
  18. msg (str): The message to log.
  19. """
  20. _console.log(msg)
  21. def print(msg: str) -> None:
  22. """Prints the given message to the console.
  23. Args:
  24. msg (str): The message to print to the console.
  25. """
  26. _console.print(msg)
  27. def rule(title: str) -> None:
  28. """Prints a horizontal rule with a title.
  29. Args:
  30. title (str): The title of the rule.
  31. """
  32. _console.rule(title)
  33. def ask(
  34. question: str, choices: Optional[List[str]] = None, default: Optional[str] = None
  35. ) -> str:
  36. """Takes a prompt question and optionally a list of choices
  37. and returns the user input.
  38. Args:
  39. question (str): The question to ask the user.
  40. choices (Optional[List[str]]): A list of choices to select from.
  41. default(Optional[str]): The default option selected.
  42. Returns:
  43. A string
  44. """
  45. return Prompt.ask(question, choices=choices, default=default) # type: ignore
  46. def status(msg: str) -> Status:
  47. """Returns a status,
  48. which can be used as a context manager.
  49. Args:
  50. msg (str): The message to be used as status title.
  51. Returns:
  52. The status of the console.
  53. """
  54. return _console.status(msg)