astanin / python-tabulate

Pretty-print tabular data in Python, a library and a command-line utility. Repository migrated from bitbucket.org/astanin/python-tabulate.
https://pypi.org/project/tabulate/
MIT License
2.15k stars 164 forks source link

support for `shorten`? #171

Open NewUserHa opened 2 years ago

NewUserHa commented 2 years ago

for a table that contains only a few lines that have very long text, like:

>>> print(tabulate(table, headers, tablefmt="github"))
| item   | qty   |
|--------|-------|
| spam abcabcabcabcabcabcabcabcabcabcabc   | 42    |
| eggs   | 451   |
| bacon  | 0     |

to

>>> print(tabulate(table, headers, tablefmt="github"))
| item   | qty   |
|--------|-------|
| spam abca...   | 42    |
| eggs   | 451   |
| bacon  | 0     |

since https://github.com/astanin/python-tabulate#automating-multilines already uses https://docs.python.org/3/library/textwrap.html#textwrap.wrap, probably could use https://docs.python.org/3/library/textwrap.html#textwrap.shorten?

astanin commented 2 years ago

I may add an option to tabulate which allows to specify a custom formatter function for all or some values. In your case you would be able to supply a function which shortens strings.

Would it work for you?

Related: #170

NewUserHa commented 2 years ago

the most common cases may be ... that .shorten provided. the other left may need a custom function I guess