diff --git a/dvc/command/ls/__init__.py b/dvc/command/ls/__init__.py index a44877f037..369eae681a 100644 --- a/dvc/command/ls/__init__.py +++ b/dvc/command/ls/__init__.py @@ -35,9 +35,7 @@ def run(self): dvc_only=self.args.dvc_only, ) if self.args.show_json: - import json - - ui.write(json.dumps(entries)) + ui.write_json(entries) elif entries: entries = _prettify(entries, with_color=True) ui.write("\n".join(entries)) diff --git a/dvc/ui/__init__.py b/dvc/ui/__init__.py index fd55858b33..b266a1db56 100644 --- a/dvc/ui/__init__.py +++ b/dvc/ui/__init__.py @@ -74,6 +74,20 @@ def error_write( styled=styled, ) + def write_json( + self, + data: Any, + indent: int = 2, + ) -> None: + if sys.stdout.isatty(): + from rich.json import JSON + + j = JSON.from_data(data, indent=indent) + return self.write(j, styled=True) + import json + + return self.write(json.dumps(data)) + def write( self, *objects: Any, diff --git a/requirements/default.txt b/requirements/default.txt index b97a691a84..a64b8e98bd 100644 --- a/requirements/default.txt +++ b/requirements/default.txt @@ -36,7 +36,7 @@ tabulate>=0.8.7 pygtrie>=2.3.2 dpath>=2.0.2,<3 shtab>=1.3.4,<2 -rich>=10.0.0 +rich>=10.9.0 dictdiffer>=0.8.1 python-benedict>=0.21.1 pyparsing==2.4.7