@@ -1056,7 +1056,7 @@ def visible_prompt(self) -> str:
1056
1056
"""
1057
1057
return ansi .strip_style (self .prompt )
1058
1058
1059
- def poutput (self , msg : Any = '' , * , end : str = '\n ' ) -> None :
1059
+ def poutput (self , msg : Any = '' , * , end : str = '\n ' , apply_style : bool = True ) -> None :
1060
1060
"""Print message to self.stdout and appends a newline by default
1061
1061
1062
1062
Also handles BrokenPipeError exceptions for when a command's output has
@@ -1065,9 +1065,15 @@ def poutput(self, msg: Any = '', *, end: str = '\n') -> None:
1065
1065
1066
1066
:param msg: object to print
1067
1067
: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.
1068
1070
"""
1071
+ if apply_style :
1072
+ final_msg = ansi .style_output (msg )
1073
+ else :
1074
+ final_msg = str (msg )
1069
1075
try :
1070
- ansi .style_aware_write (self .stdout , f"{ msg } { end } " )
1076
+ ansi .style_aware_write (self .stdout , f"{ final_msg } { end } " )
1071
1077
except BrokenPipeError :
1072
1078
# This occurs if a command's output is being piped to another
1073
1079
# process and that process closes before the command is
@@ -1077,6 +1083,20 @@ def poutput(self, msg: Any = '', *, end: str = '\n') -> None:
1077
1083
if self .broken_pipe_warning :
1078
1084
sys .stderr .write (self .broken_pipe_warning )
1079
1085
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
+
1080
1100
# noinspection PyMethodMayBeStatic
1081
1101
def perror (self , msg : Any = '' , * , end : str = '\n ' , apply_style : bool = True ) -> None :
1082
1102
"""Print message to sys.stderr
@@ -4985,8 +5005,8 @@ class TestMyAppCase(Cmd2TestCase):
4985
5005
if test_results .wasSuccessful ():
4986
5006
ansi .style_aware_write (sys .stderr , stream .read ())
4987
5007
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 )
4990
5010
else :
4991
5011
# Strip off the initial traceback which isn't particularly useful for end users
4992
5012
error_str = stream .read ()
0 commit comments