measure.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. from operator import itemgetter
  2. from typing import Callable, Iterable, NamedTuple, Optional, TYPE_CHECKING
  3. from . import errors
  4. from .protocol import is_renderable, rich_cast
  5. if TYPE_CHECKING:
  6. from .console import Console, ConsoleOptions, RenderableType
  7. class Measurement(NamedTuple):
  8. """Stores the minimum and maximum widths (in characters) required to render an object."""
  9. minimum: int
  10. """Minimum number of cells required to render."""
  11. maximum: int
  12. """Maximum number of cells required to render."""
  13. @property
  14. def span(self) -> int:
  15. """Get difference between maximum and minimum."""
  16. return self.maximum - self.minimum
  17. def normalize(self) -> "Measurement":
  18. """Get measurement that ensures that minimum <= maximum and minimum >= 0
  19. Returns:
  20. Measurement: A normalized measurement.
  21. """
  22. minimum, maximum = self
  23. minimum = min(max(0, minimum), maximum)
  24. return Measurement(max(0, minimum), max(0, max(minimum, maximum)))
  25. def with_maximum(self, width: int) -> "Measurement":
  26. """Get a RenderableWith where the widths are <= width.
  27. Args:
  28. width (int): Maximum desired width.
  29. Returns:
  30. Measurement: New Measurement object.
  31. """
  32. minimum, maximum = self
  33. return Measurement(min(minimum, width), min(maximum, width))
  34. def with_minimum(self, width: int) -> "Measurement":
  35. """Get a RenderableWith where the widths are >= width.
  36. Args:
  37. width (int): Minimum desired width.
  38. Returns:
  39. Measurement: New Measurement object.
  40. """
  41. minimum, maximum = self
  42. width = max(0, width)
  43. return Measurement(max(minimum, width), max(maximum, width))
  44. def clamp(
  45. self, min_width: Optional[int] = None, max_width: Optional[int] = None
  46. ) -> "Measurement":
  47. """Clamp a measurement within the specified range.
  48. Args:
  49. min_width (int): Minimum desired width, or ``None`` for no minimum. Defaults to None.
  50. max_width (int): Maximum desired width, or ``None`` for no maximum. Defaults to None.
  51. Returns:
  52. Measurement: New Measurement object.
  53. """
  54. measurement = self
  55. if min_width is not None:
  56. measurement = measurement.with_minimum(min_width)
  57. if max_width is not None:
  58. measurement = measurement.with_maximum(max_width)
  59. return measurement
  60. @classmethod
  61. def get(
  62. cls, console: "Console", options: "ConsoleOptions", renderable: "RenderableType"
  63. ) -> "Measurement":
  64. """Get a measurement for a renderable.
  65. Args:
  66. console (~rich.console.Console): Console instance.
  67. options (~rich.console.ConsoleOptions): Console options.
  68. renderable (RenderableType): An object that may be rendered with Rich.
  69. Raises:
  70. errors.NotRenderableError: If the object is not renderable.
  71. Returns:
  72. Measurement: Measurement object containing range of character widths required to render the object.
  73. """
  74. _max_width = options.max_width
  75. if _max_width < 1:
  76. return Measurement(0, 0)
  77. if isinstance(renderable, str):
  78. renderable = console.render_str(renderable, markup=options.markup)
  79. renderable = rich_cast(renderable)
  80. if is_renderable(renderable):
  81. get_console_width: Optional[
  82. Callable[["Console", "ConsoleOptions"], "Measurement"]
  83. ] = getattr(renderable, "__rich_measure__", None)
  84. if get_console_width is not None:
  85. render_width = (
  86. get_console_width(console, options)
  87. .normalize()
  88. .with_maximum(_max_width)
  89. )
  90. if render_width.maximum < 1:
  91. return Measurement(0, 0)
  92. return render_width.normalize()
  93. else:
  94. return Measurement(0, _max_width)
  95. else:
  96. raise errors.NotRenderableError(
  97. f"Unable to get render width for {renderable!r}; "
  98. "a str, Segment, or object with __rich_console__ method is required"
  99. )
  100. def measure_renderables(
  101. console: "Console",
  102. options: "ConsoleOptions",
  103. renderables: Iterable["RenderableType"],
  104. ) -> "Measurement":
  105. """Get a measurement that would fit a number of renderables.
  106. Args:
  107. console (~rich.console.Console): Console instance.
  108. options (~rich.console.ConsoleOptions): Console options.
  109. renderables (Iterable[RenderableType]): One or more renderable objects.
  110. Returns:
  111. Measurement: Measurement object containing range of character widths required to
  112. contain all given renderables.
  113. """
  114. if not renderables:
  115. return Measurement(0, 0)
  116. get_measurement = Measurement.get
  117. measurements = [
  118. get_measurement(console, options, renderable) for renderable in renderables
  119. ]
  120. measured_width = Measurement(
  121. max(measurements, key=itemgetter(0)).minimum,
  122. max(measurements, key=itemgetter(1)).maximum,
  123. )
  124. return measured_width