Write lines of source code possibly highlighted. Keeping this private for now because the API is clunky. We should discuss how to evolve the terminal writer so we can have more precise color support, for example being able to write part of a line in one color and the rest in
(self, lines: Sequence[str], indents: Sequence[str] = ())
| 187 | self._file.flush() |
| 188 | |
| 189 | def _write_source(self, lines: Sequence[str], indents: Sequence[str] = ()) -> None: |
| 190 | """Write lines of source code possibly highlighted. |
| 191 | |
| 192 | Keeping this private for now because the API is clunky. We should discuss how |
| 193 | to evolve the terminal writer so we can have more precise color support, for example |
| 194 | being able to write part of a line in one color and the rest in another, and so on. |
| 195 | """ |
| 196 | if indents and len(indents) != len(lines): |
| 197 | raise ValueError( |
| 198 | f"indents size ({len(indents)}) should have same size as lines ({len(lines)})" |
| 199 | ) |
| 200 | if not indents: |
| 201 | indents = [""] * len(lines) |
| 202 | source = "\n".join(lines) |
| 203 | new_lines = self._highlight(source).splitlines() |
| 204 | # Would be better to strict=True but that fails some CI jobs. |
| 205 | for indent, new_line in zip(indents, new_lines, strict=False): |
| 206 | self.line(indent + new_line) |
| 207 | |
| 208 | def _get_pygments_lexer(self, lexer: Literal["python", "diff"]) -> Lexer: |
| 209 | if lexer == "python": |