Skip to content

Instantly share code, notes, and snippets.

@jelmervdl
Created July 28, 2023 10:24
Show Gist options
  • Save jelmervdl/3b45af92aaf91b75c3040a7f2824b973 to your computer and use it in GitHub Desktop.
Save jelmervdl/3b45af92aaf91b75c3040a7f2824b973 to your computer and use it in GitHub Desktop.
def print_cells(*rows, format:Callable[[Any],str]=repr,file=None) -> None:
# Convert it all to text
text_rows = [
[format(cell) for cell in row]
for row in rows
]
# Calculate column width
widths = [
max(len(cell) for cell in column)
for column in zip(*text_rows)
]
# Print rows
for row in text_rows:
for width, cell in zip(widths, row):
print(f'{{:<{width:d}}} '.format(cell), end='', file=file)
print(file=file)
print_cells([(1,2), (3,4), (5,6)])
# prints
# 1 3 5
# 2 4 6
@jelmervdl
Copy link
Author

Do yourself (myself, once I read this in the future) and use https://pypi.org/project/wcwidth/.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment