Skip to content

Commit 3f208a8

Browse files
committed
Updated poutput to use new style_output definition. Added psuccess() that uses style_success
1 parent adc5a71 commit 3f208a8

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

cmd2/ansi.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,9 @@ def style(
10421042
# Default styles for printing strings of various types.
10431043
# These can be altered to suit an application's needs and only need to be a
10441044
# function with the following structure: func(str) -> str
1045+
style_output = functools.partial(style)
1046+
"""Partial function supplying arguments to :meth:`cmd2.ansi.style()` which colors text for normal output"""
1047+
10451048
style_success = functools.partial(style, fg=Fg.GREEN)
10461049
"""Partial function supplying arguments to :meth:`cmd2.ansi.style()` which colors text to signify success"""
10471050

cmd2/cmd2.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,7 @@ def visible_prompt(self) -> str:
10561056
"""
10571057
return ansi.strip_style(self.prompt)
10581058

1059-
def poutput(self, msg: Any = '', *, end: str = '\n') -> None:
1059+
def poutput(self, msg: Any = '', *, end: str = '\n', apply_style: bool = True) -> None:
10601060
"""Print message to self.stdout and appends a newline by default
10611061
10621062
Also handles BrokenPipeError exceptions for when a command's output has
@@ -1065,9 +1065,15 @@ def poutput(self, msg: Any = '', *, end: str = '\n') -> None:
10651065
10661066
:param msg: object to print
10671067
:param end: string appended after the end of the message, default a newline
1068+
:param apply_style: If True, then ansi.style_output will be applied to the message text. Set to False in cases
1069+
where the message text already has the desired style. Defaults to True.
10681070
"""
1071+
if apply_style:
1072+
final_msg = ansi.style_output(msg)
1073+
else:
1074+
final_msg = str(msg)
10691075
try:
1070-
ansi.style_aware_write(self.stdout, f"{msg}{end}")
1076+
ansi.style_aware_write(self.stdout, f"{final_msg}{end}")
10711077
except BrokenPipeError:
10721078
# This occurs if a command's output is being piped to another
10731079
# process and that process closes before the command is
@@ -1077,6 +1083,20 @@ def poutput(self, msg: Any = '', *, end: str = '\n') -> None:
10771083
if self.broken_pipe_warning:
10781084
sys.stderr.write(self.broken_pipe_warning)
10791085

1086+
def psuccess(self, msg: Any = '', *, end: str = '\n', apply_style: bool = True) -> None:
1087+
"""Wraps poutput but applies ansi.style_success by default
1088+
1089+
:param msg: object to print
1090+
:param end: string appended after the end of the message, default a newline
1091+
:param apply_style: If True, then ansi.style_success will be applied to the message text. Set to False in cases
1092+
where the message text already has the desired style. Defaults to True.
1093+
"""
1094+
if apply_style:
1095+
msg = ansi.style_success(msg)
1096+
else:
1097+
msg = str(msg)
1098+
self.poutput(msg, end=end, apply_style=False)
1099+
10801100
# noinspection PyMethodMayBeStatic
10811101
def perror(self, msg: Any = '', *, end: str = '\n', apply_style: bool = True) -> None:
10821102
"""Print message to sys.stderr
@@ -4985,8 +5005,8 @@ class TestMyAppCase(Cmd2TestCase):
49855005
if test_results.wasSuccessful():
49865006
ansi.style_aware_write(sys.stderr, stream.read())
49875007
finish_msg = f' {num_transcripts} transcript{plural} passed in {execution_time:.3f} seconds '
4988-
finish_msg = ansi.style_success(utils.align_center(finish_msg, fill_char='='))
4989-
self.poutput(finish_msg)
5008+
finish_msg = utils.align_center(finish_msg, fill_char='=')
5009+
self.psuccess(finish_msg)
49905010
else:
49915011
# Strip off the initial traceback which isn't particularly useful for end users
49925012
error_str = stream.read()

0 commit comments

Comments
 (0)