def _get_column_widths(self):
"""
returns a dict keyed by column name, values are column widths
"""
column_widths = {}
# Calculate widths for columns
# TODO: cache these values if records don't change between renders
for column in self.columns:
record_max_width = 0
for record in self.records:
if column in record:
r_value = record[column]
if column in self.translations:
r_value = self.translations[column](r_value)
else:
r_value = str(r_value)
record_max_width = max(record_max_width, len(r_value))
record_max_width += 3
# len(column) + 3:
# len(column): space for column header
# +2: left border + space
# +1: space on right of header
column_width = max(record_max_width, len(column) + 3)
column_widths[column] = column_width
# Shrink columns until all fits on screen
# TODO: handling when there's too many columns to render happily
if sum(column_widths.values()) >= curses.COLS:
while sum(column_widths.values()) >= curses.COLS:
key_largest = max(column_widths, key=column_widths.get)
column_widths[key_largest] -= 1
return column_widths
评论列表
文章目录