geändert: README.md

geändert:       pyproject.toml
	geändert:       src/text_table/_core.py
This commit is contained in:
2026-02-09 18:57:33 +01:00
parent a622448ea5
commit a03fdeb5df
3 changed files with 30 additions and 8 deletions
+13 -6
View File
@@ -87,9 +87,16 @@ def _get_max_widths(data: Sequence[dict]) -> dict:
max_widths = {}
for row in data:
for col in row:
max_widths[col] = max(len(str(row[col])), max_widths.get(col, 0))
max_widths[col] = max(_len(row[col]), max_widths.get(col, 0))
return max_widths
def _len(obj, /) -> int:
try:
len(obj)
return len(obj)
except:
return len(str(obj))
def table(data: Sequence[dict], *, borders: bool = False, border_charset: TableBorderCharset = BORDER_THIN, column_space: int = 1, uppercase_column_headers: bool = True) -> str:
'''
Creates a text table.
@@ -113,19 +120,19 @@ def table(data: Sequence[dict], *, borders: bool = False, border_charset: TableB
table = f'''{(column_space * ' ').join([(c.upper() if uppercase_column_headers else c) + ' ' * (column_widths[c] - len(c)) for c in columns])}'''
table = ''
for col in columns:
column_widths[col] = max(column_widths[col], len(col))
column_widths[col] = max(column_widths[col], _len(col))
for col in columns:
table += col.upper() if uppercase_column_headers else col
table += ' ' * (column_widths[col] - len(col))
table += ' ' * (column_widths[col] - _len(col))
table += ' ' * column_space
if borders:
table += border_charset.line_vertical + ' '
for row in data:
table += '\n'
for column in columns:
value = str(row.get(column, ''))
table += value
table += ' ' * (column_widths[column] - len(str(value)))
value = row.get(column, '')
table += str(value)
table += ' ' * (column_widths[column] - _len(value))
table += column_space * ' '
if borders:
table += border_charset.line_vertical + ' '