diff --git a/dvc/commands/plots.py b/dvc/commands/plots.py index 5fca3e78b..04a3d65d0 100644 --- a/dvc/commands/plots.py +++ b/dvc/commands/plots.py @@ -19,7 +19,7 @@ def _show_json(renderers, split=False): from dvc.render.convert import to_json result = {renderer.name: to_json(renderer, split) for renderer in renderers} - ui.write_json(result) + ui.write_json(result, highlight=False) def _adjust_vega_renderers(renderers): diff --git a/dvc/ui/__init__.py b/dvc/ui/__init__.py index 88c4a9430..e11abeb8a 100644 --- a/dvc/ui/__init__.py +++ b/dvc/ui/__init__.py @@ -17,7 +17,9 @@ from dvc.utils.objects import cached_property if TYPE_CHECKING: from rich.console import Console as RichConsole + from rich.console import JustifyMethod, OverflowMethod from rich.status import Status + from rich.style import Style from rich.text import Text as RichText from dvc.progress import Tqdm @@ -105,8 +107,9 @@ class Console: if indent is None and self.isatty(): indent = 2 - console = self.error_console if stderr else self.rich_console - return console.print_json( + from rich.json import JSON + + json = JSON.from_data( data=data, indent=indent, highlight=bool(highlight), @@ -117,6 +120,50 @@ class Console: default=default, sort_keys=sort_keys, ) + if not highlight: + return self.write(json.text, stderr=stderr) + return self.rich_print(json, stderr=stderr, soft_wrap=True) + + def rich_print( + self, + *objects: Any, + sep: str = " ", + end: str = "\n", + stderr: bool = False, + style: Optional[Union[str, "Style"]] = None, + justify: Optional["JustifyMethod"] = None, + overflow: Optional["OverflowMethod"] = None, + no_wrap: Optional[bool] = None, + emoji: Optional[bool] = None, + markup: Optional[bool] = None, + highlight: Optional[bool] = None, + width: Optional[int] = None, + height: Optional[int] = None, + crop: bool = True, + soft_wrap: Optional[bool] = None, + new_line_start: bool = False, + ) -> None: + if stderr: + console = self.error_console + else: + console = self.rich_console + return console.print( + *objects, + sep=sep, + end=end, + style=style, + justify=justify, + overflow=overflow, + no_wrap=no_wrap, + emoji=emoji, + markup=markup, + highlight=highlight, + width=width, + height=height, + crop=crop, + soft_wrap=soft_wrap, + new_line_start=new_line_start, + ) def write( self, @@ -142,10 +189,21 @@ class Console: with Tqdm.external_write_mode(file=file): # if we are inside pager context, send the output to rich's buffer if styled or self._paginate.get(): - console = self.error_console if stderr else self.rich_console if styled: - return console.print(*objects, sep=sep, end=end) - return console.out(*objects, sep=sep, end=end, highlight=False) + return self.rich_print(*objects, sep=sep, end=end, stderr=stderr) + return self.rich_print( + sep.join(str(_object) for _object in objects), + style=None, + highlight=False, + emoji=False, + markup=False, + no_wrap=True, + overflow="ignore", + crop=False, + sep=sep, + end=end, + stderr=stderr, + ) values = (self.formatter.format(obj, style) for obj in objects) return print(*values, sep=sep, end=end, file=file)