Skip to content

Commit

Permalink
Updates to match latest master
Browse files Browse the repository at this point in the history
  • Loading branch information
anselor committed Sep 27, 2023
1 parent ea262dc commit b7ded80
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 10 deletions.
1 change: 0 additions & 1 deletion cmd2/cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '',
Expand Down
127 changes: 118 additions & 9 deletions cmd2/mixins/rich.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import sys
from typing import (
Any,
Callable,
IO,
Optional,
TextIO,
Union,
)

from rich import Console
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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):
"""
Expand All @@ -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)




0 comments on commit b7ded80

Please sign in to comment.