jazzband / prettytable

Display tabular data in a visually appealing ASCII table format
https://pypi.org/project/PrettyTable/
Other
1.36k stars 155 forks source link

max_table_width not handled for headers #170

Closed OlafvdSpek closed 2 years ago

OlafvdSpek commented 2 years ago

Noticed max_table_width in the code but didn't see it @ https://github.com/jazzband/prettytable/blob/master/README.md Is it undocumented?

Headers don't appear to handle max_table_width properly.

from prettytable import PrettyTable
t = PrettyTable()
t.max_table_width = 160
t.field_names = ['A Field Name', 'B Field Name', 'D Field Name', 'E Field Name', 'F Field Name', 'G Field Name', 'H Field Name', 'I Field Name', 'J Field Name', 'K Field Name', 'L Field Name', 'M Field Name']
t.add_row([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
print(t)
+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+
| A Field Name | B Field Name | D Field Name | E Field Name | F Field Name | G Field Name | H Field Name | I Field Name | J Field Name | K Field Name | L Field Name | M Field Name |
+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+
|      0      |      1      |      2      |      3      |      4      |      5      |      6      |      7      |      8      |      9      |      10     |      11     |
+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+
hugovk commented 2 years ago

It is documented in a docstring but not README:

        min_table_width - minimum desired table width, in characters
        max_table_width - maximum desired table width, in characters

It was added in https://github.com/jazzband/prettytable/commit/1001f0e94be2269ce02909f1428487882b2916e7 in 2013.

I'd welcome PRs to document them in README and to fix it for headers. It may be that min_table_width doesn't handle headers either.

OlafvdSpek commented 2 years ago

This is interesting as well:

t = PrettyTable()
t.max_table_width = 20
t.add_row([0, 1, 2, 3, 4, 5, 6])
print(len(t.get_string().split('\n')[0]))
print(t)
36
+----+----+----+----+----+----+----+
| Fi | Fi | Fi | Fi | Fi | Fi | Fi |
+----+----+----+----+----+----+----+
| 0  | 1  | 2  | 3  | 4  | 5  | 6  |
+----+----+----+----+----+----+----+