Skip to content

Commit

Permalink
use global UI object for printing (iterative#5653)
Browse files Browse the repository at this point in the history
  • Loading branch information
skshetry authored Mar 24, 2021
1 parent f2ba67a commit 8b6d655
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
8 changes: 5 additions & 3 deletions dvc/command/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def _show(self, status, indent=0):
self._show(value, indent + 1)

def run(self):
from dvc.ui import ui

indent = 1 if self.args.cloud else 0
try:
st = self.repo.status(
Expand All @@ -68,14 +70,14 @@ def run(self):
elif st:
self._show(st, indent)
elif not self.repo.stages:
logger.info(self.EMPTY_PROJECT_MSG)
ui.write(self.EMPTY_PROJECT_MSG)
elif self.args.cloud or self.args.remote:
remote = self.args.remote or self.repo.config["core"].get(
"remote"
)
logger.info(self.IN_SYNC_MSG.format(remote=remote))
ui.write(self.IN_SYNC_MSG.format(remote=remote))
else:
logger.info(self.UP_TO_DATE_MSG)
ui.write(self.UP_TO_DATE_MSG)

except DvcException:
logger.exception("")
Expand Down
5 changes: 5 additions & 0 deletions dvc/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ def main(argv=None): # noqa: C901

logger.trace(args)

if not args.quiet:
from dvc.ui import ui

ui.enable()

with debugtools(args):
cmd = args.func(args)
ret = cmd.run()
Expand Down
26 changes: 14 additions & 12 deletions dvc/ui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
from dvc.progress import Tqdm
from dvc.utils import colorize

NEWLINE = "\n"
SEP = " "


class Formatter:
def __init__(self, theme: Dict = None, defaults: Dict = None) -> None:
Expand All @@ -30,14 +27,17 @@ def __init__(
formatter: Formatter = None,
output: TextIO = None,
error: TextIO = None,
disable: bool = False,
enable: bool = False,
) -> None:
self._input: TextIO = sys.stdin
self._output: TextIO = output or sys.stdout
self._error: TextIO = error or sys.stderr

self.formatter: Formatter = formatter or Formatter()
self.disabled: bool = disable
self._enabled: bool = enable

def enable(self):
self._enabled = True

def success(self, message: str) -> None:
self.write(message, style="success")
Expand All @@ -52,8 +52,8 @@ def error_write(
self,
*objects: Any,
style: str = None,
sep: str = SEP,
end: str = NEWLINE,
sep: str = None,
end: str = None,
flush: bool = False,
) -> None:
return self.write(
Expand All @@ -69,12 +69,12 @@ def write(
self,
*objects: Any,
style: str = None,
sep: str = SEP,
end: str = NEWLINE,
sep: str = None,
end: str = None,
file: TextIO = None,
flush: bool = False,
) -> None:
if self.disabled:
if not self._enabled:
return

file = file or self._output
Expand Down Expand Up @@ -152,9 +152,11 @@ def table(self, header, rows, markdown: bool = False):
self.write(ret)


if __name__ == "__main__":
ui = Console()
ui = Console()


if __name__ == "__main__":
ui.enable()
ui.write("No default remote set")
ui.success("Everything is up to date.")
ui.warn("Run queued experiments will be removed.")
Expand Down

0 comments on commit 8b6d655

Please sign in to comment.