Skip to main content

Detects the Unicode Version of an interactive terminal for export

Project description

This CLI utility determines the Unicode Version of a terminal, and prints an sh-compatible variable for export:

$ ucs-detect
UNICODE_VERSION=12.0.0; export UNICODE_VERSION

This environment variable is then used by the python wcwidth library, to determine how dependent python programs, such as IPython and others using python-prompt-toolkit render zero-width and wide unicode characters.

If this variable is not exported, the python wcwidth library assumes the latest.

Installation & Usage

From an sh-compatible shell:

pip install -U ucs-detect
eval $(ucs-detect)

To make this automatic, add the eval statement to your shell login profile.

Problem

Chinese, Japanese, Korean, and Emoticon characters are “double-wide”, occupying 2 cells, instead of 1, and some other special characters are “zero-width”.

Any terminal application that formats and displays these characters may have trouble determining how it will be displayed to the end-user. Here is one such example, from vercel/hyper terminal:

An example of misaligned wide characters by the Hyper Terminal

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!

Many languages and libraries continue to conform only to Unicode 5.0, which is the last version of wcwidth.c released by Markus Kuhn in 2007.

Solution

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

This program, ucs-detect, is able to automatically detect the version of unicode that the connecting Terminal supports. 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 return value for by using the given value of the UNICODE_VERSION environment variable.

With this solution, we can correctly determine the UNICODE_VERSION of vercel/hyper terminal as 5.1.0, and the cells that were previously mis-aligned are now aligned correctly:

An example of corrected alignment by Hyper Terminal

How it works

The unicode version is determined using the Query Cursor Position terminal sequence, which asks the terminal emulator “where is the cursor?”.

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

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 Terminal Emulators automatically export the environment variable, UNICODE_VERSION and that this tool would not be required.

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-0.0.1.tar.gz (5.1 kB view details)

Uploaded Source

Built Distributions

ucs_detect-0.0.1-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

ucs_detect-0.0.1-py2.py3-none-any.whl (5.1 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: ucs_detect-0.0.1.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.20.1 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.1

File hashes

Hashes for ucs_detect-0.0.1.tar.gz
Algorithm Hash digest
SHA256 643383ff4b07cedde4b47550b0da889299b42683f7d36d2f4602562e3c66b5fe
MD5 8e8c9d7b3eb6d30c0a845ca73083a6d9
BLAKE2b-256 7bbe90af1949d5293bd1054e54b87617dcadb3a52b749e875fc22d459ea77912

See more details on using hashes here.

File details

Details for the file ucs_detect-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: ucs_detect-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.20.1 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.1

File hashes

Hashes for ucs_detect-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 24beab78d7d8a37e26d4731090911c17cec48748fec160b412ca9849c781425d
MD5 0d832b4725a498aa45b5db5419e10d96
BLAKE2b-256 8a139dafc46d68837244c1d25c42135a2e4ffb37ccf5fe0c153e174ae229a1c1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ucs_detect-0.0.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.20.1 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.1

File hashes

Hashes for ucs_detect-0.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 4136304cb991aba5a61cf3ea262ccd0ccb1c3518db45c7b19fb0f70e0412a481
MD5 60201b4e370206989a8fa74ea87f8f65
BLAKE2b-256 11520b4e71c78f14a73aa618306a89998009562d443b7d940c396b8223de10b8

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