jazzband / prettytable

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

Full-width characters cannot be wrapped correctly. #262

Closed yeruizhi closed 9 months ago

yeruizhi commented 9 months ago

Problem:

I want to put a long sentence in a cell, and it should be wrapped with given max_table_width.

It works well when all characters are half-width characters.

┌──────────────────────────────────────────────────────────────────────────────────┐
│                                      暂放区                                      │
├──────────────────────────────────────────────────────────────────────────────────┤
│ Field 1                                                                          │
├──────────────────────────────────────────────────────────────────────────────────┤
│ abcd, abcd, abcd, abcd, abcd, abcd, abcd, abcd, abcd, abcd, abcd, abcd, abcd,    │
│ abcd, abcd, abcd, abcd, abcd, abcd                                               │
└──────────────────────────────────────────────────────────────────────────────────┘

But when some chinese characters (full-width characters) appeared in the row, the max_table_width cannot warp the line correctly. The sentence was wraped into several lines, but the line-width was not correct.

┌────────────────────────────────────────────────────────────────────────────────────┐
│                                       暂放区                                       │
├────────────────────────────────────────────────────────────────────────────────────┤
│ Field 1                                                                            │
├────────────────────────────────────────────────────────────────────────────────────┤
│ 数学-陈景润, 数学-陈景润, 数学-陈景润, 数学-陈景润, 语文-孔子, 语文-孔子, 英语-莎士比亚, 英语-莎士比亚, 英语-莎士比亚, 英语-莎士比亚, │
│ 物理-牛顿, 物理-牛顿, 化学-门捷列夫, 化学-门捷列夫, 生物-达尔文, 生物-达尔文, 历史-当年明月, 历史-当年明月, 地理-竺可桢, 地理-竺可桢, │
│ 政治-邓小平, 政治-邓小平, 信息-比尔盖茨, 体育-乔丹, 体育-乔丹, 周会                │
└────────────────────────────────────────────────────────────────────────────────────┘

Suggestion:

Support for full-width characters will be nice.

firai commented 9 months ago

Duplicate of #49 and a number of other previously filed issues. I have previously provided an explanation of this at https://github.com/jazzband/prettytable/issues/213#issuecomment-1627747588. In short, this isn't caused by the library but rather by the font(s) that you're using, and there isn't anything that the library can do. You can try to use a monospace font that includes the Chinese character set, such as Sarasa Gothic.

yeruizhi commented 9 months ago

I tried another testcase.

image

In this case above, the row2 had not been wrapped because the code assume row2 (len=50) was shorter than max_width, while row1(len=100) should be wrapped.

Prettytable wrap the string by character number counting, but ignore the fact that "full-width" takes two width while 'half-witdh" tasks one.

I think, it is about wrapping functionality, not about displaying.