4040import threading
4141from collections import namedtuple
4242from contextlib import redirect_stdout
43- from typing import Any , Callable , Dict , List , Mapping , Optional , Tuple , Type , Union , IO
43+ from typing import Any , Callable , Dict , List , Mapping , Optional , Tuple , Type , Union
4444
4545import colorama
4646
@@ -589,16 +589,6 @@ def allow_redirection(self, value: bool) -> None:
589589 """Setter for the allow_redirection property that determines whether or not redirection of stdout is allowed."""
590590 self ._statement_parser .allow_redirection = value
591591
592- def _decolorized_write (self , fileobj : IO , msg : str ) -> None :
593- """Write a string to a fileobject, stripping ANSI escape sequences if necessary
594-
595- Honor the current colors setting, which requires us to check whether the fileobject is a tty.
596- """
597- if ansi .allow_ansi .lower () == ansi .ANSI_NEVER .lower () or \
598- (ansi .allow_ansi .lower () == ansi .ANSI_TERMINAL .lower () and not fileobj .isatty ()):
599- msg = ansi .strip_ansi (msg )
600- fileobj .write (msg )
601-
602592 def poutput (self , msg : Any , * , end : str = '\n ' ) -> None :
603593 """Print message to self.stdout and appends a newline by default
604594
@@ -610,7 +600,7 @@ def poutput(self, msg: Any, *, end: str = '\n') -> None:
610600 :param end: string appended after the end of the message, default a newline
611601 """
612602 try :
613- self . _decolorized_write (self .stdout , "{}{}" .format (msg , end ))
603+ ansi . ansi_aware_write (self .stdout , "{}{}" .format (msg , end ))
614604 except BrokenPipeError :
615605 # This occurs if a command's output is being piped to another
616606 # process and that process closes before the command is
@@ -632,7 +622,7 @@ def perror(self, msg: Any, *, end: str = '\n', apply_style: bool = True) -> None
632622 final_msg = ansi .style_error (msg )
633623 else :
634624 final_msg = "{}" .format (msg )
635- self . _decolorized_write (sys .stderr , final_msg + end )
625+ ansi . ansi_aware_write (sys .stderr , final_msg + end )
636626
637627 def pexcept (self , msg : Any , * , end : str = '\n ' , apply_style : bool = True ) -> None :
638628 """Print Exception message to sys.stderr. If debug is true, print exception traceback if one exists.
@@ -668,7 +658,7 @@ def pfeedback(self, msg: str) -> None:
668658 if self .feedback_to_output :
669659 self .poutput (msg )
670660 else :
671- self . _decolorized_write (sys .stderr , "{}\n " .format (msg ))
661+ ansi . ansi_aware_write (sys .stderr , "{}\n " .format (msg ))
672662
673663 def ppaged (self , msg : str , end : str = '\n ' , chop : bool = False ) -> None :
674664 """Print output using a pager if it would go off screen and stdout isn't currently being redirected.
@@ -717,7 +707,7 @@ def ppaged(self, msg: str, end: str = '\n', chop: bool = False) -> None:
717707 pipe_proc = subprocess .Popen (pager , shell = True , stdin = subprocess .PIPE )
718708 pipe_proc .communicate (msg_str .encode ('utf-8' , 'replace' ))
719709 else :
720- self . _decolorized_write (self .stdout , msg_str )
710+ ansi . ansi_aware_write (self .stdout , msg_str )
721711 except BrokenPipeError :
722712 # This occurs if a command's output is being piped to another process and that process closes before the
723713 # command is finished. If you would like your application to print a warning message, then set the
@@ -2162,7 +2152,7 @@ def default(self, statement: Statement) -> Optional[bool]:
21622152 return self .do_shell (statement .command_and_args )
21632153 else :
21642154 err_msg = self .default_error .format (statement .command )
2165- self . _decolorized_write (sys .stderr , "{}\n " .format (err_msg ))
2155+ ansi . ansi_aware_write (sys .stderr , "{}\n " .format (err_msg ))
21662156
21672157 def _pseudo_raw_input (self , prompt : str ) -> str :
21682158 """Began life as a copy of cmd's cmdloop; like raw_input but
@@ -2695,7 +2685,7 @@ def do_help(self, args: argparse.Namespace) -> None:
26952685 # If there is no help information then print an error
26962686 elif help_func is None and (func is None or not func .__doc__ ):
26972687 err_msg = self .help_error .format (args .command )
2698- self . _decolorized_write (sys .stderr , "{}\n " .format (err_msg ))
2688+ ansi . ansi_aware_write (sys .stderr , "{}\n " .format (err_msg ))
26992689
27002690 # Otherwise delegate to cmd base class do_help()
27012691 else :
@@ -3767,7 +3757,7 @@ class TestMyAppCase(Cmd2TestCase):
37673757 test_results = runner .run (testcase )
37683758 execution_time = time .time () - start_time
37693759 if test_results .wasSuccessful ():
3770- self . _decolorized_write (sys .stderr , stream .read ())
3760+ ansi . ansi_aware_write (sys .stderr , stream .read ())
37713761 finish_msg = '{0} transcript{1} passed in {2:.3f} seconds' .format (num_transcripts , plural , execution_time )
37723762 finish_msg = ansi .style_success (utils .center_text (finish_msg , pad = '=' ))
37733763 self .poutput (finish_msg )
@@ -4026,7 +4016,7 @@ def _report_disabled_command_usage(self, *args, message_to_print: str, **kwargs)
40264016 :param message_to_print: the message reporting that the command is disabled
40274017 :param kwargs: not used
40284018 """
4029- self . _decolorized_write (sys .stderr , "{}\n " .format (message_to_print ))
4019+ ansi . ansi_aware_write (sys .stderr , "{}\n " .format (message_to_print ))
40304020
40314021 def cmdloop (self , intro : Optional [str ] = None ) -> int :
40324022 """This is an outer wrapper around _cmdloop() which deals with extra features provided by cmd2.
0 commit comments