@@ -112,52 +112,56 @@ def shprint(command, *args, **kwargs):
112
112
kwargs ["_out_bufsize" ] = 1
113
113
kwargs ["_err_to_out" ] = True
114
114
kwargs ["_bg" ] = True
115
+ is_critical = kwargs .pop ('_critical' , False )
116
+ tail_n = kwargs .pop ('_tail' , 0 )
115
117
if len (logger .handlers ) > 1 :
116
118
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
117
123
command_path = str (command ).split ('/' )
118
124
command_string = command_path [- 1 ]
119
125
string = ' ' .join (['running' , command_string ] + list (args ))
120
126
121
127
# If logging is not in DEBUG mode, trim the command if necessary
122
128
if logger .level > logging .DEBUG :
123
129
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 ) )
126
132
logger .info (short_string + Style .RESET_ALL )
127
133
else :
128
134
logger .debug (string + Style .RESET_ALL )
129
135
136
+ need_closing_newline = False
130
137
try :
138
+ msg_hdr = ' working: '
139
+ msg_width = columns - len (msg_hdr ) - 1
131
140
output = command (* args , ** kwargs )
132
- need_closing_newline = False
133
141
for line in output :
134
142
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
144
149
else :
145
150
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 )))
148
152
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 :
152
155
lines = err .stdout .splitlines ()
153
- if len (lines ) <= N :
156
+ if len (lines ) <= tail_n :
154
157
info ('STDOUT:\n {}\t {}{}' .format (Fore .YELLOW , '\t \n ' .join (lines ), Fore .RESET ))
155
158
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 ))
157
160
lines = err .stderr .splitlines ()
158
161
if len (lines ):
159
162
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 ))
161
165
exit (1 )
162
166
else :
163
167
raise
0 commit comments