@@ -234,18 +234,27 @@ def stage(self, name: str):
234234 def print_stats (self ):
235235 total_duration = sum (self .stages .values ())
236236
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
239246
240247 with StringIO () as output :
241248 print (divider , file = output )
242249 for (name , duration ) in self .stages .items ():
243250 pct = (duration / total_duration ) * 100
244251 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 )
246254
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 )
249258 print (divider , file = output , end = "" )
250259 LOGGER .info (f"Timer results\n { output .getvalue ()} " )
251260
0 commit comments