Inject the ANSI color codes to the diff.
(contents: str)
| 94 | |
| 95 | |
| 96 | def color_diff(contents: str) -> str: |
| 97 | """Inject the ANSI color codes to the diff.""" |
| 98 | lines = contents.split("\n") |
| 99 | for i, line in enumerate(lines): |
| 100 | if line.startswith("+++") or line.startswith("---"): |
| 101 | line = "\033[1m" + line + "\033[0m" # bold, reset |
| 102 | elif line.startswith("@@"): |
| 103 | line = "\033[36m" + line + "\033[0m" # cyan, reset |
| 104 | elif line.startswith("+"): |
| 105 | line = "\033[32m" + line + "\033[0m" # green, reset |
| 106 | elif line.startswith("-"): |
| 107 | line = "\033[31m" + line + "\033[0m" # red, reset |
| 108 | lines[i] = line |
| 109 | return "\n".join(lines) |
| 110 | |
| 111 | |
| 112 | @mypyc_attr(patchable=True) |
no outgoing calls