command: Show dvc list output with pretty JSON format if output to tty (#6647)

1. update ls __init__.py to output pretty JSON format
2. introduce write_json function into ui __init__.py

Co-authored-by: Zhenyu Gao <[email protected]>
This commit is contained in:
eggqq007
2021-09-29 08:44:33 +05:45
committed by GitHub
co-authored by Zhenyu Gao
parent 05eaeef012
commit fed9f088ca
3 changed files with 16 additions and 4 deletions
+1 -3
View File
@@ -35,9 +35,7 @@ class CmdList(CmdBaseNoRepo):
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))
+14
View File
@@ -82,6 +82,20 @@ class Console:
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,
+1 -1
View File
@@ -34,7 +34,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