This API is mainly for Terminal Emulator implementors -- any python program that attempts to determine the printable width of a string on a Terminal. It is implemented in python (no C library calls) and has no 3rd-party dependencies.
It is certainly possible to use your Operating System's wcwidth(3)
and
wcswidth(3)
calls if it is POSIX-conforming, but this would not be possible
on non-POSIX platforms, such as Windows, or for alternative Python
implementations, such as jython. It is also commonly many releases older
than the most current Unicode Standard release files, which this project
aims to track.
The most current release of this API is based from Unicode Standard release
7.0.0, dated 2014-02-28, 23:15:00 GMT [KW, LI] for table generated by
file EastAsianWidth-7.0.0.txt
and 2014-02-07, 18:42:08 GMT [MD] for
DerivedCombiningClass-7.0.0.txt
.
The stable version of this package is maintained on pypi, install using pip:
pip install wcwidth
You may have noticed some characters especially Chinese, Japanese, and
Korean (collectively known as the CJK Unified Ideographs) consume more
than 1 terminal cell. If you ask for the length of the string, u'コンニチハ'
(Japanese: Hello), it is correctly determined to be a length of 5 using
the len()
built-in.
However, if you were to print this to a Terminal Emulator, such as xterm,
urxvt, Terminal.app, PuTTY, or iTerm2, it would consume 10 cells (columns).
This causes problems for many of the text-alignment functions, such as rjust()
.
On an 80-wide terminal, the following would wrap along the margin, instead
of displaying it right-aligned as desired:
>>> text = u'コンニチハ' >>> print(text.rjust(80)) コン ニチハ
This API allows one to determine the printable length of these strings,
that the length of wcwidth(u'コ')
is reported as 2
, and
wcswidth(u'コンニチハ')
as 10
.
This allows one to determine the printable effects of displaying CJK characters on a terminal emulator.
Use wcwidth
to determine the length of a single character,
and wcswidth
to determine the length of a string of characters.
To Display u'コンニチハ'
right-adjusted on screen of 80 columns:
>>> from wcwidth import wcswidth >>> text = u'コンニチハ' >>> print(u' ' * (80 - wcswidth(text)) + text) コンニチハ
A general overview of return values:
-1
: indeterminate (see Todo).0
: do not advance the cursor, such as NULL.2
: East_Asian_Width property values W and F (Wide and Full-width).1
: all others.
wcswidth()
simply returns the sum of all values along a string, or
-1
if it has occurred for any value returned by wcwidth()
. A more
exacting list of conditions and return values may be found in the docstring
for wcwidth()
.
There may be discrepancies with the determined printable width of of characters
by wcwidth and the results of any given terminal emulator -- most commonly,
emulators are using your Operating System's wcwidth(3)
implementation which
is often based on tables much older than the most current Unicode Specification.
Python's determination of non-zero combining characters may also be based on an
older specification.
You may determine an exacting list of these discrepancies using files wcwidth-libc-comparator.py and wcwidth-combining-comparator.py
Execute the command python setup.py develop
to prepare an environment
for running tests (python setup.py test
), updating tables (
python setup.py update
) or using any of the scripts in the bin/
sub-folder. These files are only made available in the source repository.
The command python setup.py update
will fetch the following resources:
- http://www.unicode.org/Public/UNIDATA/EastAsianWidth.txt
- http://www.unicode.org/Public/UNIDATA/extracted/DerivedCombiningClass.txt
And generate the table files wcwidth/table_wide.py and wcwidth/table_comb.py.
This code was originally derived directly from C code of the same name, whose latest version is available at: wcwidth.c And is authored by Markus Kuhn -- 2007-05-26 (Unicode 5.0)
This library is used in:
- jquast/blessed, a simplified wrapper around curses.
- jonathanslenders/python-prompt-toolkit, a Library for building powerful interactive command lines in Python.
Additional tools for displaying and testing wcwidth is found in the bin/
folder of this project (github link: wcwidth/bin). They are not distributed
as a script or part of the module.
Though some of the most common ("zero-width") combining characters are understood by wcswidth, there are still many edge cases that need to be covered, especially certain kinds of sequences such as those containing Control-Sequence-Inducer (CSI).
The original license is as follows:
Permission to use, copy, modify, and distribute this software for any purpose and without fee is hereby granted. The author disclaims all warranties with regard to this software.
No specific licensing is specified, and Mr. Kuhn resides in the UK which allows some protection from Copyrighting. As this derivative is based on US Soil, an OSI-approved license that appears most-alike has been chosen, the MIT license:
The MIT License (MIT) Copyright (c) 2014 <[email protected]> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- 0.1.4
- Feature:
wcswidth()
now determines printable length for (most) combining characters. The developer's tool bin/wcwidth-browser.py is improved to display combining characters when provided the--combining
option (Thomas Ballinger and Leta Montopoli PR #5). - added static analysis (prospector) to testing framework.
- Feature:
- 0.1.3
- Bugfix: 2nd parameter of wcswidth was not honored. (Thomas Ballinger, PR #4).
- 0.1.2
- Updated tables to Unicode Specification 7.0.0. (Thomas Ballinger, PR #3).
- 0.1.1
- Initial release to pypi, Based on Unicode Specification 6.3.0