@@ -948,7 +948,7 @@ def format(self, obj):
948
948
raise KeyError (f"Invalid format field { exc } for { typestr } " )
949
949
return retval
950
950
951
- def filter (self , items ):
951
+ def filter (self , items , no_header = False ):
952
952
"""
953
953
Check for format fields that are prefixed with `?:` (e.g. "?:{name}"),
954
954
and filter them out of the current format string if they result in an
@@ -960,6 +960,10 @@ def filter(self, items):
960
960
961
961
(`?+:` requests both actions: filter out field if it is empty for all
962
962
items, if not expand to maximum width)
963
+
964
+ Args:
965
+ items (iterable): list of items to consider for output
966
+ no_header (boolean): do not use header in calculations (default: False)
963
967
"""
964
968
965
969
# Build a list of all format strings that have one of the width
@@ -1000,12 +1004,18 @@ def sentinel_keys():
1000
1004
1001
1005
# Save the modified format, index, type, maximum width,
1002
1006
# observed width, and broken-down spec in lst:
1007
+ initialmaxwidth = spec .width or 0
1008
+ if sentinels [end ] in ("maxwidth" , "both" ):
1009
+ initialmaxwidth = max (
1010
+ initialmaxwidth ,
1011
+ 0 if no_header else len (self .headings .get (field , "" )),
1012
+ )
1003
1013
lst .append (
1004
1014
dict (
1005
1015
fmt = fmt ,
1006
1016
index = index ,
1007
1017
type = sentinels [end ],
1008
- maxwidth = spec . width or 0 ,
1018
+ maxwidth = initialmaxwidth ,
1009
1019
width = 0 ,
1010
1020
spec = spec ,
1011
1021
)
@@ -1129,7 +1139,7 @@ def print_items(self, items, no_header=False, pre=None, post=None):
1129
1139
post (callable): Function to call after printing each item
1130
1140
"""
1131
1141
# Preprocess original format by processing with filter():
1132
- newfmt = self .filter (items )
1142
+ newfmt = self .filter (items , no_header = no_header )
1133
1143
# Get the current class for creating a new formatter instance:
1134
1144
cls = self .__class__
1135
1145
# Create new instance of the current class from filtered format:
0 commit comments