summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Lin <anselor@gmail.com>2022-04-18 16:07:34 -0400
committerEric Lin <anselor@gmail.com>2022-04-18 16:07:34 -0400
commit026e279512087f953b1780c48a7183c357b4c904 (patch)
tree5633978bdc5a2336a3af45e3411439aa70c6e8cd
parent96acc5a36233f17edcb0925ddf4ff382a0fa0f77 (diff)
downloadcmd2-git-1216-rich-print-mixin.tar.gz
Initial rich mixin class1216-rich-print-mixin
-rw-r--r--cmd2/mixins/__init__.py0
-rw-r--r--cmd2/mixins/rich.py82
-rwxr-xr-xsetup.py3
3 files changed, 85 insertions, 0 deletions
diff --git a/cmd2/mixins/__init__.py b/cmd2/mixins/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/cmd2/mixins/__init__.py
diff --git a/cmd2/mixins/rich.py b/cmd2/mixins/rich.py
new file mode 100644
index 00000000..85fe7e74
--- /dev/null
+++ b/cmd2/mixins/rich.py
@@ -0,0 +1,82 @@
+import sys
+from typing import (
+ Any,
+)
+
+from rich import Console
+
+try:
+ from typing import (
+ Protocol,
+ runtime_checkable,
+)
+except ImportError:
+ from typing_extensions import ( # type: ignore[misc]
+ Protocol,
+ runtime_checkable,
+ )
+
+
+class Cmd2PrintProtocol(Protocol):
+ debug: bool
+
+ def poutput(self, msg: Any = '', *, end: str = '\n') -> None:
+ """Print message to self.stdout and appends a newline by default
+
+ Also handles BrokenPipeError exceptions for when a command's output has
+ been piped to another process and that process terminates before the
+ cmd2 command is finished executing.
+
+ :param msg: object to print
+ :param end: string appended after the end of the message, default a newline
+ """
+
+ # noinspection PyMethodMayBeStatic
+ def perror(self, msg: Any = '', *, end: str = '\n', apply_style: bool = True) -> None:
+ """Print message to sys.stderr
+
+ :param msg: object to print
+ :param end: string appended after the end of the message, default a newline
+ :param apply_style: If True, then ansi.style_error will be applied to the message text. Set to False in cases
+ where the message text already has the desired style. Defaults to True.
+ """
+
+ def pwarning(self, msg: Any = '', *, end: str = '\n', apply_style: bool = True) -> None:
+ """Wraps perror, but applies ansi.style_warning by default
+
+ :param msg: object to print
+ :param end: string appended after the end of the message, default a newline
+ :param apply_style: If True, then ansi.style_warning will be applied to the message text. Set to False in cases
+ where the message text already has the desired style. Defaults to True.
+ """
+
+ def pexcept(self, msg: Any, *, end: str = '\n', apply_style: bool = True) -> None:
+ """Print Exception message to sys.stderr. If debug is true, print exception traceback if one exists.
+
+ :param msg: message or Exception to print
+ :param end: string appended after the end of the message, default a newline
+ :param apply_style: If True, then ansi.style_error will be applied to the message text. Set to False in cases
+ where the message text already has the desired style. Defaults to True.
+ """
+
+
+class RichCmd(Cmd2PrintProtocol):
+ """
+ Mixin class that swaps out the cmd2.Cmd output calls to use the ``rich`` library.
+
+ NOTE: Mixin classes should be placed to the left of the base Cmd class in the inheritance declaration.
+ """
+ def __init__(self, *args, **kwargs) -> None:
+ super(RichCmd, self).__init__(*args, **kwargs)
+ self.rich_out = Console(file=self.stdout)
+ self.rich_err = Console(stderr=True)
+
+ def pexcept(self, msg: Any, *, end: str = '\n', apply_style: bool = True) -> None:
+ if self.debug and sys.exc_info() != (None, None, None):
+ self.rich_err.print_exception(show_locals=True)
+ else:
+ super().pexcept(msg, end=end, apply_style=apply_style)
+
+
+
+
diff --git a/setup.py b/setup.py
index 9c133df9..c88d3ab0 100755
--- a/setup.py
+++ b/setup.py
@@ -83,6 +83,9 @@ EXTRAS_REQUIRE = {
'mypy==0.902',
'types-pkg-resources',
],
+ 'rich': [
+ 'rich',
+ ]
}
PACKAGE_DATA = {