prettytable icon indicating copy to clipboard operation
prettytable copied to clipboard

Support joined cells

Open FFY00 opened this issue 4 years ago • 4 comments

Would supporting joining table cells be something you might consider?

I want to make tables like the following:

+-------------+-----------+--------------+---------------+------------------------+------------+
|     byte    |     0     |       1      |       2       |            3           |  4 .. End  |
+-------------+-----------+--------------+---------------+----------+-------------+------------+
|     bit     |                                          |  15 .. 8 |    7 .. 0   |            |
+=============+===========+==============+===============+==========+=============+============+
| description | Report ID | Device Index | Feature Index | Function | Software ID | Parameters |
+-------------+-----------+--------------+---------------+----------+-------------+------------+

My use case is constructing packet structures, where a field might expand over several bytes or bits.

FFY00 avatar Oct 13 '20 19:10 FFY00

It could be useful.

What sort of API do you suggest? Would it be possible to support it for all the output styles?

hugovk avatar Oct 16 '20 11:10 hugovk

Not sure yet, designing an API for this is a bit tricky. I will think about it and try to come back.

FFY00 avatar Oct 16 '20 14:10 FFY00

For ascii output in particular, you could create a new option of empty_vertical_char, similar to vertical_char, except this would be a single character to print between two empty (i.e., None value) fields. The user could then just set it to be a " " space to make it appear empty.

Then when they add rows, they'd do this to get your example row:

x.add_row(["bit", None, None, None, "15 .. 8", "7 .. 0", None])

hadrielk avatar May 30 '22 09:05 hadrielk

Oh wait, no you really do want to "join cells", even if they contain data not just for empty ones. Sorry I didn't notice your top/header row doing that for "3".

Yeah, that's hard.

One way might be to create a new class in prettytable, like for example a JoinedColumn.

The user could add rows like this:

from prettytable import JoinedColumn

x.add_row(["byte", 0, 1, 2, JoinedColumn(width=2, value=3), "4 .. End"])

Internally prettytable would check for that class type, and do what it needs to based on that.

hadrielk avatar May 30 '22 17:05 hadrielk