| 94 | ) |
| 95 | |
| 96 | def write_json( |
| 97 | self, |
| 98 | data: Any, |
| 99 | indent: Optional[int] = None, |
| 100 | highlight: Optional[bool] = None, |
| 101 | stderr: bool = False, |
| 102 | skip_keys: bool = False, |
| 103 | ensure_ascii: bool = True, |
| 104 | check_circular: bool = True, |
| 105 | allow_nan: bool = True, |
| 106 | default: Optional[Callable[[Any], Any]] = None, |
| 107 | sort_keys: bool = False, |
| 108 | ) -> None: |
| 109 | if highlight is None: |
| 110 | highlight = self.isatty() |
| 111 | if indent is None and self.isatty(): |
| 112 | indent = 2 |
| 113 | |
| 114 | from rich.json import JSON |
| 115 | |
| 116 | json = JSON.from_data( |
| 117 | data=data, |
| 118 | indent=indent, |
| 119 | highlight=bool(highlight), |
| 120 | skip_keys=skip_keys, |
| 121 | ensure_ascii=ensure_ascii, |
| 122 | check_circular=check_circular, |
| 123 | allow_nan=allow_nan, |
| 124 | default=default, |
| 125 | sort_keys=sort_keys, |
| 126 | ) |
| 127 | if not highlight: |
| 128 | import os |
| 129 | |
| 130 | # we don't need colorama to try to strip ansi codes |
| 131 | # when highlighting is disabled |
| 132 | ctx = nullcontext() if "DVC_TEST" in os.environ else disable_colorama() |
| 133 | with ctx: |
| 134 | return self.write(json.text, stderr=stderr) |
| 135 | return self.rich_print(json, stderr=stderr, soft_wrap=True) |
| 136 | |
| 137 | def rich_print( # noqa: PLR0913 |
| 138 | self, |