Skip to content

Commit a97239b

Browse files
committed
postprocess: use rich to pretty-print the diff
1 parent 24f2c84 commit a97239b

4 files changed

Lines changed: 55 additions & 5 deletions

File tree

c2rust-postprocess/postprocess/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ def main(argv: Sequence[str] | None = None):
154154
break
155155

156156
for failure in failures:
157-
print(f"{failure}\n")
157+
failure.print()
158+
print()
158159

159160
if failures:
160161
print(f"""\

c2rust-postprocess/postprocess/transforms.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from textwrap import dedent
99

1010
import yaml
11+
from rich.console import Console
12+
from rich.syntax import Syntax
1113

1214
from postprocess.cache import AbstractCache
1315
from postprocess.definitions import (
@@ -87,7 +89,7 @@ def header(self) -> str:
8789
)
8890

8991
def diff(self) -> str:
90-
diff = "".join(
92+
return "".join(
9193
unified_diff(
9294
a=[f"{comment}\n" for comment in self.c_comments],
9395
b=[f"{comment}\n" for comment in self.rust_comments],
@@ -96,13 +98,23 @@ def diff(self) -> str:
9698
tofile=f"{self.prompt.rust_source_file}:{self.prompt.identifier}",
9799
)
98100
).rstrip()
101+
102+
def __str__(self) -> str:
99103
return f"""\
104+
{self.header()}
105+
100106
```diff
101-
{diff}
107+
{self.diff()}
102108
```"""
103109

104-
def __str__(self) -> str:
105-
return f"{self.header()}\n\n{self.diff()}"
110+
def print(self):
111+
console = Console()
112+
console.print(f"""\
113+
{self.header()}
114+
115+
```diff""")
116+
console.print(Syntax(self.diff(), "diff"))
117+
console.print("```")
106118

107119
@staticmethod
108120
def to_exclude_file(failures: Iterable["CommentTransferFailure"]) -> str:

c2rust-postprocess/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ dependencies = [
99
"platformdirs>=4.5.1",
1010
"pygments>=2.19.2",
1111
"pyyaml>=6.0.3",
12+
"rich>=14.2.0",
1213
"tomli>=2.3.0",
1314
"tomlkit>=0.13.3",
1415
"tree-sitter>=0.25.2",

c2rust-postprocess/uv.lock

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)