status.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. from types import TracebackType
  2. from typing import Optional, Type
  3. from .console import Console, RenderableType
  4. from .jupyter import JupyterMixin
  5. from .live import Live
  6. from .spinner import Spinner
  7. from .style import StyleType
  8. class Status(JupyterMixin):
  9. """Displays a status indicator with a 'spinner' animation.
  10. Args:
  11. status (RenderableType): A status renderable (str or Text typically).
  12. console (Console, optional): Console instance to use, or None for global console. Defaults to None.
  13. spinner (str, optional): Name of spinner animation (see python -m rich.spinner). Defaults to "dots".
  14. spinner_style (StyleType, optional): Style of spinner. Defaults to "status.spinner".
  15. speed (float, optional): Speed factor for spinner animation. Defaults to 1.0.
  16. refresh_per_second (float, optional): Number of refreshes per second. Defaults to 12.5.
  17. """
  18. def __init__(
  19. self,
  20. status: RenderableType,
  21. *,
  22. console: Optional[Console] = None,
  23. spinner: str = "dots",
  24. spinner_style: StyleType = "status.spinner",
  25. speed: float = 1.0,
  26. refresh_per_second: float = 12.5,
  27. ):
  28. self.status = status
  29. self.spinner_style = spinner_style
  30. self.speed = speed
  31. self._spinner = Spinner(spinner, text=status, style=spinner_style, speed=speed)
  32. self._live = Live(
  33. self.renderable,
  34. console=console,
  35. refresh_per_second=refresh_per_second,
  36. transient=True,
  37. )
  38. @property
  39. def renderable(self) -> Spinner:
  40. return self._spinner
  41. @property
  42. def console(self) -> "Console":
  43. """Get the Console used by the Status objects."""
  44. return self._live.console
  45. def update(
  46. self,
  47. status: Optional[RenderableType] = None,
  48. *,
  49. spinner: Optional[str] = None,
  50. spinner_style: Optional[StyleType] = None,
  51. speed: Optional[float] = None,
  52. ) -> None:
  53. """Update status.
  54. Args:
  55. status (Optional[RenderableType], optional): New status renderable or None for no change. Defaults to None.
  56. spinner (Optional[str], optional): New spinner or None for no change. Defaults to None.
  57. spinner_style (Optional[StyleType], optional): New spinner style or None for no change. Defaults to None.
  58. speed (Optional[float], optional): Speed factor for spinner animation or None for no change. Defaults to None.
  59. """
  60. if status is not None:
  61. self.status = status
  62. if spinner_style is not None:
  63. self.spinner_style = spinner_style
  64. if speed is not None:
  65. self.speed = speed
  66. if spinner is not None:
  67. self._spinner = Spinner(
  68. spinner, text=self.status, style=self.spinner_style, speed=self.speed
  69. )
  70. self._live.update(self.renderable, refresh=True)
  71. else:
  72. self._spinner.update(
  73. text=self.status, style=self.spinner_style, speed=self.speed
  74. )
  75. def start(self) -> None:
  76. """Start the status animation."""
  77. self._live.start()
  78. def stop(self) -> None:
  79. """Stop the spinner animation."""
  80. self._live.stop()
  81. def __rich__(self) -> RenderableType:
  82. return self.renderable
  83. def __enter__(self) -> "Status":
  84. self.start()
  85. return self
  86. def __exit__(
  87. self,
  88. exc_type: Optional[Type[BaseException]],
  89. exc_val: Optional[BaseException],
  90. exc_tb: Optional[TracebackType],
  91. ) -> None:
  92. self.stop()
  93. if __name__ == "__main__": # pragma: no cover
  94. from time import sleep
  95. from .console import Console
  96. console = Console()
  97. with console.status("[magenta]Covid detector booting up") as status:
  98. sleep(3)
  99. console.log("Importing advanced AI")
  100. sleep(3)
  101. console.log("Advanced Covid AI Ready")
  102. sleep(3)
  103. status.update(status="[bold blue] Scanning for Covid", spinner="earth")
  104. sleep(3)
  105. console.log("Found 10,000,000,000 copies of Covid32.exe")
  106. sleep(3)
  107. status.update(
  108. status="[bold red]Moving Covid32.exe to Trash",
  109. spinner="bouncingBall",
  110. spinner_style="yellow",
  111. )
  112. sleep(5)
  113. console.print("[bold green]Covid deleted successfully")