@@ -566,13 +566,14 @@ def poutput(self, msg, end='\n'):
566
566
Also handles BrokenPipeError exceptions for when a commands's output has been piped to another process and
567
567
that process terminates before the cmd2 command is finished executing.
568
568
569
- :param msg: str - message to print to current stdout
569
+ :param msg: str - message to print to current stdout - anyting convertible to a str with '{}'.format() is OK
570
570
:param end: str - string appended after the end of the message if not already present, default a newline
571
571
"""
572
572
if msg :
573
573
try :
574
- self .stdout .write (msg )
575
- if not msg .endswith (end ):
574
+ msg_str = '{}' .format (msg )
575
+ self .stdout .write (msg_str )
576
+ if not msg_str .endswith (end ):
576
577
self .stdout .write (end )
577
578
except BrokenPipeError :
578
579
# This occurs if a command's output is being piped to another process and that process closes before the
@@ -698,6 +699,10 @@ def postparsing_postcmd(self, stop):
698
699
:param stop: bool - True implies the entire application should exit.
699
700
:return: bool - True implies the entire application should exit.
700
701
"""
702
+ if not sys .platform .startswith ('win' ):
703
+ # Fix those annoying problems that occur with terminal programs like "less" when you pipe to them
704
+ proc = subprocess .Popen (shlex .split ('stty sane' ))
705
+ proc .communicate ()
701
706
return stop
702
707
703
708
def parseline (self , line ):
@@ -844,21 +849,21 @@ def _restore_output(self, statement):
844
849
self .stdout .seek (0 )
845
850
write_to_paste_buffer (self .stdout .read ())
846
851
847
- # Close the file or pipe that stdout was redirected to
848
852
try :
853
+ # Close the file or pipe that stdout was redirected to
849
854
self .stdout .close ()
850
855
except BrokenPipeError :
851
856
pass
857
+ finally :
858
+ # Restore self.stdout
859
+ self .kept_state .restore ()
860
+ self .kept_state = None
852
861
853
862
# If we were piping output to a shell command, then close the subprocess the shell command was running in
854
863
if self .pipe_proc is not None :
855
864
self .pipe_proc .communicate ()
856
865
self .pipe_proc = None
857
866
858
- # Restore self.stdout
859
- self .kept_state .restore ()
860
- self .kept_state = None
861
-
862
867
# Restore sys.stdout if need be
863
868
if self .kept_sys is not None :
864
869
self .kept_sys .restore ()
@@ -895,15 +900,15 @@ def onecmd(self, line):
895
900
statement = self .parser_manager .parsed (line )
896
901
funcname = self ._func_named (statement .parsed .command )
897
902
if not funcname :
898
- return self ._default (statement )
903
+ return self .default (statement )
899
904
try :
900
905
func = getattr (self , funcname )
901
906
except AttributeError :
902
- return self ._default (statement )
907
+ return self .default (statement )
903
908
stop = func (statement )
904
909
return stop
905
910
906
- def _default (self , statement ):
911
+ def default (self , statement ):
907
912
"""Executed when the command given isn't a recognized command implemented by a do_* method.
908
913
909
914
:param statement: ParsedString - subclass of string including the pyparsing ParseResults
@@ -914,12 +919,10 @@ def _default(self, statement):
914
919
result = os .system (arg )
915
920
# If os.system() succeeded, then don't print warning about unknown command
916
921
if not result :
917
- return False
922
+ return
918
923
919
924
# Print out a message stating this is an unknown command
920
- self .default (arg )
921
-
922
- return False
925
+ self .poutput ('*** Unknown syntax: {}\n ' .format (arg ))
923
926
924
927
@staticmethod
925
928
def _surround_ansi_escapes (prompt , start = "\x01 " , end = "\x02 " ):
0 commit comments