Skip to main content

Detects Unicode support of an interactive terminal

Project description

ucs-detect

Without any arguments,

$ ucs-detect

The tool, ucs-detect, tests the Unicode support level of the terminal emulator for Wide character, Emoji sequence, and Language support and displays a brief summary of the results. Add –save-yaml argument to store a detailed report.

video demonstration of executing ucs-detect

Versions of ucs-detect prior to 1.0 served only a single purpose, to export an sh-compatible line for export of UNICODE_VERSION. To continue this purpose, use --shell --quick, for example:

$ ucs-detect --shell --quick
UNICODE_VERSION=15.0.0; export UNICODE_VERSION

Test Results

Popular terminals were tested using this program and their results were collated at https://ucs-detect.readthedocs.io/results.html

Installation & Usage

To install:

$ pip install -U ucs-detect

To use:

$ ucs-detect

To create a yaml data file result:

$ ucs-detect --save-yaml my-terminal.yaml --limit-code points=5000 --limit-words=5000 --limit-errors=500

UNICODE_VERSION

The environment variable, UNICODE_VERSION is used by the python wcwidth library, which contains every past unicode table version, to determine how dependent python programs, such as IPython render wide and zero-width characters.

Create sh-compatible line for export:

$ ucs-detect --shell --quick
UNICODE_VERSION=15.0.0; export UNICODE_VERSION

$ eval "$(ucs-detect --quick --shell)"
$ echo $UNICODE_VERSION
15.0.0

Problem

Chinese, Japanese, Korean, and Emoticon characters are “double-wide”, occupying 2 cells, instead of 1, and some other special characters are “zero-width”, which do not occopy any cells at all, or modify the previous cell as a “combining” character.

A terminal application that displays these characters may have trouble determining how it will be displayed to the end-user. This problem happens often, because the Unicode Consortium releases new versions of the Unicode Standard periodically, but the source code of libraries and applications are not updated at the same time, or at all!

Solution

The most important factor is to determine: What version of unicode is the Terminal using?

This program, ucs-detect, is able to automatically detect the version of unicode that the connecting Terminal supports for WIDE characters. The python wcwidth library supports all Unicode versions, 4.1.0 through 12.1.0 at time of this writing, and so it is able to select and match the correct width, by selecting for the given value of the UNICODE_VERSION environment variable.

How it works

The unicode version is determined using the Query Cursor Position terminal sequence, which asks, “where is the cursor?” using a special sequence, and conforming terminals reply.

By displaying a series of Wide Unicode characters for each Unicode version expected to advance the cursor by 2 cells, the very last version that successfully advances 2 cells determines the version of Unicode supported by the Terminal.

This solution of using Query Cursor Position and exporting an sh variable is precisely the same solution used by the resize(1) program distributed with X11, which determines the terminal size over transports that are not capable of communicating or forwarding it (such as over a serial line).

Further

I hope that this CLI tool is provisional! I’d like to see all Terminals automatically export the environment variable, UNICODE_VERSION and that this tool would not be required.

If you would like to read more about this tool and the related problems I hope to address with the UNICODE_VERSION environment variable, have a look at this companion article, https://www.jeffquast.com/post/terminal_wcwidth_solution/

History

  • 1.0.4 (2023-11-13): Add support for Emoji with VS-16 and more complete testing. Published test results.

  • 1.0.3 (2023-10-28): Drop python 2 support. Add more advanced testing. Changes default behavior when called without arguments, use ucs-detect --quick --shell to use the new release with matching previous release behavior.

  • 0.0.4 (2020-06-20): Initial releases and bugfixes

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ucs_detect-1.0.4.tar.gz (51.6 kB view details)

Uploaded Source

Built Distribution

ucs_detect-1.0.4-py2.py3-none-any.whl (47.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file ucs_detect-1.0.4.tar.gz.

File metadata

  • Download URL: ucs_detect-1.0.4.tar.gz
  • Upload date:
  • Size: 51.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for ucs_detect-1.0.4.tar.gz
Algorithm Hash digest
SHA256 e4f9d016c1fd5db2710c37db1dea9fee0a9886356f502497e341c38f1b80cf99
MD5 b3cd3e17ad7ec9ee2ad0fc4f68373b0a
BLAKE2b-256 40f1266a872f1be5bfacaf5bf09164a93b9b3a79884787eab1c5ba63f97dc0d8

See more details on using hashes here.

File details

Details for the file ucs_detect-1.0.4-py2.py3-none-any.whl.

File metadata

  • Download URL: ucs_detect-1.0.4-py2.py3-none-any.whl
  • Upload date:
  • Size: 47.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for ucs_detect-1.0.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 66c5f1dc080ad56103d037e2099e19d670b921d0d84714993dc80931e3cc9848
MD5 b6547ee7a772d2ef77c3c3d4cc15e48e
BLAKE2b-256 d9da779a8d487a84092958d0eb87f8e4c6726897b7097dc3d8278939b0b9da0f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page