@@ -234,18 +234,27 @@ def stage(self, name: str):
234
234
def print_stats (self ):
235
235
total_duration = sum (self .stages .values ())
236
236
237
- # 57 is the width of the whole table
238
- divider = "-" * 57
237
+ total_duration_label = "Total duration:"
238
+
239
+ # 1 is for ":", 2 is horizontal space
240
+ max_label_length = max (16 , max (
241
+ len (label ) for label in list (self .stages .keys ()) + [total_duration_label [:- 1 ]]
242
+ )) + 1 + 2
243
+
244
+ table_width = max_label_length + 24
245
+ divider = "-" * table_width
239
246
240
247
with StringIO () as output :
241
248
print (divider , file = output )
242
249
for (name , duration ) in self .stages .items ():
243
250
pct = (duration / total_duration ) * 100
244
251
name_str = f"{ name } :"
245
- print (f"{ name_str :<34} { duration :>12.2f} s ({ pct :>5.2f} %)" , file = output )
252
+ print (f"{ name_str :<{max_label_length }} { duration :>12.2f} s ({ pct :>5.2f} %)" ,
253
+ file = output )
246
254
247
- total_duration_label = "Total duration:"
248
- print (f"{ total_duration_label :<34} { total_duration :>12.2f} s" , file = output )
255
+ print (file = output )
256
+ print (f"{ total_duration_label :<{max_label_length }} { total_duration :>12.2f} s" ,
257
+ file = output )
249
258
print (divider , file = output , end = "" )
250
259
LOGGER .info (f"Timer results\n { output .getvalue ()} " )
251
260
0 commit comments