From b7ded80f282eed5d2a908beb22fcf625c914c388 Mon Sep 17 00:00:00 2001 From: Eric Lin Date: Wed, 27 Sep 2023 15:58:25 -0400 Subject: [PATCH] Updates to match latest master --- cmd2/cmd2.py | 1 - cmd2/mixins/rich.py | 127 ++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 118 insertions(+), 10 deletions(-) diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 228bdbe6..b7c6cbb2 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -1117,7 +1117,6 @@ def poutput( """ self.print_to(self.stdout, msg, end=end, style=ansi.style_output if apply_style else None, paged=paged, chop=chop) - # noinspection PyMethodMayBeStatic def perror( self, msg: Any = '', diff --git a/cmd2/mixins/rich.py b/cmd2/mixins/rich.py index 85fe7e74..9f330318 100644 --- a/cmd2/mixins/rich.py +++ b/cmd2/mixins/rich.py @@ -1,6 +1,11 @@ import sys from typing import ( Any, + Callable, + IO, + Optional, + TextIO, + Union, ) from rich import Console @@ -20,7 +25,35 @@ class Cmd2PrintProtocol(Protocol): debug: bool - def poutput(self, msg: Any = '', *, end: str = '\n') -> None: + def print_to( + self, + dest: Union[TextIO, IO[str]], + msg: Any, + *, + end: str = '\n', + style: Optional[Callable[[str], str]] = None, + paged: bool = False, + chop: bool = False, + ) -> None: + """Print output to the specified stream with the provided style and format options. + + :param dest: Destination stream to write to. + :param msg: object to print + :param end: string appended after the end of the message, default a newline + :param style: Optional style format function to apply + :param paged: If True, pass the output through the configured pager. + :param chop: If paged is True, True to truncate long lines or False to wrap long lines. + """ + + def poutput( + self, + msg: Any = '', + *, + end: str = '\n', + apply_style: bool = True, + paged: bool = False, + chop: bool = False, + ) -> None: """Print message to self.stdout and appends a newline by default Also handles BrokenPipeError exceptions for when a command's output has @@ -29,25 +62,85 @@ def poutput(self, msg: Any = '', *, end: str = '\n') -> None: :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_output will be applied to the message text. Set to False in cases + where the message text already has the desired style. Defaults to True. + :param paged: If True, pass the output through the configured pager. + :param chop: If paged is True, True to truncate long lines or False to wrap long lines. """ # noinspection PyMethodMayBeStatic - def perror(self, msg: Any = '', *, end: str = '\n', apply_style: bool = True) -> None: + def perror( + self, + msg: Any = '', + *, + end: str = '\n', + apply_style: bool = True, + paged: bool = False, + chop: bool = False, + ) -> 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. + :param paged: If True, pass the output through the configured pager. + :param chop: If paged is True, True to truncate long lines or False to wrap long lines. """ - def pwarning(self, msg: Any = '', *, end: str = '\n', apply_style: bool = True) -> None: + def psuccess( + self, + msg: Any = '', + *, + end: str = '\n', + paged: bool = False, + chop: bool = False, + ) -> None: + """Writes to stdout applying ansi.style_success by default + + :param msg: object to print + :param end: string appended after the end of the message, default a newline + :param paged: If True, pass the output through the configured pager. + :param chop: If paged is True, True to truncate long lines or False to wrap long lines. + """ + + def pwarning( + self, + msg: Any = '', + *, + end: str = '\n', + apply_style: bool = True, + paged: bool = False, + chop: bool = False, + ) -> 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. + :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. + + .. deprecated: 2.4.4 + Use :meth:`~cmd2.Cmd.print_to` instead to print to stderr without style applied. + :param paged: If True, pass the output through the configured pager. + :param chop: If paged is True, True to truncate long lines or False to wrap long lines. + """ + + def pfailure( + self, + msg: Any = '', + *, + end: str = '\n', + paged: bool = False, + chop: bool = False, + ) -> None: + """Writes to stderr applying ansi.style_error by default + + :param msg: object to print + :param end: string appended after the end of the message, default a newline + :param paged: If True, pass the output through the configured pager. + :param chop: If paged is True, True to truncate long lines or False to wrap long lines. """ def pexcept(self, msg: Any, *, end: str = '\n', apply_style: bool = True) -> None: @@ -59,6 +152,26 @@ def pexcept(self, msg: Any, *, end: str = '\n', apply_style: bool = True) -> Non where the message text already has the desired style. Defaults to True. """ + def pfeedback( + self, + msg: Any, + *, + end: str = '\n', + apply_style: bool = True, + paged: bool = False, + chop: bool = False, + ) -> None: + """For printing nonessential feedback. Can be silenced with `quiet`. + Inclusion in redirected output is controlled by `feedback_to_output`. + + :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_output will be applied to the message text. Set to False in cases + where the message text already has the desired style. Defaults to True. + :param paged: If True, pass the output through the configured pager. + :param chop: If paged is True, True to truncate long lines or False to wrap long lines. + """ + class RichCmd(Cmd2PrintProtocol): """ @@ -76,7 +189,3 @@ def pexcept(self, msg: Any, *, end: str = '\n', apply_style: bool = True) -> Non self.rich_err.print_exception(show_locals=True) else: super().pexcept(msg, end=end, apply_style=apply_style) - - - -