(
self, console: "Console", options: "ConsoleOptions"
)
| 141 | ) |
| 142 | |
| 143 | def __rich_console__( |
| 144 | self, console: "Console", options: "ConsoleOptions" |
| 145 | ) -> "RenderResult": |
| 146 | align = self.align |
| 147 | width = console.measure(self.renderable, options=options).maximum |
| 148 | rendered = console.render( |
| 149 | Constrain( |
| 150 | self.renderable, width if self.width is None else min(width, self.width) |
| 151 | ), |
| 152 | options.update(height=None), |
| 153 | ) |
| 154 | lines = list(Segment.split_lines(rendered)) |
| 155 | width, height = Segment.get_shape(lines) |
| 156 | lines = Segment.set_shape(lines, width, height) |
| 157 | new_line = Segment.line() |
| 158 | excess_space = options.max_width - width |
| 159 | style = console.get_style(self.style) if self.style is not None else None |
| 160 | |
| 161 | def generate_segments() -> Iterable[Segment]: |
| 162 | if excess_space <= 0: |
| 163 | # Exact fit |
| 164 | for line in lines: |
| 165 | yield from line |
| 166 | yield new_line |
| 167 | |
| 168 | elif align == "left": |
| 169 | # Pad on the right |
| 170 | pad = Segment(" " * excess_space, style) if self.pad else None |
| 171 | for line in lines: |
| 172 | yield from line |
| 173 | if pad: |
| 174 | yield pad |
| 175 | yield new_line |
| 176 | |
| 177 | elif align == "center": |
| 178 | # Pad left and right |
| 179 | left = excess_space // 2 |
| 180 | pad = Segment(" " * left, style) |
| 181 | pad_right = ( |
| 182 | Segment(" " * (excess_space - left), style) if self.pad else None |
| 183 | ) |
| 184 | for line in lines: |
| 185 | if left: |
| 186 | yield pad |
| 187 | yield from line |
| 188 | if pad_right: |
| 189 | yield pad_right |
| 190 | yield new_line |
| 191 | |
| 192 | elif align == "right": |
| 193 | # Padding on left |
| 194 | pad = Segment(" " * excess_space, style) |
| 195 | for line in lines: |
| 196 | yield pad |
| 197 | yield from line |
| 198 | yield new_line |
| 199 | |
| 200 | blank_line = ( |
nothing calls this directly
no test coverage detected