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 this statement to your shell (bash) profile:
if [ -z "$UNICODE_VERSION" ] && command -v ucs-detect >/dev/null; then eval "$(ucs-detect)" fi
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:
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 width, by selecting for 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:
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.
If you would like to read more about this tool and related problems I hope to address with this environment variable, have a look at his companion article, https://jeffquast.com/post/terminal_wcwidth_solution/
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file ucs_detect-0.0.4.tar.gz
.
File metadata
- Download URL: ucs_detect-0.0.4.tar.gz
- Upload date:
- Size: 5.5 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 99e718ea9a89c7da520ac318fa596025507431eb219b9a50c78803150e8a5fc1 |
|
MD5 | 7ce1a1df40a00592d9e9fba72421da0f |
|
BLAKE2b-256 | 2b3104accab8194ae3c4a36025b92d47d94bab80ce592154e5db0406deee18e2 |
File details
Details for the file ucs_detect-0.0.4-py2.py3-none-any.whl
.
File metadata
- Download URL: ucs_detect-0.0.4-py2.py3-none-any.whl
- Upload date:
- Size: 5.5 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8a6356923ce48e48c9f8d2305c42fcc5e84359a3622ab3ef599411b422c29208 |
|
MD5 | cb855fd1d276a84c22869baf30fc9fde |
|
BLAKE2b-256 | e788d684ab90fad4dce3f492b190ffafa9c1f2afb3044044d2886cdd629129d9 |