wcwidth.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. """
  2. This is a python implementation of wcwidth() and wcswidth().
  3. https://github.com/jquast/wcwidth
  4. from Markus Kuhn's C code, retrieved from:
  5. http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
  6. This is an implementation of wcwidth() and wcswidth() (defined in
  7. IEEE Std 1002.1-2001) for Unicode.
  8. http://www.opengroup.org/onlinepubs/007904975/functions/wcwidth.html
  9. http://www.opengroup.org/onlinepubs/007904975/functions/wcswidth.html
  10. In fixed-width output devices, Latin characters all occupy a single
  11. "cell" position of equal width, whereas ideographic CJK characters
  12. occupy two such cells. Interoperability between terminal-line
  13. applications and (teletype-style) character terminals using the
  14. UTF-8 encoding requires agreement on which character should advance
  15. the cursor by how many cell positions. No established formal
  16. standards exist at present on which Unicode character shall occupy
  17. how many cell positions on character terminals. These routines are
  18. a first attempt of defining such behavior based on simple rules
  19. applied to data provided by the Unicode Consortium.
  20. For some graphical characters, the Unicode standard explicitly
  21. defines a character-cell width via the definition of the East Asian
  22. FullWidth (F), Wide (W), Half-width (H), and Narrow (Na) classes.
  23. In all these cases, there is no ambiguity about which width a
  24. terminal shall use. For characters in the East Asian Ambiguous (A)
  25. class, the width choice depends purely on a preference of backward
  26. compatibility with either historic CJK or Western practice.
  27. Choosing single-width for these characters is easy to justify as
  28. the appropriate long-term solution, as the CJK practice of
  29. displaying these characters as double-width comes from historic
  30. implementation simplicity (8-bit encoded characters were displayed
  31. single-width and 16-bit ones double-width, even for Greek,
  32. Cyrillic, etc.) and not any typographic considerations.
  33. Much less clear is the choice of width for the Not East Asian
  34. (Neutral) class. Existing practice does not dictate a width for any
  35. of these characters. It would nevertheless make sense
  36. typographically to allocate two character cells to characters such
  37. as for instance EM SPACE or VOLUME INTEGRAL, which cannot be
  38. represented adequately with a single-width glyph. The following
  39. routines at present merely assign a single-cell width to all
  40. neutral characters, in the interest of simplicity. This is not
  41. entirely satisfactory and should be reconsidered before
  42. establishing a formal standard in this area. At the moment, the
  43. decision which Not East Asian (Neutral) characters should be
  44. represented by double-width glyphs cannot yet be answered by
  45. applying a simple rule from the Unicode database content. Setting
  46. up a proper standard for the behavior of UTF-8 character terminals
  47. will require a careful analysis not only of each Unicode character,
  48. but also of each presentation form, something the author of these
  49. routines has avoided to do so far.
  50. http://www.unicode.org/unicode/reports/tr11/
  51. Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
  52. """
  53. from __future__ import annotations
  54. # std imports
  55. import os
  56. import warnings
  57. from functools import lru_cache
  58. from typing import TYPE_CHECKING
  59. # local
  60. from .bisearch import bisearch as _bisearch
  61. from .grapheme import iter_graphemes
  62. from .table_vs16 import VS16_NARROW_TO_WIDE
  63. from .table_wide import WIDE_EASTASIAN
  64. from .table_zero import ZERO_WIDTH
  65. from .control_codes import ILLEGAL_CTRL, VERTICAL_CTRL, HORIZONTAL_CTRL, ZERO_WIDTH_CTRL
  66. from .table_ambiguous import AMBIGUOUS_EASTASIAN
  67. from .escape_sequences import (ZERO_WIDTH_PATTERN,
  68. CURSOR_LEFT_SEQUENCE,
  69. CURSOR_RIGHT_SEQUENCE,
  70. INDETERMINATE_EFFECT_SEQUENCE)
  71. from .unicode_versions import list_versions
  72. if TYPE_CHECKING: # pragma: no cover
  73. # std imports
  74. from collections.abc import Iterator
  75. from typing import Literal
  76. _AMBIGUOUS_TABLE = AMBIGUOUS_EASTASIAN[next(iter(AMBIGUOUS_EASTASIAN))]
  77. # Translation table to strip C0/C1 control characters for fast 'ignore' mode.
  78. _CONTROL_CHAR_TABLE = str.maketrans('', '', (
  79. ''.join(chr(c) for c in range(0x00, 0x20)) + # C0: NUL through US (including tab)
  80. '\x7f' + # DEL
  81. ''.join(chr(c) for c in range(0x80, 0xa0)) # C1: U+0080-U+009F
  82. ))
  83. # Unlike wcwidth.__all__, wcwidth.wcwidth.__all__ is NOT for the purpose of defining a public API,
  84. # or what we prefer to be imported with statement, "from wcwidth.wcwidth import *". Explicitly
  85. # re-export imports here for no other reason than to satisfy the type checkers (mypy). Yak shavings.
  86. __all__ = (
  87. 'ZERO_WIDTH',
  88. 'WIDE_EASTASIAN',
  89. 'AMBIGUOUS_EASTASIAN',
  90. 'VS16_NARROW_TO_WIDE',
  91. 'list_versions',
  92. 'wcwidth',
  93. 'wcswidth',
  94. 'width',
  95. 'iter_sequences',
  96. 'ljust',
  97. 'rjust',
  98. 'center',
  99. 'clip',
  100. 'strip_sequences',
  101. '_wcmatch_version',
  102. '_wcversion_value',
  103. )
  104. @lru_cache(maxsize=2000)
  105. def wcwidth(wc: str, unicode_version: str = 'auto', ambiguous_width: int = 1) -> int:
  106. r"""
  107. Given one Unicode codepoint, return its printable length on a terminal.
  108. :param wc: A single Unicode character.
  109. :param unicode_version: A Unicode version number, such as
  110. ``'6.0.0'``. A list of version levels supported by wcwidth
  111. is returned by :func:`list_versions`.
  112. Any version string may be specified without error -- the nearest
  113. matching version is selected. When ``'auto'`` (default), the
  114. ``UNICODE_VERSION`` environment variable is used if set, otherwise
  115. the highest Unicode version level is used.
  116. .. deprecated:: 0.3.0
  117. This parameter is deprecated. Empirical data shows that Unicode
  118. support in terminals varies not only by unicode version, but
  119. by capabilities, Emojis, and specific language support.
  120. The default ``'auto'`` behavior is recommended for all use cases.
  121. :param ambiguous_width: Width to use for East Asian Ambiguous (A)
  122. characters. Default is ``1`` (narrow). Set to ``2`` for CJK contexts
  123. where ambiguous characters display as double-width. See
  124. :ref:`ambiguous_width` for details.
  125. :returns: The width, in cells, necessary to display the character of
  126. Unicode string character, ``wc``. Returns 0 if the ``wc`` argument has
  127. no printable effect on a terminal (such as NUL '\0'), -1 if ``wc`` is
  128. not printable, or has an indeterminate effect on the terminal, such as
  129. a control character. Otherwise, the number of column positions the
  130. character occupies on a graphic terminal (1 or 2) is returned.
  131. See :ref:`Specification` for details of cell measurement.
  132. """
  133. ucs = ord(wc) if wc else 0
  134. # small optimization: early return of 1 for printable ASCII, this provides
  135. # approximately 40% performance improvement for mostly-ascii documents, with
  136. # less than 1% impact to others.
  137. if 32 <= ucs < 0x7f:
  138. return 1
  139. # C0/C1 control characters are -1 for compatibility with POSIX-like calls
  140. if ucs and ucs < 32 or 0x07F <= ucs < 0x0A0:
  141. return -1
  142. _unicode_version = _wcmatch_version(unicode_version)
  143. # Zero width
  144. if _bisearch(ucs, ZERO_WIDTH[_unicode_version]):
  145. return 0
  146. # Wide (F/W categories)
  147. if _bisearch(ucs, WIDE_EASTASIAN[_unicode_version]):
  148. return 2
  149. # Ambiguous width (A category) - only when ambiguous_width=2
  150. if ambiguous_width == 2 and _bisearch(ucs, _AMBIGUOUS_TABLE):
  151. return 2
  152. return 1
  153. def wcswidth(
  154. pwcs: str,
  155. n: int | None = None,
  156. unicode_version: str = 'auto',
  157. ambiguous_width: int = 1,
  158. ) -> int:
  159. """
  160. Given a unicode string, return its printable length on a terminal.
  161. :param pwcs: Measure width of given unicode string.
  162. :param n: When ``n`` is None (default), return the length of the entire
  163. string, otherwise only the first ``n`` characters are measured. This
  164. argument exists only for compatibility with the C POSIX function
  165. signature. It is suggested instead to use python's string slicing
  166. capability, ``wcswidth(pwcs[:n])``
  167. :param unicode_version: A Unicode version number, such as
  168. ``'6.0.0'``, or ``'auto'`` (default) which uses the
  169. ``UNICODE_VERSION`` environment variable if defined, or the latest
  170. available unicode version otherwise.
  171. .. deprecated:: 0.3.0
  172. This parameter is deprecated. Empirical data shows that Unicode
  173. support in terminals varies not only by unicode version, but
  174. by capabilities, Emojis, and specific language support.
  175. The default ``'auto'`` behavior is recommended for all use cases.
  176. :param ambiguous_width: Width to use for East Asian Ambiguous (A)
  177. characters. Default is ``1`` (narrow). Set to ``2`` for CJK contexts.
  178. :returns: The width, in cells, needed to display the first ``n`` characters
  179. of the unicode string ``pwcs``. Returns ``-1`` for C0 and C1 control
  180. characters!
  181. See :ref:`Specification` for details of cell measurement.
  182. """
  183. # this 'n' argument is a holdover for POSIX function
  184. # Fast path: pure ASCII printable strings are always width == length
  185. if n is None and pwcs.isascii() and pwcs.isprintable():
  186. return len(pwcs)
  187. _unicode_version = None
  188. end = len(pwcs) if n is None else n
  189. total_width = 0
  190. idx = 0
  191. last_measured_idx = -2 # Track index of last measured char for VS16
  192. while idx < end:
  193. char = pwcs[idx]
  194. if char == '\u200D':
  195. # Zero Width Joiner, do not measure this or next character
  196. idx += 2
  197. continue
  198. if char == '\uFE0F' and last_measured_idx >= 0:
  199. # VS16 following a measured character: add 1 if that character is
  200. # known to be converted from narrow to wide by VS16.
  201. if _unicode_version is None:
  202. _unicode_version = _wcversion_value(_wcmatch_version(unicode_version))
  203. if _unicode_version >= (9, 0, 0):
  204. total_width += _bisearch(ord(pwcs[last_measured_idx]),
  205. VS16_NARROW_TO_WIDE["9.0.0"])
  206. last_measured_idx = -2 # Prevent double application
  207. idx += 1
  208. continue
  209. # measure character at current index
  210. wcw = wcwidth(char, unicode_version, ambiguous_width)
  211. if wcw < 0:
  212. # early return -1 on C0 and C1 control characters
  213. return wcw
  214. if wcw > 0:
  215. last_measured_idx = idx
  216. total_width += wcw
  217. idx += 1
  218. return total_width
  219. @lru_cache(maxsize=128)
  220. def _wcversion_value(ver_string: str) -> tuple[int, ...]:
  221. """
  222. Integer-mapped value of given dotted version string.
  223. :param ver_string: Unicode version string, of form ``n.n.n``.
  224. :returns: tuple of digit tuples, ``tuple(int, [...])``.
  225. """
  226. retval = tuple(map(int, (ver_string.split('.'))))
  227. return retval
  228. @lru_cache(maxsize=8)
  229. def _wcmatch_version(given_version: str) -> str:
  230. """
  231. Return nearest matching supported Unicode version level.
  232. If an exact match is not determined, the nearest lowest version level is
  233. returned after a warning is emitted. For example, given supported levels
  234. ``4.1.0`` and ``5.0.0``, and a version string of ``4.9.9``, then ``4.1.0``
  235. is selected and returned:
  236. >>> _wcmatch_version('4.9.9')
  237. '4.1.0'
  238. >>> _wcmatch_version('8.0')
  239. '8.0.0'
  240. >>> _wcmatch_version('1')
  241. '4.1.0'
  242. :param given_version: given version for compare, may be ``auto``
  243. (default), to select Unicode Version from Environment Variable,
  244. ``UNICODE_VERSION``. If the environment variable is not set, then the
  245. latest is used.
  246. :returns: unicode string.
  247. """
  248. # Design note: the choice to return the same type that is given certainly
  249. # complicates it for python 2 str-type, but allows us to define an api that
  250. # uses 'string-type' for unicode version level definitions, so all of our
  251. # example code works with all versions of python.
  252. #
  253. # That, along with the string-to-numeric and comparisons of earliest,
  254. # latest, matching, or nearest, greatly complicates this function.
  255. # Performance is somewhat curbed by memoization.
  256. unicode_versions = list_versions()
  257. latest_version = unicode_versions[-1]
  258. if given_version == 'auto':
  259. given_version = os.environ.get(
  260. 'UNICODE_VERSION',
  261. 'latest')
  262. if given_version == 'latest':
  263. # default match, when given as 'latest', use the most latest unicode
  264. # version specification level supported.
  265. return latest_version
  266. if given_version in unicode_versions:
  267. # exact match, downstream has specified an explicit matching version
  268. # matching any value of list_versions().
  269. return given_version
  270. # The user's version is not supported by ours. We return the newest unicode
  271. # version level that we support below their given value.
  272. try:
  273. cmp_given = _wcversion_value(given_version)
  274. except ValueError:
  275. # submitted value raises ValueError in int(), warn and use latest.
  276. warnings.warn(f"UNICODE_VERSION value, {given_version!r}, is invalid. "
  277. "Value should be in form of `integer[.]+', the latest "
  278. f"supported unicode version {latest_version!r} has been "
  279. "inferred.")
  280. return latest_version
  281. # given version is less than any available version, return earliest
  282. # version.
  283. earliest_version = unicode_versions[0]
  284. cmp_earliest_version = _wcversion_value(earliest_version)
  285. if cmp_given <= cmp_earliest_version:
  286. # this probably isn't what you wanted, the oldest wcwidth.c you will
  287. # find in the wild is likely version 5 or 6, which we both support,
  288. # but it's better than not saying anything at all.
  289. warnings.warn(f"UNICODE_VERSION value, {given_version!r}, is lower "
  290. "than any available unicode version. Returning lowest "
  291. f"version level, {earliest_version!r}")
  292. return earliest_version
  293. # create list of versions which are less than our equal to given version,
  294. # and return the tail value, which is the highest level we may support,
  295. # or the latest value we support, when completely unmatched or higher
  296. # than any supported version.
  297. #
  298. # function will never complete, always returns.
  299. for idx, unicode_version in enumerate(unicode_versions):
  300. # look ahead to next value
  301. try:
  302. cmp_next_version = _wcversion_value(unicode_versions[idx + 1])
  303. except IndexError:
  304. # at end of list, return latest version
  305. return latest_version
  306. # Maybe our given version has less parts, as in tuple(8, 0), than the
  307. # next compare version tuple(8, 0, 0). Test for an exact match by
  308. # comparison of only the leading dotted piece(s): (8, 0) == (8, 0).
  309. if cmp_given == cmp_next_version[:len(cmp_given)]:
  310. return unicode_versions[idx + 1]
  311. # Or, if any next value is greater than our given support level
  312. # version, return the current value in index. Even though it must
  313. # be less than the given value, it's our closest possible match. That
  314. # is, 4.1 is returned for given 4.9.9, where 4.1 and 5.0 are available.
  315. if cmp_next_version > cmp_given:
  316. return unicode_version
  317. assert False, ("Code path unreachable", given_version, unicode_versions) # pragma: no cover
  318. def iter_sequences(text: str) -> Iterator[tuple[str, bool]]:
  319. r"""
  320. Iterate through text, yielding segments with sequence identification.
  321. This generator yields tuples of ``(segment, is_sequence)`` for each part
  322. of the input text, where ``is_sequence`` is ``True`` if the segment is
  323. a recognized terminal escape sequence.
  324. :param text: String to iterate through.
  325. :returns: Iterator of (segment, is_sequence) tuples.
  326. .. versionadded:: 0.3.0
  327. Example::
  328. >>> list(iter_sequences('hello'))
  329. [('hello', False)]
  330. >>> list(iter_sequences('\\x1b[31mred'))
  331. [('\\x1b[31m', True), ('red', False)]
  332. >>> list(iter_sequences('\\x1b[1m\\x1b[31m'))
  333. [('\\x1b[1m', True), ('\\x1b[31m', True)]
  334. """
  335. idx = 0
  336. text_len = len(text)
  337. segment_start = 0
  338. while idx < text_len:
  339. char = text[idx]
  340. if char == '\x1b':
  341. # Yield any accumulated non-sequence text
  342. if idx > segment_start:
  343. yield (text[segment_start:idx], False)
  344. # Try to match an escape sequence
  345. match = ZERO_WIDTH_PATTERN.match(text, idx)
  346. if match:
  347. yield (match.group(), True)
  348. idx = match.end()
  349. else:
  350. # Lone ESC or unrecognized - yield as sequence anyway
  351. yield (char, True)
  352. idx += 1
  353. segment_start = idx
  354. else:
  355. idx += 1
  356. # Yield any remaining text
  357. if segment_start < text_len:
  358. yield (text[segment_start:], False)
  359. def _width_ignored_codes(text: str, ambiguous_width: int = 1) -> int:
  360. """
  361. Fast path for width() with control_codes='ignore'.
  362. Strips escape sequences and control characters, then measures remaining text.
  363. """
  364. return wcswidth(
  365. strip_sequences(text).translate(_CONTROL_CHAR_TABLE),
  366. ambiguous_width=ambiguous_width
  367. )
  368. def width(
  369. text: str,
  370. *,
  371. control_codes: Literal['parse', 'strict', 'ignore'] = 'parse',
  372. tabsize: int = 8,
  373. ambiguous_width: int = 1,
  374. ) -> int:
  375. r"""
  376. Return printable width of text containing many kinds of control codes and sequences.
  377. Unlike :func:`wcswidth`, this function handles most control characters and many popular terminal
  378. output sequences. Never returns -1.
  379. :param text: String to measure.
  380. :param control_codes: How to handle control characters and sequences:
  381. - ``'parse'`` (default): Track horizontal cursor movement from BS ``\\b``, CR ``\\r``, TAB
  382. ``\\t``, and cursor left and right movement sequences. Vertical movement (LF, VT, FF) and
  383. indeterminate sequences are zero-width. Never raises.
  384. - ``'strict'``: Like parse, but raises :exc:`ValueError` on control characters with
  385. indeterminate results of the screen or cursor, like clear or vertical movement. Generally,
  386. these should be handled with a virtual terminal emulator (like 'pyte').
  387. - ``'ignore'``: All C0 and C1 control characters and escape sequences are measured as
  388. width 0. This is the fastest measurement for text already filtered or known not to contain
  389. any kinds of control codes or sequences. TAB ``\\t`` is zero-width; for tab expansion,
  390. pre-process: ``text.replace('\\t', ' ' * 8)``.
  391. :param tabsize: Tab stop width for ``'parse'`` and ``'strict'`` modes. Default is 8.
  392. Must be positive. Has no effect when ``control_codes='ignore'``.
  393. :param ambiguous_width: Width to use for East Asian Ambiguous (A)
  394. characters. Default is ``1`` (narrow). Set to ``2`` for CJK contexts.
  395. :returns: Maximum cursor position reached, "extent", accounting for cursor movement sequences
  396. present in ``text`` according to given parameters. This represents the rightmost column the
  397. cursor reaches. Always a non-negative integer.
  398. :raises ValueError: If ``control_codes='strict'`` and control characters with indeterminate
  399. effects, such as vertical movement or clear sequences are encountered, or on unexpected
  400. C0 or C1 control code. Also raised when ``control_codes`` is not one of the valid values.
  401. .. versionadded:: 0.3.0
  402. Examples::
  403. >>> width('hello')
  404. 5
  405. >>> width('コンニチハ')
  406. 10
  407. >>> width('\\x1b[31mred\\x1b[0m')
  408. 3
  409. >>> width('\\x1b[31mred\\x1b[0m', control_codes='ignore') # same result (ignored)
  410. 3
  411. >>> width('123\\b4') # backspace overwrites previous cell (outputs '124')
  412. 3
  413. >>> width('abc\\t') # tab caused cursor to move to column 8
  414. 8
  415. >>> width('1\\x1b[10C') # '1' + cursor right 10, cursor ends on column 11
  416. 11
  417. >>> width('1\\x1b[10C', control_codes='ignore') # faster but wrong in this case
  418. 1
  419. """
  420. # pylint: disable=too-complex,too-many-branches,too-many-statements,too-many-locals
  421. # This could be broken into sub-functions (#1, #3, and 6 especially), but for reduced overhead
  422. # considering this function is a likely "hot path", they are inlined, breaking many of our
  423. # complexity rules.
  424. # Fast parse: if no horizontal cursor movements are possible, switch to 'ignore' mode.
  425. # Only check for longer strings - the detection overhead hurts short string performance.
  426. if control_codes == 'parse' and len(text) > 20:
  427. # Check for cursor-affecting control characters
  428. if '\b' not in text and '\t' not in text and '\r' not in text:
  429. # Check for escape sequences - if none, or only non-cursor-movement sequences
  430. if '\x1b' not in text or (
  431. not CURSOR_RIGHT_SEQUENCE.search(text) and
  432. not CURSOR_LEFT_SEQUENCE.search(text)
  433. ):
  434. control_codes = 'ignore'
  435. # Fast path for ignore mode -- this is useful if you know the text is already "clean"
  436. if control_codes == 'ignore':
  437. return _width_ignored_codes(text, ambiguous_width)
  438. strict = control_codes == 'strict'
  439. # Track absolute positions: tab stops need modulo on absolute column, CR resets to 0.
  440. # Initialize max_extent to 0 so backward movement (CR, BS) won't yield negative width.
  441. current_col = 0
  442. max_extent = 0
  443. idx = 0
  444. last_measured_idx = -2 # Track index of last measured char for VS16; -2 can never match idx-1
  445. text_len = len(text)
  446. while idx < text_len:
  447. char = text[idx]
  448. # 1. Handle ESC sequences
  449. if char == '\x1b':
  450. match = ZERO_WIDTH_PATTERN.match(text, idx)
  451. if match:
  452. seq = match.group()
  453. if strict and INDETERMINATE_EFFECT_SEQUENCE.match(seq):
  454. raise ValueError(f"Indeterminate cursor sequence at position {idx}")
  455. # Apply cursor movement
  456. right = CURSOR_RIGHT_SEQUENCE.match(seq)
  457. if right:
  458. current_col += int(right.group(1) or 1)
  459. else:
  460. left = CURSOR_LEFT_SEQUENCE.match(seq)
  461. if left:
  462. current_col = max(0, current_col - int(left.group(1) or 1))
  463. idx = match.end()
  464. else:
  465. idx += 1
  466. max_extent = max(max_extent, current_col)
  467. continue
  468. # 2. Handle illegal and vertical control characters (zero width, error in strict)
  469. if char in ILLEGAL_CTRL:
  470. if strict:
  471. raise ValueError(f"Illegal control character {ord(char):#x} at position {idx}")
  472. idx += 1
  473. continue
  474. if char in VERTICAL_CTRL:
  475. if strict:
  476. raise ValueError(f"Vertical movement character {ord(char):#x} at position {idx}")
  477. idx += 1
  478. continue
  479. # 3. Handle horizontal movement characters
  480. if char in HORIZONTAL_CTRL:
  481. if char == '\x09' and tabsize > 0: # Tab
  482. current_col += tabsize - (current_col % tabsize)
  483. elif char == '\x08': # Backspace
  484. if current_col > 0:
  485. current_col -= 1
  486. elif char == '\x0d': # Carriage return
  487. current_col = 0
  488. max_extent = max(max_extent, current_col)
  489. idx += 1
  490. continue
  491. # 4. Handle ZWJ (skip this and next character)
  492. if char == '\u200D':
  493. idx += 2
  494. continue
  495. # 5. Handle other zero-width characters (control chars)
  496. if char in ZERO_WIDTH_CTRL:
  497. idx += 1
  498. continue
  499. # 6. Handle VS16: converts preceding narrow character to wide
  500. if char == '\uFE0F':
  501. if last_measured_idx == idx - 1:
  502. if _bisearch(ord(text[last_measured_idx]), VS16_NARROW_TO_WIDE["9.0.0"]):
  503. current_col += 1
  504. max_extent = max(max_extent, current_col)
  505. idx += 1
  506. continue
  507. # 7. Normal characters: measure with wcwidth
  508. w = wcwidth(char, 'auto', ambiguous_width)
  509. if w > 0:
  510. current_col += w
  511. max_extent = max(max_extent, current_col)
  512. last_measured_idx = idx
  513. idx += 1
  514. return max_extent
  515. def ljust(
  516. text: str,
  517. dest_width: int,
  518. fillchar: str = ' ',
  519. *,
  520. control_codes: Literal['parse', 'strict', 'ignore'] = 'parse',
  521. ambiguous_width: int = 1,
  522. ) -> str:
  523. r"""
  524. Return text left-justified in a string of given display width.
  525. :param text: String to justify, may contain terminal sequences.
  526. :param dest_width: Total display width of result in terminal cells.
  527. :param fillchar: Single character for padding (default space). Must have
  528. display width of 1 (not wide, not zero-width, not combining). Unicode
  529. characters like ``'·'`` are acceptable. The width is not validated.
  530. :param control_codes: How to handle control sequences when measuring.
  531. Passed to :func:`width` for measurement.
  532. :param ambiguous_width: Width to use for East Asian Ambiguous (A)
  533. characters. Default is ``1`` (narrow). Set to ``2`` for CJK contexts.
  534. :returns: Text padded on the right to reach ``dest_width``.
  535. .. versionadded:: 0.3.0
  536. Example::
  537. >>> wcwidth.ljust('hi', 5)
  538. 'hi '
  539. >>> wcwidth.ljust('\\x1b[31mhi\\x1b[0m', 5)
  540. '\\x1b[31mhi\\x1b[0m '
  541. >>> wcwidth.ljust('\\U0001F468\\u200D\\U0001F469\\u200D\\U0001F467', 6)
  542. '👨‍👩‍👧 '
  543. """
  544. if text.isascii() and text.isprintable():
  545. text_width = len(text)
  546. else:
  547. text_width = width(text, control_codes=control_codes, ambiguous_width=ambiguous_width)
  548. padding_cells = max(0, dest_width - text_width)
  549. return text + fillchar * padding_cells
  550. def rjust(
  551. text: str,
  552. dest_width: int,
  553. fillchar: str = ' ',
  554. *,
  555. control_codes: Literal['parse', 'strict', 'ignore'] = 'parse',
  556. ambiguous_width: int = 1,
  557. ) -> str:
  558. r"""
  559. Return text right-justified in a string of given display width.
  560. :param text: String to justify, may contain terminal sequences.
  561. :param dest_width: Total display width of result in terminal cells.
  562. :param fillchar: Single character for padding (default space). Must have
  563. display width of 1 (not wide, not zero-width, not combining). Unicode
  564. characters like ``'·'`` are acceptable. The width is not validated.
  565. :param control_codes: How to handle control sequences when measuring.
  566. Passed to :func:`width` for measurement.
  567. :param ambiguous_width: Width to use for East Asian Ambiguous (A)
  568. characters. Default is ``1`` (narrow). Set to ``2`` for CJK contexts.
  569. :returns: Text padded on the left to reach ``dest_width``.
  570. .. versionadded:: 0.3.0
  571. Example::
  572. >>> wcwidth.rjust('hi', 5)
  573. ' hi'
  574. >>> wcwidth.rjust('\\x1b[31mhi\\x1b[0m', 5)
  575. ' \\x1b[31mhi\\x1b[0m'
  576. >>> wcwidth.rjust('\\U0001F468\\u200D\\U0001F469\\u200D\\U0001F467', 6)
  577. ' 👨‍👩‍👧'
  578. """
  579. if text.isascii() and text.isprintable():
  580. text_width = len(text)
  581. else:
  582. text_width = width(text, control_codes=control_codes, ambiguous_width=ambiguous_width)
  583. padding_cells = max(0, dest_width - text_width)
  584. return fillchar * padding_cells + text
  585. def center(
  586. text: str,
  587. dest_width: int,
  588. fillchar: str = ' ',
  589. *,
  590. control_codes: Literal['parse', 'strict', 'ignore'] = 'parse',
  591. ambiguous_width: int = 1,
  592. ) -> str:
  593. r"""
  594. Return text centered in a string of given display width.
  595. :param text: String to center, may contain terminal sequences.
  596. :param dest_width: Total display width of result in terminal cells.
  597. :param fillchar: Single character for padding (default space). Must have
  598. display width of 1 (not wide, not zero-width, not combining). Unicode
  599. characters like ``'·'`` are acceptable. The width is not validated.
  600. :param control_codes: How to handle control sequences when measuring.
  601. Passed to :func:`width` for measurement.
  602. :param ambiguous_width: Width to use for East Asian Ambiguous (A)
  603. characters. Default is ``1`` (narrow). Set to ``2`` for CJK contexts.
  604. :returns: Text padded on both sides to reach ``dest_width``.
  605. For odd-width padding, the extra cell goes on the right (matching
  606. Python's :meth:`str.center` behavior).
  607. .. versionadded:: 0.3.0
  608. Example::
  609. >>> wcwidth.center('hi', 6)
  610. ' hi '
  611. >>> wcwidth.center('\\x1b[31mhi\\x1b[0m', 6)
  612. ' \\x1b[31mhi\\x1b[0m '
  613. >>> wcwidth.center('\\U0001F468\\u200D\\U0001F469\\u200D\\U0001F467', 6)
  614. ' 👨‍👩‍👧 '
  615. """
  616. if text.isascii() and text.isprintable():
  617. text_width = len(text)
  618. else:
  619. text_width = width(text, control_codes=control_codes, ambiguous_width=ambiguous_width)
  620. total_padding = max(0, dest_width - text_width)
  621. left_pad = total_padding // 2
  622. right_pad = total_padding - left_pad
  623. return fillchar * left_pad + text + fillchar * right_pad
  624. def strip_sequences(text: str) -> str:
  625. r"""
  626. Return text with all terminal escape sequences removed.
  627. Unknown or incomplete ESC sequences are preserved.
  628. :param text: String that may contain terminal escape sequences.
  629. :returns: The input text with all escape sequences stripped.
  630. .. versionadded:: 0.3.0
  631. Example::
  632. >>> strip_sequences('\\x1b[31mred\\x1b[0m')
  633. 'red'
  634. >>> strip_sequences('hello')
  635. 'hello'
  636. >>> strip_sequences('\\x1b[1m\\x1b[31mbold red\\x1b[0m text')
  637. 'bold red text'
  638. """
  639. return ZERO_WIDTH_PATTERN.sub('', text)
  640. def clip(
  641. text: str,
  642. start: int,
  643. end: int,
  644. *,
  645. fillchar: str = ' ',
  646. tabsize: int = 8,
  647. ambiguous_width: int = 1,
  648. ) -> str:
  649. r"""
  650. Clip text to display columns ``(start, end)`` while preserving all terminal sequences.
  651. This function extracts a substring based on visible column positions rather than
  652. character indices. Terminal escape sequences are preserved in the output since
  653. they have zero display width. If a wide character (width 2) would be split at
  654. either boundary, it is replaced with ``fillchar``.
  655. TAB characters (``\\t``) are expanded to spaces up to the next tab stop,
  656. controlled by the ``tabsize`` parameter.
  657. Other cursor movement characters (backspace, carriage return) and cursor
  658. movement sequences are passed through unchanged as zero-width.
  659. :param text: String to clip, may contain terminal escape sequences.
  660. :param start: Absolute starting column (inclusive, 0-indexed).
  661. :param end: Absolute ending column (exclusive).
  662. :param fillchar: Character to use when a wide character must be split at
  663. a boundary (default space). Must have display width of 1.
  664. :param tabsize: Tab stop width (default 8). Set to 0 to pass tabs through
  665. as zero-width (preserved in output but don't advance column position).
  666. :param ambiguous_width: Width to use for East Asian Ambiguous (A)
  667. characters. Default is ``1`` (narrow). Set to ``2`` for CJK contexts.
  668. :returns: Substring of ``text`` spanning display columns ``(start, end)``,
  669. with all terminal sequences preserved and wide characters at boundaries
  670. replaced with ``fillchar``.
  671. .. versionadded:: 0.3.0
  672. Example::
  673. >>> clip('hello world', 0, 5)
  674. 'hello'
  675. >>> clip('中文字', 0, 3) # Wide char split at column 3
  676. '中 '
  677. >>> clip('a\\tb', 0, 10) # Tab expanded to spaces
  678. 'a b'
  679. """
  680. # pylint: disable=too-complex,too-many-locals,too-many-branches
  681. start = max(start, 0)
  682. if end <= start:
  683. return ''
  684. # Fast path: printable ASCII only (no tabs, escapes, or wide chars)
  685. if text.isascii() and text.isprintable():
  686. return text[start:end]
  687. output = []
  688. col = 0
  689. idx = 0
  690. text_len = len(text)
  691. while idx < text_len:
  692. char = text[idx]
  693. # Escape sequences: always include (zero-width)
  694. if char == '\x1b':
  695. match = ZERO_WIDTH_PATTERN.match(text, idx)
  696. if match:
  697. output.append(match.group())
  698. idx = match.end()
  699. else:
  700. output.append(char)
  701. idx += 1
  702. continue
  703. # TAB: expand to spaces (or pass through if tabsize=0)
  704. if char == '\t':
  705. if tabsize > 0:
  706. next_tab = col + (tabsize - (col % tabsize))
  707. while col < next_tab:
  708. if start <= col < end:
  709. output.append(' ')
  710. col += 1
  711. else:
  712. output.append(char)
  713. idx += 1
  714. continue
  715. # Grapheme clustering handles everything else (including control chars)
  716. grapheme = next(iter_graphemes(text[idx:]))
  717. w = width(grapheme, ambiguous_width=ambiguous_width)
  718. if w == 0:
  719. # Zero-width (combining marks, etc): always include, doesn't advance column
  720. output.append(grapheme)
  721. else:
  722. if col >= start and col + w <= end:
  723. # Fully visible: include the grapheme
  724. output.append(grapheme)
  725. elif col < end and col + w > start:
  726. # Partially visible: wide char spans boundary, replace with fillchar
  727. output.append(fillchar * (min(end, col + w) - max(start, col)))
  728. # Else: fully outside (start, end), omit entirely
  729. col += w
  730. idx += len(grapheme)
  731. return ''.join(output)