console.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 warn(msg: str) -> None:
  16. """Print a warning about bad usage in Reflex.
  17. Args:
  18. msg: The warning message.
  19. """
  20. _console.print(f"[orange1]UsageWarning: {msg}[/orange1]")
  21. def log(msg: str) -> None:
  22. """Takes a string and logs it to the console.
  23. Args:
  24. msg: The message to log.
  25. """
  26. _console.log(msg)
  27. def print(msg: str) -> None:
  28. """Prints the given message to the console.
  29. Args:
  30. msg: The message to print to the console.
  31. """
  32. _console.print(msg)
  33. def rule(title: str) -> None:
  34. """Prints a horizontal rule with a title.
  35. Args:
  36. title: The title of the rule.
  37. """
  38. _console.rule(title)
  39. def ask(
  40. question: str, choices: Optional[List[str]] = None, default: Optional[str] = None
  41. ) -> str:
  42. """Takes a prompt question and optionally a list of choices
  43. and returns the user input.
  44. Args:
  45. question: The question to ask the user.
  46. choices: A list of choices to select from.
  47. default: The default option selected.
  48. Returns:
  49. A string
  50. """
  51. return Prompt.ask(question, choices=choices, default=default) # type: ignore
  52. def status(msg: str) -> Status:
  53. """Returns a status,
  54. which can be used as a context manager.
  55. Args:
  56. msg: The message to be used as status title.
  57. Returns:
  58. The status of the console.
  59. """
  60. return _console.status(msg)