README.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. |pypi_downloads| |codecov| |license|
  2. ============
  3. Introduction
  4. ============
  5. This library is mainly for CLI programs that carefully produce output for
  6. Terminals, or make pretend to be an emulator.
  7. **Problem Statement**: The printable length of *most* strings are equal to the
  8. number of cells they occupy on the screen ``1 character : 1 cell``. However,
  9. there are categories of characters that *occupy 2 cells* (full-wide), and
  10. others that *occupy 0* cells (zero-width).
  11. **Solution**: POSIX.1-2001 and POSIX.1-2008 conforming systems provide
  12. `wcwidth(3)`_ and `wcswidth(3)`_ C functions of which this python module's
  13. functions precisely copy. *These functions return the number of cells a
  14. unicode string is expected to occupy.*
  15. Installation
  16. ------------
  17. The stable version of this package is maintained on pypi, install using pip::
  18. pip install wcwidth
  19. Example
  20. -------
  21. **Problem**: given the following phrase (Japanese),
  22. >>> text = u'コンニチハ'
  23. Python **incorrectly** uses the *string length* of 5 codepoints rather than the
  24. *printable length* of 10 cells, so that when using the `rjust` function, the
  25. output length is wrong::
  26. >>> print(len('コンニチハ'))
  27. 5
  28. >>> print('コンニチハ'.rjust(20, '_'))
  29. _______________コンニチハ
  30. By defining our own "rjust" function that uses wcwidth, we can correct this::
  31. >>> def wc_rjust(text, length, padding=' '):
  32. ... from wcwidth import wcswidth
  33. ... return padding * max(0, (length - wcswidth(text))) + text
  34. ...
  35. Our **Solution** uses wcswidth to determine the string length correctly::
  36. >>> from wcwidth import wcswidth
  37. >>> print(wcswidth('コンニチハ'))
  38. 10
  39. >>> print(wc_rjust('コンニチハ', 20, '_'))
  40. __________コンニチハ
  41. Choosing a Version
  42. ------------------
  43. Export an environment variable, ``UNICODE_VERSION``. This should be done by
  44. *terminal emulators* or those developers experimenting with authoring one of
  45. their own, from shell::
  46. $ export UNICODE_VERSION=13.0
  47. If unspecified, the latest version is used. If your Terminal Emulator does not
  48. export this variable, you can use the `jquast/ucs-detect`_ utility to
  49. automatically detect and export it to your shell.
  50. wcwidth, wcswidth
  51. -----------------
  52. Use function ``wcwidth()`` to determine the length of a *single unicode
  53. character*, and ``wcswidth()`` to determine the length of many, a *string
  54. of unicode characters*.
  55. Briefly, return values of function ``wcwidth()`` are:
  56. ``-1``
  57. Indeterminate (not printable).
  58. ``0``
  59. Does not advance the cursor, such as NULL or Combining.
  60. ``2``
  61. Characters of category East Asian Wide (W) or East Asian
  62. Full-width (F) which are displayed using two terminal cells.
  63. ``1``
  64. All others.
  65. Function ``wcswidth()`` simply returns the sum of all values for each character
  66. along a string, or ``-1`` when it occurs anywhere along a string.
  67. Full API Documentation at https://wcwidth.readthedocs.org
  68. ==========
  69. Developing
  70. ==========
  71. Install wcwidth in editable mode::
  72. pip install -e .
  73. Execute unit tests using tox_::
  74. tox -e py27,py35,py36,py37,py38,py39,py310,py311,py312
  75. Updating Unicode Version
  76. ------------------------
  77. Regenerate python code tables from latest Unicode Specification data files::
  78. tox -e update
  79. The script is located at ``bin/update-tables.py``, requires Python 3.9 or
  80. later. It is recommended but not necessary to run this script with the newest
  81. Python, because the newest Python has the latest ``unicodedata`` for generating
  82. comments.
  83. Building Documentation
  84. ----------------------
  85. This project is using `sphinx`_ 4.5 to build documentation::
  86. tox -e sphinx
  87. The output will be in ``docs/_build/html/``.
  88. Updating Requirements
  89. ---------------------
  90. This project is using `pip-tools`_ to manage requirements.
  91. To upgrade requirements for updating unicode version, run::
  92. tox -e update_requirements_update
  93. To upgrade requirements for testing, run::
  94. tox -e update_requirements37,update_requirements39
  95. To upgrade requirements for building documentation, run::
  96. tox -e update_requirements_docs
  97. Utilities
  98. ---------
  99. Supplementary tools for browsing and testing terminals for wide unicode
  100. characters are found in the `bin/`_ of this project's source code. Just ensure
  101. to first ``pip install -r requirements-develop.txt`` from this projects main
  102. folder. For example, an interactive browser for testing::
  103. python ./bin/wcwidth-browser.py
  104. ====
  105. Uses
  106. ====
  107. This library is used in:
  108. - `jquast/blessed`_: a thin, practical wrapper around terminal capabilities in
  109. Python.
  110. - `prompt-toolkit/python-prompt-toolkit`_: a Library for building powerful
  111. interactive command lines in Python.
  112. - `dbcli/pgcli`_: Postgres CLI with autocompletion and syntax highlighting.
  113. - `thomasballinger/curtsies`_: a Curses-like terminal wrapper with a display
  114. based on compositing 2d arrays of text.
  115. - `selectel/pyte`_: Simple VTXXX-compatible linux terminal emulator.
  116. - `astanin/python-tabulate`_: Pretty-print tabular data in Python, a library
  117. and a command-line utility.
  118. - `rspeer/python-ftfy`_: Fixes mojibake and other glitches in Unicode
  119. text.
  120. - `nbedos/termtosvg`_: Terminal recorder that renders sessions as SVG
  121. animations.
  122. - `peterbrittain/asciimatics`_: Package to help people create full-screen text
  123. UIs.
  124. - `python-cmd2/cmd2`_: A tool for building interactive command line apps
  125. - `stratis-storage/stratis-cli`_: CLI for the Stratis project
  126. - `ihabunek/toot`_: A Mastodon CLI/TUI client
  127. - `saulpw/visidata`_: Terminal spreadsheet multitool for discovering and
  128. arranging data
  129. ===============
  130. Other Languages
  131. ===============
  132. - `timoxley/wcwidth`_: JavaScript
  133. - `janlelis/unicode-display_width`_: Ruby
  134. - `alecrabbit/php-wcwidth`_: PHP
  135. - `Text::CharWidth`_: Perl
  136. - `bluebear94/Terminal-WCWidth`_: Perl 6
  137. - `mattn/go-runewidth`_: Go
  138. - `grepsuzette/wcwidth`_: Haxe
  139. - `aperezdc/lua-wcwidth`_: Lua
  140. - `joachimschmidt557/zig-wcwidth`_: Zig
  141. - `fumiyas/wcwidth-cjk`_: `LD_PRELOAD` override
  142. - `joshuarubin/wcwidth9`_: Unicode version 9 in C
  143. =======
  144. History
  145. =======
  146. 0.2.13 *2024-01-06*
  147. * **Bugfix** zero-width support for Hangul Jamo (Korean)
  148. 0.2.12 *2023-11-21*
  149. * re-release to remove .pyi file misplaced in wheel files `Issue #101`_.
  150. 0.2.11 *2023-11-20*
  151. * Include tests files in the source distribution (`PR #98`_, `PR #100`_).
  152. 0.2.10 *2023-11-13*
  153. * **Bugfix** accounting of some kinds of emoji sequences using U+FE0F
  154. Variation Selector 16 (`PR #97`_).
  155. * **Updated** `Specification <Specification_from_pypi_>`_.
  156. 0.2.9 *2023-10-30*
  157. * **Bugfix** zero-width characters used in Emoji ZWJ sequences, Balinese,
  158. Jamo, Devanagari, Tamil, Kannada and others (`PR #91`_).
  159. * **Updated** to include `Specification <Specification_from_pypi_>`_ of
  160. character measurements.
  161. 0.2.8 *2023-09-30*
  162. * Include requirements files in the source distribution (`PR #82`_).
  163. 0.2.7 *2023-09-28*
  164. * **Updated** tables to include Unicode Specification 15.1.0.
  165. * Include ``bin``, ``docs``, and ``tox.ini`` in the source distribution
  166. 0.2.6 *2023-01-14*
  167. * **Updated** tables to include Unicode Specification 14.0.0 and 15.0.0.
  168. * **Changed** developer tools to use pip-compile, and to use jinja2 templates
  169. for code generation in `bin/update-tables.py` to prepare for possible
  170. compiler optimization release.
  171. 0.2.1 .. 0.2.5 *2020-06-23*
  172. * **Repository** changes to update tests and packaging issues, and
  173. begin tagging repository with matching release versions.
  174. 0.2.0 *2020-06-01*
  175. * **Enhancement**: Unicode version may be selected by exporting the
  176. Environment variable ``UNICODE_VERSION``, such as ``13.0``, or ``6.3.0``.
  177. See the `jquast/ucs-detect`_ CLI utility for automatic detection.
  178. * **Enhancement**:
  179. API Documentation is published to readthedocs.org.
  180. * **Updated** tables for *all* Unicode Specifications with files
  181. published in a programmatically consumable format, versions 4.1.0
  182. through 13.0
  183. 0.1.9 *2020-03-22*
  184. * **Performance** optimization by `Avram Lubkin`_, `PR #35`_.
  185. * **Updated** tables to Unicode Specification 13.0.0.
  186. 0.1.8 *2020-01-01*
  187. * **Updated** tables to Unicode Specification 12.0.0. (`PR #30`_).
  188. 0.1.7 *2016-07-01*
  189. * **Updated** tables to Unicode Specification 9.0.0. (`PR #18`_).
  190. 0.1.6 *2016-01-08 Production/Stable*
  191. * ``LICENSE`` file now included with distribution.
  192. 0.1.5 *2015-09-13 Alpha*
  193. * **Bugfix**:
  194. Resolution of "combining_ character width" issue, most especially
  195. those that previously returned -1 now often (correctly) return 0.
  196. resolved by `Philip Craig`_ via `PR #11`_.
  197. * **Deprecated**:
  198. The module path ``wcwidth.table_comb`` is no longer available,
  199. it has been superseded by module path ``wcwidth.table_zero``.
  200. 0.1.4 *2014-11-20 Pre-Alpha*
  201. * **Feature**: ``wcswidth()`` now determines printable length
  202. for (most) combining_ characters. The developer's tool
  203. `bin/wcwidth-browser.py`_ is improved to display combining_
  204. characters when provided the ``--combining`` option
  205. (`Thomas Ballinger`_ and `Leta Montopoli`_ `PR #5`_).
  206. * **Feature**: added static analysis (prospector_) to testing
  207. framework.
  208. 0.1.3 *2014-10-29 Pre-Alpha*
  209. * **Bugfix**: 2nd parameter of wcswidth was not honored.
  210. (`Thomas Ballinger`_, `PR #4`_).
  211. 0.1.2 *2014-10-28 Pre-Alpha*
  212. * **Updated** tables to Unicode Specification 7.0.0.
  213. (`Thomas Ballinger`_, `PR #3`_).
  214. 0.1.1 *2014-05-14 Pre-Alpha*
  215. * Initial release to pypi, Based on Unicode Specification 6.3.0
  216. This code was originally derived directly from C code of the same name,
  217. whose latest version is available at
  218. https://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c::
  219. * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
  220. *
  221. * Permission to use, copy, modify, and distribute this software
  222. * for any purpose and without fee is hereby granted. The author
  223. * disclaims all warranties with regard to this software.
  224. .. _`Specification_from_pypi`: https://wcwidth.readthedocs.io/en/latest/specs.html
  225. .. _`tox`: https://tox.wiki/en/latest/
  226. .. _`prospector`: https://github.com/landscapeio/prospector
  227. .. _`combining`: https://en.wikipedia.org/wiki/Combining_character
  228. .. _`bin/`: https://github.com/jquast/wcwidth/tree/master/bin
  229. .. _`bin/wcwidth-browser.py`: https://github.com/jquast/wcwidth/blob/master/bin/wcwidth-browser.py
  230. .. _`Thomas Ballinger`: https://github.com/thomasballinger
  231. .. _`Leta Montopoli`: https://github.com/lmontopo
  232. .. _`Philip Craig`: https://github.com/philipc
  233. .. _`PR #3`: https://github.com/jquast/wcwidth/pull/3
  234. .. _`PR #4`: https://github.com/jquast/wcwidth/pull/4
  235. .. _`PR #5`: https://github.com/jquast/wcwidth/pull/5
  236. .. _`PR #11`: https://github.com/jquast/wcwidth/pull/11
  237. .. _`PR #18`: https://github.com/jquast/wcwidth/pull/18
  238. .. _`PR #30`: https://github.com/jquast/wcwidth/pull/30
  239. .. _`PR #35`: https://github.com/jquast/wcwidth/pull/35
  240. .. _`PR #82`: https://github.com/jquast/wcwidth/pull/82
  241. .. _`PR #91`: https://github.com/jquast/wcwidth/pull/91
  242. .. _`PR #97`: https://github.com/jquast/wcwidth/pull/97
  243. .. _`PR #98`: https://github.com/jquast/wcwidth/pull/98
  244. .. _`PR #100`: https://github.com/jquast/wcwidth/pull/100
  245. .. _`Issue #101`: https://github.com/jquast/wcwidth/issues/101
  246. .. _`jquast/blessed`: https://github.com/jquast/blessed
  247. .. _`selectel/pyte`: https://github.com/selectel/pyte
  248. .. _`thomasballinger/curtsies`: https://github.com/thomasballinger/curtsies
  249. .. _`dbcli/pgcli`: https://github.com/dbcli/pgcli
  250. .. _`prompt-toolkit/python-prompt-toolkit`: https://github.com/prompt-toolkit/python-prompt-toolkit
  251. .. _`timoxley/wcwidth`: https://github.com/timoxley/wcwidth
  252. .. _`wcwidth(3)`: https://man7.org/linux/man-pages/man3/wcwidth.3.html
  253. .. _`wcswidth(3)`: https://man7.org/linux/man-pages/man3/wcswidth.3.html
  254. .. _`astanin/python-tabulate`: https://github.com/astanin/python-tabulate
  255. .. _`janlelis/unicode-display_width`: https://github.com/janlelis/unicode-display_width
  256. .. _`rspeer/python-ftfy`: https://github.com/rspeer/python-ftfy
  257. .. _`alecrabbit/php-wcwidth`: https://github.com/alecrabbit/php-wcwidth
  258. .. _`Text::CharWidth`: https://metacpan.org/pod/Text::CharWidth
  259. .. _`bluebear94/Terminal-WCWidth`: https://github.com/bluebear94/Terminal-WCWidth
  260. .. _`mattn/go-runewidth`: https://github.com/mattn/go-runewidth
  261. .. _`grepsuzette/wcwidth`: https://github.com/grepsuzette/wcwidth
  262. .. _`jquast/ucs-detect`: https://github.com/jquast/ucs-detect
  263. .. _`Avram Lubkin`: https://github.com/avylove
  264. .. _`nbedos/termtosvg`: https://github.com/nbedos/termtosvg
  265. .. _`peterbrittain/asciimatics`: https://github.com/peterbrittain/asciimatics
  266. .. _`aperezdc/lua-wcwidth`: https://github.com/aperezdc/lua-wcwidth
  267. .. _`joachimschmidt557/zig-wcwidth`: https://github.com/joachimschmidt557/zig-wcwidth
  268. .. _`fumiyas/wcwidth-cjk`: https://github.com/fumiyas/wcwidth-cjk
  269. .. _`joshuarubin/wcwidth9`: https://github.com/joshuarubin/wcwidth9
  270. .. _`python-cmd2/cmd2`: https://github.com/python-cmd2/cmd2
  271. .. _`stratis-storage/stratis-cli`: https://github.com/stratis-storage/stratis-cli
  272. .. _`ihabunek/toot`: https://github.com/ihabunek/toot
  273. .. _`saulpw/visidata`: https://github.com/saulpw/visidata
  274. .. _`pip-tools`: https://pip-tools.readthedocs.io/
  275. .. _`sphinx`: https://www.sphinx-doc.org/
  276. .. |pypi_downloads| image:: https://img.shields.io/pypi/dm/wcwidth.svg?logo=pypi
  277. :alt: Downloads
  278. :target: https://pypi.org/project/wcwidth/
  279. .. |codecov| image:: https://codecov.io/gh/jquast/wcwidth/branch/master/graph/badge.svg
  280. :alt: codecov.io Code Coverage
  281. :target: https://app.codecov.io/gh/jquast/wcwidth/
  282. .. |license| image:: https://img.shields.io/pypi/l/wcwidth.svg
  283. :target: https://pypi.org/project/wcwidth/
  284. :alt: MIT License