ui: disable colorama on write_json when highlighting is disabled (#9186)

On a large json output (say ~300MB), this saves about ~6 seconds of time.
This commit is contained in:
skshetry
2023-03-15 17:42:45 +05:45
committed by GitHub
parent c8745dd580
commit 8312eb9b42
+15 -1
View File
@@ -13,6 +13,8 @@ from typing import (
Union,
)
import colorama
from dvc.utils.objects import cached_property
if TYPE_CHECKING:
@@ -27,6 +29,15 @@ if TYPE_CHECKING:
from dvc.ui.table import Headers, Styles, TableData
@contextmanager
def disable_colorama():
colorama.deinit()
try:
yield
finally:
colorama.reinit()
class Formatter:
def __init__(
self, theme: Optional[Dict] = None, defaults: Optional[Dict] = None
@@ -121,7 +132,10 @@ class Console:
sort_keys=sort_keys,
)
if not highlight:
return self.write(json.text, stderr=stderr)
# we don't need colorama to try to strip ansi codes
# when highlighting is disabled
with disable_colorama():
return self.write(json.text, stderr=stderr)
return self.rich_print(json, stderr=stderr, soft_wrap=True)
def rich_print(