Skip to content

[Suggestion] Text width (string length without sequences). #53

@rysson

Description

@rysson

Yours lib is awesome, I used it for years.
Now I have a little problem, a have fine formatting and print table separated.

Is any way to find printable text width to calculate table cell width?
If not I suggest add a function to count length of the text without sequences.

Of course I can add some regex to transform text before taking its length but it'll be nice to have it out of the box.


EDIT: some example

rx_omit_sequences = re.compile(r'(?:\033\[|\x9b)[0-?]*[!-/]*[@-~]')

def text_width(s: str) -> int:
    return len(rx_omit_sequences.sub('', s))

s = f'{fg.red}This is red text!{fg.rs}'
print('-' * len(s))
print(s)
print('-' * text_width(s))

20240111-132352-231x50

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions