prettytable icon indicating copy to clipboard operation
prettytable copied to clipboard

Option to truncate column headers

Open OlafvdSpek opened this issue 3 years ago • 5 comments

Sometimes column headers are (much) longer than the column data. An option to ignore the width of the header itself would be nice to have.

OlafvdSpek avatar Feb 13 '22 10:02 OlafvdSpek

Please could you give some example input that causes this? And show the current output and what sort of thing you're after.

hugovk avatar Feb 13 '22 19:02 hugovk

#!/usr/bin/python3
from prettytable import PrettyTable

t = PrettyTable()
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.field_names = ['A', 'B', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M']

t.add_row([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
print(t)

# Current:
# +--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
# | 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      |
# +--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+

# Desired:
# +---+---+---+---+---+---+---+---+---+---+----+----+
# | A | B | D | E | F | G | H | I | J | K | L  | M  |
# +---+---+---+---+---+---+---+---+---+---+----+----+
# | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
# +---+---+---+---+---+---+---+---+---+---+----+----+

OlafvdSpek avatar Feb 20 '22 10:02 OlafvdSpek

@OlafvdSpek do you think it would be appropriate to add the option to truncate headers? Maybe something like t.truncate_header['A Field Name'] = True?

av-guy avatar Mar 07 '22 16:03 av-guy

Yes, but I'm not sure about the hardest part, the naming. use_header_widths, ignore_header_widths, something else?

OlafvdSpek avatar Mar 07 '22 16:03 OlafvdSpek

@hugovk I can handle adding this feature. Do you have any input on what a good option name might be?

av-guy avatar Mar 07 '22 17:03 av-guy