-
Notifications
You must be signed in to change notification settings - Fork 0
/
pretty_print.py
50 lines (35 loc) · 1.32 KB
/
pretty_print.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from abc import ABCMeta, abstractmethod
from typing import Any
from rich.console import Console
from rich.markdown import Markdown
from rich.pretty import pprint
class PrettyPrint(metaclass=ABCMeta):
@classmethod
@abstractmethod
def pretty_print(cls, content: Any):
raise NotImplementedError(
f"{cls.__name__} does not implement `pretty_print` func."
)
class PrintMarkDown(PrettyPrint):
"""Render Markdown to the console."""
@classmethod
def pretty_print(cls, content: Any, *args, **kwargs):
md = Markdown(content)
Console().print(md, *args, **kwargs)
class PrintJson(PrettyPrint):
"""Pretty prints JSON. Output will be valid JSON."""
@classmethod
def pretty_print(cls, content: Any, *args, **kwargs):
Console().print_json(content, *args, **kwargs)
class Pprint(PrettyPrint):
"""A convenience function for pretty printing."""
@classmethod
def pretty_print(cls, content: Any, *args, **kwargs):
pprint(content, *args, **kwargs)
####################################
class AllConsole:
def __init__(self, content: Any, consoleCls: object) -> None:
self._content: Any = content
self._console = consoleCls
def pretty_out(self, *args, **kwargs):
self._console.pretty_print(self._content, *args, **kwargs)