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.1k stars 163 forks source link

Table with hyperlink text with space and maxcolwidths not working #245

Open bendanye opened 1 year ago

bendanye commented 1 year ago

Hi there,

I was trying to create a table with clickable text inside the table. Not sure if i did anything wrong.

My expected result:

+----------------+--------+---------------+
| This is a link | 696000 |    1.9891e+09 |
+----------------+--------+---------------+
| This is a link |   6371 | 5973.6        |
+----------------+--------+---------------+
| This is a link |   1737 |   73.5        |
+----------------+--------+---------------+
| This is a link |   3390 |  641.85       |
+----------------+--------+---------------+

Actual result:

+----------------------+--------+---------------+
| This is        |        |               |
| alink         |        |               |
+----------------------+--------+---------------+
| This is        |        |               |
| alink         |        |               |
+----------------------+--------+---------------+
| This is        |        |               |
| alink         |        |               |
+----------------------+--------+---------------+
| This is        |        |               |
| alink         |        |               |
+----------------------+--------+---------------+

Sample Codes

from tabulate import tabulate

def clickable(text: str, target: str):
    return "\x1b]8;;https://example.com\x1b\\This is a link\x1b]8;;\x1b\\"

table = [
    [clickable("test abc", "http://example.org"), 696000, 1989100000],
    [clickable("test bbb", "http://example.org"), 6371, 5973.6],
    [clickable("test ccc", "http://example.org"), 1737, 73.5],
    [clickable("test dddd", "http://example.org"), 3390, 641.85],
]

print(tabulate(table, tablefmt="grid", maxcolwidths=[20, 20, 20])),