Skip to content

Better Unicode terminal support detection #437

@lichray

Description

@lichray

A terminal can use Unicode characters doesn't mean it has to be able to decode UTF-* encoded data, it merely means that you can encode your Unicode characters and the terminal an recognize that encoding. For example, GB18030 is a Unicode 5.0 implementation, but itself is nether UTF-8 or UTF-16. And many legacy encodings support the charset required by tqdm, but the currently _supports_unicode doesn't take this in account.

A visual difference is that pip displays progress bar nicely on my terminal (LC_CTYPE=GBK) but tqdm only displays #. You can find out how pip detects Unicode terminal support in pip/utils/ui.py:_select_progress_class.

And here is how I adopt pip's approach for fixing tqdm in my code:

def _has_unicode(fp):
    try:
        u'\u2588\u2589'.encode(fp.encoding)
    except (UnicodeEncodeError, AttributeError):
        return False
    else:
        return True

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions