Robpol86 / terminaltables

Project no longer maintained.
https://github.com/matthewdeanmartin/terminaltables
MIT License
689 stars 70 forks source link

Add API for truncating/fitting/handling long titles. #24

Open Robpol86 opened 8 years ago

Robpol86 commented 8 years ago

Implement table.title_cb which accepts a function/lambda that handles titles that won't fit in the top border.

Function signature expected:

:param str title: The title to overlay on the top border.
:param int max_len: Maximum number of characters that can be shown right now.

:return: Processed title and its length.
:rtype: (str, int)

Must return title that will fit in the border (else the table will appear broken/misaligned/wrapped) as well as the length of the string.

Length values are for visible characters. When using CJK (Chinese/Japanese/Korean) characters some may be len() of 1 but take up 2 visible spaces in terminals. Hence API expects function to return title length in case the function truncates the string and the last character is one short of the maximum width available due to it being a CJK character.

e.g. terminal with 7 spaces available for the title and the title is "世界你好" That's 8 visible spaces so if the function truncates it to "世界你" API expects length of 6 to be returned.