@@ -561,22 +561,25 @@ def decolorized_write(self, fileobj: IO, msg: str) -> None:
561561 msg = utils .strip_ansi (msg )
562562 fileobj .write (msg )
563563
564- def poutput (self , msg : Any , end : str = '\n ' ) -> None :
564+ def poutput (self , msg : Any , end : str = '\n ' , color : str = '' ) -> None :
565565 """Smarter self.stdout.write(); color aware and adds newline of not present.
566566
567567 Also handles BrokenPipeError exceptions for when a commands's output has
568568 been piped to another process and that process terminates before the
569569 cmd2 command is finished executing.
570570
571571 :param msg: message to print to current stdout (anything convertible to a str with '{}'.format() is OK)
572- :param end: string appended after the end of the message if not already present, default a newline
572+ :param end: (optional) string appended after the end of the message if not already present, default a newline
573+ :param color: (optional) color escape to output this message with
573574 """
574575 if msg is not None and msg != '' :
575576 try :
576577 msg_str = '{}' .format (msg )
577- self .decolorized_write (self .stdout , msg_str )
578578 if not msg_str .endswith (end ):
579- self .decolorized_write (self .stdout , end )
579+ msg_str += end
580+ if color :
581+ msg_str = color + msg_str + Fore .RESET
582+ self .decolorized_write (self .stdout , msg_str )
580583 except BrokenPipeError :
581584 # This occurs if a command's output is being piped to another
582585 # process and that process closes before the command is
@@ -586,12 +589,14 @@ def poutput(self, msg: Any, end: str='\n') -> None:
586589 if self .broken_pipe_warning :
587590 sys .stderr .write (self .broken_pipe_warning )
588591
589- def perror (self , err : Union [str , Exception ], traceback_war : bool = True ) -> None :
592+ def perror (self , err : Union [str , Exception ], traceback_war : bool = True , err_color : str = Fore .LIGHTRED_EX ,
593+ war_color : str = Fore .LIGHTYELLOW_EX ) -> None :
590594 """ Print error message to sys.stderr and if debug is true, print an exception Traceback if one exists.
591595
592596 :param err: an Exception or error message to print out
593597 :param traceback_war: (optional) if True, print a message to let user know they can enable debug
594- :return:
598+ :param err_color: (optional) color escape to output error with
599+ :param war_color: (optional) color escape to output warning with
595600 """
596601 if self .debug :
597602 import traceback
@@ -601,12 +606,12 @@ def perror(self, err: Union[str, Exception], traceback_war: bool=True) -> None:
601606 err_msg = "EXCEPTION of type '{}' occurred with message: '{}'\n " .format (type (err ).__name__ , err )
602607 else :
603608 err_msg = "ERROR: {}\n " .format (err )
604- err_msg = Fore . RED + err_msg + Fore .RESET
609+ err_msg = err_color + err_msg + Fore .RESET
605610 self .decolorized_write (sys .stderr , err_msg )
606611
607612 if traceback_war :
608613 war = "To enable full traceback, run the following command: 'set debug true'\n "
609- war = Fore . YELLOW + war + Fore .RESET
614+ war = war_color + war + Fore .RESET
610615 self .decolorized_write (sys .stderr , war )
611616
612617 def pfeedback (self , msg : str ) -> None :
@@ -679,7 +684,7 @@ def ppaged(self, msg: str, end: str='\n', chop: bool=False) -> None:
679684 except BrokenPipeError :
680685 # This occurs if a command's output is being piped to another process and that process closes before the
681686 # command is finished. If you would like your application to print a warning message, then set the
682- # broken_pipe_warning attribute to the message you want printed.
687+ # broken_pipe_warning attribute to the message you want printed.`
683688 if self .broken_pipe_warning :
684689 sys .stderr .write (self .broken_pipe_warning )
685690
0 commit comments