Like `rich.print()` but returns the string instead
(obj: object)
| 64 | |
| 65 | |
| 66 | def rich_print_str(obj: object) -> str: |
| 67 | """Like `rich.print()` but returns the string instead""" |
| 68 | |
| 69 | buf = io.StringIO() |
| 70 | console = rich.console.Console( |
| 71 | file=buf, |
| 72 | width=120, |
| 73 | force_terminal=False, |
| 74 | color_system=None, |
| 75 | record=True, |
| 76 | ) |
| 77 | |
| 78 | # Use Rich's pretty printer for nice multi-line formatting |
| 79 | rich.pretty.install(console) |
| 80 | console.print(obj, overflow="fold", width=120) |
| 81 | |
| 82 | result = buf.getvalue() |
| 83 | # Strip out [~...] patterns to exclude generic content from snapshots |
| 84 | result = re.sub(r"\[~[^\]]*\]", "", result) |
| 85 | result = result.replace("[TypeVar]", "") |
| 86 | return result |