terminaltables icon indicating copy to clipboard operation
terminaltables copied to clipboard

Set table width

Open Nekmo opened this issue 8 years ago • 3 comments

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.

Nekmo avatar May 28 '16 01:05 Nekmo

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.

Robpol86 avatar May 28 '16 01:05 Robpol86

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?

ashwanth10 avatar Nov 30 '17 04:11 ashwanth10

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

privatwolke avatar Jul 26 '18 09:07 privatwolke