You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
7.3 KiB

4 years ago
  1. .. image:: https://img.shields.io/travis/jquast/wcwidth.svg
  2. :target: https://travis-ci.org/jquast/wcwidth
  3. :alt: Travis Continous Integration
  4. .. image:: https://img.shields.io/coveralls/jquast/wcwidth.svg
  5. :target: https://coveralls.io/r/jquast/wcwidth
  6. :alt: Coveralls Code Coverage
  7. .. image:: https://img.shields.io/pypi/v/wcwidth.svg
  8. :target: https://pypi.python.org/pypi/wcwidth/
  9. :alt: Latest Version
  10. .. image:: https://img.shields.io/github/license/jquast/wcwidth.svg
  11. :target: https://pypi.python.org/pypi/wcwidth/
  12. :alt: License
  13. .. image:: https://img.shields.io/pypi/wheel/wcwidth.svg
  14. :alt: Wheel Status
  15. .. image:: https://img.shields.io/pypi/dm/wcwidth.svg
  16. :target: https://pypi.python.org/pypi/wcwidth/
  17. :alt: Downloads
  18. ============
  19. Introduction
  20. ============
  21. This Library is mainly for those implementing a Terminal Emulator, or programs
  22. that carefully produce output to be interpreted by one.
  23. **Problem Statement**: When printed to the screen, the length of the string is
  24. usually equal to the number of cells it occupies. However, there are
  25. categories of characters that occupy 2 cells (full-wide), and others that
  26. occupy 0.
  27. **Solution**: POSIX.1-2001 and POSIX.1-2008 conforming systems provide
  28. `wcwidth(3)`_ and `wcswidth(3)`_ C functions of which this python module's
  29. functions precisely copy. *These functions return the number of cells a
  30. unicode string is expected to occupy.*
  31. This library aims to be forward-looking, portable, and most correct. The most
  32. current release of this API is based on the Unicode Standard release files:
  33. ``DerivedGeneralCategory-9.0.0.txt``
  34. *Date: 2016-06-01, 10:34:26 GMT*
  35. © 2016 Unicode®, Inc.
  36. ``EastAsianWidth-9.0.0.txt``
  37. *Date: 2016-05-27, 17:00:00 GMT [KW, LI]*
  38. © 2016 Unicode®, Inc.
  39. Installation
  40. ------------
  41. The stable version of this package is maintained on pypi, install using pip::
  42. pip install wcwidth
  43. Example
  44. -------
  45. To Display ``u'コンニチハ'`` right-adjusted on screen of 80 columns::
  46. >>> from wcwidth import wcswidth
  47. >>> text = u'コンニチハ'
  48. >>> text_len = wcswidth(text)
  49. >>> print(u' ' * (80 - text_len) + text)
  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 a several, or a
  54. *string 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. More documentation is available using pydoc::
  68. $ pydoc wcwidth
  69. =======
  70. Caveats
  71. =======
  72. This library attempts to determine the printable width by an unknown targeted
  73. terminal emulator. It does not provide any ability to discern what the target
  74. emulator software, version, of level of support is. Results may vary!
  75. A `crude method
  76. <http://blessed.readthedocs.org/en/latest/examples.html#detect-multibyte-py>`_
  77. of determining the level of unicode support by the target emulator may be
  78. performed using the VT100 Query Cursor Position sequence.
  79. The libc version of `wcwidth(3)`_ is often several unicode releases behind,
  80. and therefor several levels of support lower than this python library. You
  81. may determine an exacting list of these discrepancies using the project
  82. file `wcwidth-libc-comparator.py
  83. <https://github.com/jquast/wcwidth/tree/master/bin/wcwidth-libc-comparator.py>`_.
  84. ==========
  85. Developing
  86. ==========
  87. Install wcwidth in editable mode::
  88. pip install -e.
  89. Install developer requirements::
  90. pip install -r requirements-develop.txt
  91. Execute unit tests using tox::
  92. tox
  93. Updating Tables
  94. ---------------
  95. The command ``python setup.py update`` will fetch the following resources:
  96. - http://www.unicode.org/Public/UNIDATA/EastAsianWidth.txt
  97. - http://www.unicode.org/Public/UNIDATA/extracted/DerivedGeneralCategory.txt
  98. And generates the table files:
  99. - `wcwidth/table_wide.py <https://github.com/jquast/wcwidth/tree/master/wcwidth/table_wide.py>`_
  100. - `wcwidth/table_zero.py <https://github.com/jquast/wcwidth/tree/master/wcwidth/table_zero.py>`_
  101. Uses
  102. ----
  103. This library is used in:
  104. - `jquast/blessed`_, a simplified wrapper around curses.
  105. - `jonathanslenders/python-prompt-toolkit`_, a Library for building powerful
  106. interactive command lines in Python.
  107. Additional tools for displaying and testing wcwidth are found in the `bin/
  108. <https://github.com/jquast/wcwidth/tree/master/bin>`_ folder of this project's
  109. source code. They are not distributed.
  110. =======
  111. History
  112. =======
  113. 0.1.7 *2016-07-01*
  114. * **Updated** tables to Unicode Specification 9.0.0. (`PR #18`_).
  115. 0.1.6 *2016-01-08 Production/Stable*
  116. * ``LICENSE`` file now included with distribution.
  117. 0.1.5 *2015-09-13 Alpha*
  118. * **Bugfix**:
  119. Resolution of "combining_ character width" issue, most especially
  120. those that previously returned -1 now often (correctly) return 0.
  121. resolved by `Philip Craig`_ via `PR #11`_.
  122. * **Deprecated**:
  123. The module path ``wcwidth.table_comb`` is no longer available,
  124. it has been superseded by module path ``wcwidth.table_zero``.
  125. 0.1.4 *2014-11-20 Pre-Alpha*
  126. * **Feature**: ``wcswidth()`` now determines printable length
  127. for (most) combining_ characters. The developer's tool
  128. `bin/wcwidth-browser.py`_ is improved to display combining_
  129. characters when provided the ``--combining`` option
  130. (`Thomas Ballinger`_ and `Leta Montopoli`_ `PR #5`_).
  131. * **Feature**: added static analysis (prospector_) to testing
  132. framework.
  133. 0.1.3 *2014-10-29 Pre-Alpha*
  134. * **Bugfix**: 2nd parameter of wcswidth was not honored.
  135. (`Thomas Ballinger`_, `PR #4`_).
  136. 0.1.2 *2014-10-28 Pre-Alpha*
  137. * **Updated** tables to Unicode Specification 7.0.0.
  138. (`Thomas Ballinger`_, `PR #3`_).
  139. 0.1.1 *2014-05-14 Pre-Alpha*
  140. * Initial release to pypi, Based on Unicode Specification 6.3.0
  141. This code was originally derived directly from C code of the same name,
  142. whose latest version is available at
  143. http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c::
  144. * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
  145. *
  146. * Permission to use, copy, modify, and distribute this software
  147. * for any purpose and without fee is hereby granted. The author
  148. * disclaims all warranties with regard to this software.
  149. .. _`prospector`: https://github.com/landscapeio/prospector
  150. .. _`combining`: https://en.wikipedia.org/wiki/Combining_character
  151. .. _`bin/wcwidth-browser.py`: https://github.com/jquast/wcwidth/tree/master/bin/wcwidth-browser.py
  152. .. _`Thomas Ballinger`: https://github.com/thomasballinger
  153. .. _`Leta Montopoli`: https://github.com/lmontopo
  154. .. _`Philip Craig`: https://github.com/philipc
  155. .. _`PR #3`: https://github.com/jquast/wcwidth/pull/3
  156. .. _`PR #4`: https://github.com/jquast/wcwidth/pull/4
  157. .. _`PR #5`: https://github.com/jquast/wcwidth/pull/5
  158. .. _`PR #11`: https://github.com/jquast/wcwidth/pull/11
  159. .. _`PR #18`: https://github.com/jquast/wcwidth/pull/18
  160. .. _`jquast/blessed`: https://github.com/jquast/blessed
  161. .. _`jonathanslenders/python-prompt-toolkit`: https://github.com/jonathanslenders/python-prompt-toolkit
  162. .. _`wcwidth(3)`: http://man7.org/linux/man-pages/man3/wcwidth.3.html
  163. .. _`wcswidth(3)`: http://man7.org/linux/man-pages/man3/wcswidth.3.html