@@ -112,52 +112,56 @@ def shprint(command, *args, **kwargs):
112112 kwargs ["_out_bufsize" ] = 1
113113 kwargs ["_err_to_out" ] = True
114114 kwargs ["_bg" ] = True
115+ is_critical = kwargs .pop ('_critical' , False )
116+ tail_n = kwargs .pop ('_tail' , 0 )
115117 if len (logger .handlers ) > 1 :
116118 logger .removeHandler (logger .handlers [1 ])
119+ try :
120+ columns = max (25 , int (os .popen ('stty size' , 'r' ).read ().split ()[1 ]))
121+ except :
122+ columns = 100
117123 command_path = str (command ).split ('/' )
118124 command_string = command_path [- 1 ]
119125 string = ' ' .join (['running' , command_string ] + list (args ))
120126
121127 # If logging is not in DEBUG mode, trim the command if necessary
122128 if logger .level > logging .DEBUG :
123129 short_string = string
124- if len (string ) > 100 :
125- short_string = string [: 100 ] + ' ... (and {} more)' .format (len (string ) - 100 )
130+ if len (string ) > columns :
131+ short_string = '{} ...(and {} more)' .format (string [:( columns - 20 )], len (string ) - ( columns - 20 ) )
126132 logger .info (short_string + Style .RESET_ALL )
127133 else :
128134 logger .debug (string + Style .RESET_ALL )
129135
136+ need_closing_newline = False
130137 try :
138+ msg_hdr = ' working: '
139+ msg_width = columns - len (msg_hdr ) - 1
131140 output = command (* args , ** kwargs )
132- need_closing_newline = False
133141 for line in output :
134142 if logger .level > logging .DEBUG :
135- string = '' .join ([Style .RESET_ALL , '\r ' , ' ' * 11 , 'working ... ' ,
136- line [:100 ].replace ('\n ' , '' ).rstrip (), ' ...' ])
137- if len (string ) < 20 :
138- continue
139- if len (string ) < 120 :
140- string = string + ' ' * (120 - len (string ))
141- sys .stdout .write (string )
142- sys .stdout .flush ()
143- need_closing_newline = True
143+ msg = line .replace ('\n ' , ' ' ).rstrip ()
144+ if msg :
145+ # if len(msg) > msg_width: msg = msg[:(msg_width - 3)] + '...'
146+ sys .stdout .write ('{}\r {}{:<{width}.{width}}' .format (Style .RESET_ALL , msg_hdr , msg , width = msg_width ))
147+ sys .stdout .flush ()
148+ need_closing_newline = True
144149 else :
145150 logger .debug ('' .join (['\t ' , line .rstrip ()]))
146- if logger .level > logging .DEBUG and need_closing_newline :
147- print ()
151+ if need_closing_newline : sys .stdout .write ('{}\r {:>{width}}\r ' .format (Style .RESET_ALL , ' ' , width = (columns - 1 )))
148152 except sh .ErrorReturnCode_1 , err :
149- N = kwargs .get ('_tail' , 0 )
150- if N :
151- warning ("Error: {} failed" .format (command ))
153+ if need_closing_newline : sys .stdout .write ('{}\r {:>{width}}\r ' .format (Style .RESET_ALL , ' ' , width = (columns - 1 )))
154+ if tail_n :
152155 lines = err .stdout .splitlines ()
153- if len (lines ) <= N :
156+ if len (lines ) <= tail_n :
154157 info ('STDOUT:\n {}\t {}{}' .format (Fore .YELLOW , '\t \n ' .join (lines ), Fore .RESET ))
155158 else :
156- info ('STDOUT (last {} lines of {}):\n {}\t {}{}' .format (N , len (lines ), Fore .YELLOW , '\t \n ' .join (lines [- N :]), Fore .RESET ))
159+ info ('STDOUT (last {} lines of {}):\n {}\t {}{}' .format (tail_n , len (lines ), Fore .YELLOW , '\t \n ' .join (lines [- tail_n :]), Fore .RESET ))
157160 lines = err .stderr .splitlines ()
158161 if len (lines ):
159162 warning ('STDERR:\n {}\t {}{}' .format (Fore .RED , '\t \n ' .join (lines ), Fore .RESET ))
160- if kwargs .get ('_critical' , False ):
163+ if is_critical :
164+ warning ("{}ERROR: {} failed!{}" .format (Fore .RED , command , Fore .RESET ))
161165 exit (1 )
162166 else :
163167 raise
0 commit comments