Robpol86 / terminaltables

Project no longer maintained.
https://github.com/matthewdeanmartin/terminaltables
MIT License
689 stars 70 forks source link

Set table width #29

Open Nekmo opened 8 years ago

Nekmo commented 8 years ago

I have several tables and I'd like all have the same width.

pycharm_django_settings1

My idea is to get the size of the largest and set it on all tables, but there is no method to set the width.

Robpol86 commented 8 years ago

Currently terminaltables doesn't wrap/truncate cells, so it doesn't set widths, widths depend on the strings in cells. Perhaps once I implement https://github.com/Robpol86/terminaltables/issues/5 I'll consider this.

ashwanth10 commented 6 years ago

If too many columns are present, the columns just overwrite on the screen. How about adding a scrollbar to CLI if too many columns come up?

privatwolke commented 6 years ago

This is a quick and dirty solution if the data across tables is somewhat consistent:

def equalize(tables):
    """ Equalizes the columns sizes across multiple tables. """
    lens = defaultdict(lambda: 0)
    for table in tables:
        for row in table:
            for idx, cell in enumerate(row):
                lens[idx] = max(lens[idx], len(cell))

    for tid, table in enumerate(tables):
        for rid, row in enumerate(table):
            for cid, cell in enumerate(row):
                tables[tid][rid][cid] = cell.ljust(lens[cid])

    return tables