Renders a solid block bar. Args: size (float): Value for the end of the bar. begin (float): Begin point (between 0 and size, inclusive). end (float): End point (between 0 and size, inclusive). width (int, optional): Width of the bar, or ``None`` for maximum width
| 15 | |
| 16 | |
| 17 | class Bar(JupyterMixin): |
| 18 | """Renders a solid block bar. |
| 19 | |
| 20 | Args: |
| 21 | size (float): Value for the end of the bar. |
| 22 | begin (float): Begin point (between 0 and size, inclusive). |
| 23 | end (float): End point (between 0 and size, inclusive). |
| 24 | width (int, optional): Width of the bar, or ``None`` for maximum width. Defaults to None. |
| 25 | color (Union[Color, str], optional): Color of the bar. Defaults to "default". |
| 26 | bgcolor (Union[Color, str], optional): Color of bar background. Defaults to "default". |
| 27 | """ |
| 28 | |
| 29 | def __init__( |
| 30 | self, |
| 31 | size: float, |
| 32 | begin: float, |
| 33 | end: float, |
| 34 | *, |
| 35 | width: Optional[int] = None, |
| 36 | color: Union[Color, str] = "default", |
| 37 | bgcolor: Union[Color, str] = "default", |
| 38 | ): |
| 39 | self.size = size |
| 40 | self.begin = max(begin, 0) |
| 41 | self.end = min(end, size) |
| 42 | self.width = width |
| 43 | self.style = Style(color=color, bgcolor=bgcolor) |
| 44 | |
| 45 | def __repr__(self) -> str: |
| 46 | return f"Bar({self.size}, {self.begin}, {self.end})" |
| 47 | |
| 48 | def __rich_console__( |
| 49 | self, console: Console, options: ConsoleOptions |
| 50 | ) -> RenderResult: |
| 51 | width = min( |
| 52 | self.width if self.width is not None else options.max_width, |
| 53 | options.max_width, |
| 54 | ) |
| 55 | |
| 56 | if self.begin >= self.end: |
| 57 | yield Segment(" " * width, self.style) |
| 58 | yield Segment.line() |
| 59 | return |
| 60 | |
| 61 | prefix_complete_eights = int(width * 8 * self.begin / self.size) |
| 62 | prefix_bar_count = prefix_complete_eights // 8 |
| 63 | prefix_eights_count = prefix_complete_eights % 8 |
| 64 | |
| 65 | body_complete_eights = int(width * 8 * self.end / self.size) |
| 66 | body_bar_count = body_complete_eights // 8 |
| 67 | body_eights_count = body_complete_eights % 8 |
| 68 | |
| 69 | # When start and end fall into the same cell, we ideally should render |
| 70 | # a symbol that's "center-aligned", but there is no good symbol in Unicode. |
| 71 | # In this case, we fall back to right-aligned block symbol for simplicity. |
| 72 | |
| 73 | prefix = " " * prefix_bar_count |
| 74 | if prefix_eights_count: |
no outgoing calls