### Install wcwidth using pip Source: https://github.com/jquast/wcwidth/blob/master/docs/intro.md Install or upgrade the wcwidth package to the latest stable version from PyPI. This is the recommended method for most users. ```bash pip install --upgrade wcwidth ``` -------------------------------- ### Install wcwidth in Editable Mode Source: https://github.com/jquast/wcwidth/blob/master/docs/intro.md Installs the wcwidth package in editable mode for development. ```shell pip install -e . ``` -------------------------------- ### Getting Software Version with Blessed Source: https://github.com/jquast/wcwidth/blob/master/docs/intro.md Illustrates how to use the 'blessed' library to get the terminal's software version and use it with wcwidth for accurate width calculation. ```python >>> import blessed, wcwidth >>> term = blessed.Terminal() >>> sw_ver = term.get_software_version() >>> print(sw_ver) SoftwareVersion(name='VTE', version='7600') >>> wcwidth.width('\u2630', term_program=sw_ver.name) 1 ``` -------------------------------- ### Run Interactive wcwidth Browser Source: https://github.com/jquast/wcwidth/blob/master/docs/intro.md Launches an interactive browser for testing wide unicode characters in terminals. Requires 'pip install -r requirements-develop.txt'. ```shell python ./bin/wcwidth-browser.py ``` -------------------------------- ### Build Documentation with Sphinx Source: https://github.com/jquast/wcwidth/blob/master/docs/intro.md Builds the project documentation using Sphinx via tox. Output is located in 'docs/_build/html/'. ```shell tox -e sphinx ``` -------------------------------- ### Execute All Development Tasks with tox Source: https://github.com/jquast/wcwidth/blob/master/docs/intro.md Runs all code generation, autoformatters, linters, and unit tests using tox. ```shell tox ``` -------------------------------- ### Run Tests with Detailed Coverage Source: https://github.com/jquast/wcwidth/blob/master/docs/intro.md Executes tests with detailed coverage reporting, showing missing lines. ```shell tox -epy314 -- --cov-report=term-missing ``` -------------------------------- ### Update Requirements for Documentation Source: https://github.com/jquast/wcwidth/blob/master/docs/intro.md Upgrades requirements for building documentation using pip-tools via tox. ```shell tox -e update_requirements_docs ``` -------------------------------- ### Listing Supported Terminal Programs Source: https://github.com/jquast/wcwidth/blob/master/docs/intro.md Displays a list of all terminal program names recognized by the wcwidth library for applying corrections. ```python >>> wcwidth.list_term_programs() ('alacritty', 'apple_terminal', 'bobcat', 'contour', 'extraterm', 'foot', 'ghostty', 'hyper', 'iterm.app', 'iterm2', 'kitty', 'konsole', 'mintty', 'mlterm', 'pterm', 'putty', 'rio', 'rxvt', 'rxvt-unicode-256color', 'st', 'st-256color', 'tabby', 'terminology', 'urxvt', 'vscode', 'vte', 'warp', 'warpterminal', 'wezterm', 'xterm', 'xterm-ghostty', 'xterm-kitty', 'xterm.js') ``` -------------------------------- ### Execute Specific tox Tasks Source: https://github.com/jquast/wcwidth/blob/master/docs/intro.md Runs specific tasks defined in tox. Use 'tox -lv' to see all available targets. ```shell tox -e pylint,py36,py314 ``` -------------------------------- ### Wrap text with wcwidth.wrap Source: https://github.com/jquast/wcwidth/blob/master/docs/intro.md Use wcwidth.wrap to wrap text to a specific display width, correctly handling wide characters and ANSI color sequences. ```python >>> from wcwidth import wrap >>> # Basic wrapping >>> wrap('hello world', 5) ['hello', 'world'] >>> # Wrapping CJK text (each character is 2 cells wide) >>> wrap('コンニチハ', 4) ['コン', 'ニチ', 'ハ'] >>> # Text with ANSI color sequences - SGR codes are propagated by default >>> # Each line ends with reset, next line starts with restored style >>> wrap('\x1b[1;31mhello world\x1b[0m', 5) ['\x1b[1;31mhello\x1b[0m', '\x1b[1;31mworld\x1b[0m'] ``` -------------------------------- ### Replace str.center with wcwidth.center Source: https://github.com/jquast/wcwidth/blob/master/docs/intro.md Use wcwidth.center for correct centering of strings with wide characters or combining characters. ```python >>> 'cafe\u0301'.center(6, '*') # don't do this 'café*' >>> wcwidth.center('cafe\u0301', 6, '*') '*café*' # do this! ``` -------------------------------- ### Measure String Width with wcwidth() Source: https://github.com/jquast/wcwidth/blob/master/docs/intro.md Use wcwidth() to measure string width, with support for control codes and terminal program specific measurements. The `term_program` argument can be set to True or a specific terminal name for accurate measurement. ```python >>> wcwidth.width('\U0001F1FF\U0001F1FC') 2 ``` ```python >>> wcwidth.width('\U0001F1FF\U0001F1FC', term_program=True) 1 ``` ```python >>> wcwidth.width('\U0001F1FF\U0001F1FC', term_program='contour') 2 ``` ```python >>> wcwidth.width('\x1b[38;2;255;150;100mWARN\x1b[0m') 4 ``` ```python >>> wcwidth.width('\t', tabsize=4) 4 ``` ```python >>> wcwidth.width('\t\n\a\r', control_codes='ignore') 0 ``` ```python >>> wcwidth.width('\x1b[H\x1b[2J') 0 ``` ```python >>> wcwidth.width('hello\b\b\b\b\bworld') 5 ``` ```python >>> wcwidth.width('hello\x1b[5Dworld') 5 ``` ```python >>> wcwidth.width('hello\x1b[5Dworld', control_codes='ignore') 10 ``` ```python >>> width('\x1b]66;w=2;XY\x07') 2 ``` ```python >>> width('\x1b]66;s=2;ABC\x07') 6 ``` -------------------------------- ### Handling Alacritty's Emoji ZWJ Support Source: https://github.com/jquast/wcwidth/blob/master/docs/intro.md Shows how wcstwidth accounts for Alacritty's specific handling of emoji sequences involving Zero-Width Joiners (ZWJ). ```python # account for Alacritty non-support of emoji ZWJ: # man + ZWJ + woman + ZWJ + girl + ZWJ + boy >>> family = '\U0001F468\u200D\U0001F469\u200D\U0001F467\u200D\U0001F466' >>> wcwidth.wcswidth(family) 2 >>> wcwidth.wcstwidth(family, term_program='alacritty') 8 ``` -------------------------------- ### Update Requirements for Testing Source: https://github.com/jquast/wcwidth/blob/master/docs/intro.md Upgrades requirements for testing purposes using pip-tools via tox. ```shell tox -e update_requirements38,update_requirements39 ``` -------------------------------- ### Update Unicode Version Tables Source: https://github.com/jquast/wcwidth/blob/master/docs/intro.md Regenerates Python code tables from the latest Unicode Specification data files using tox. ```shell tox -e update ``` -------------------------------- ### Incorrect string justification with standard Python functions Source: https://github.com/jquast/wcwidth/blob/master/docs/intro.md Demonstrates how Python's built-in string justification functions (rjust, center) can produce incorrect results with wide characters or characters that consume more than one cell. ```python >>> # result consumes 16 total cells, 11 expected, >>> 'コンニチハ'.rjust(11, 'X') 'XXXXXXコンニチハ' >>> # result consumes 5 total cells, 6 expected, >>> 'café'.center(6, 'X') 'caféX' ``` -------------------------------- ### Measure Width of a String with Terminal Corrections Source: https://github.com/jquast/wcwidth/blob/master/docs/intro.md Use wcstwidth() for string width measurement with automatic terminal-specific corrections. It reads TERM_PROGRAM or TERM by default, or accepts a provided terminal query response. ```python >>> # '♀️' emoji w/vs-16, uncorrected: >>> wcwidth.wcswidth('\u2640\ufe0f') 2 >>> # corrected, >>> wcwidth.wcstwidth('\u2640\ufe0f', term_program='vte') 1 ``` -------------------------------- ### Extract substring with wcwidth.clip Source: https://github.com/jquast/wcwidth/blob/master/docs/intro.md Use wcwidth.clip to extract a substring by column positions, preserving terminal sequences and handling wide characters. ```python >>> from wcwidth import clip >>> # Wide characters split to Narrow boundaries using fillchar=' ' >>> clip('中文字', 0, 3) '中 ' >>> clip('中文字', 1, 5, fillchar='.') '.文.' >>> # SGR codes are propagated by default - result begins with active style >>> # and ends with reset if styles are active >>> clip('\x1b[1;31mHello world\x1b[0m', 6, 11) '\x1b[1;31mworld\x1b[0m' >>> # Disable SGR propagation to preserve sequence order outside of clip boundary >>> clip('\x1b[31m中文\x1b[32m', 0, 3, propagate_sgr=False) '\x1b[31m中 \x1b[32m' >>> # Cursor-left overwrites previous text (painter's algorithm) >>> clip('hello\x1b[2DXY', 0, 5) 'helXY' >>> # Carriage return resets to column 0, overwriting earlier cells >>> clip('abc\rXY', 0, 5) 'XYc' >>> # even OSC 8 hyperlink text may be clipped, 'Click This link' -> 'is link' ! >>> clip('\x1b]8;;http://example.com\x07Click This link\x1b]8;;\x07', 8, 15) '\x1b]8;;http://example.com\x07is link\x1b]8;;\x07' >>> # and OSC 66 kitty text sizing, supporting width and scale, 'Look' -> '...ook' >>> clip('\x1b]66;w=4:s=4;Look\x07', 1, 16, fillchar='.') '...\x1b]66;s=4:w=3;ook\x07' ``` -------------------------------- ### Update Requirements for Unicode Version Source: https://github.com/jquast/wcwidth/blob/master/docs/intro.md Upgrades requirements specifically for updating the Unicode version using pip-tools via tox. ```shell tox -e update_requirements_update ``` -------------------------------- ### Iterate Sequences with wcwidth Source: https://github.com/jquast/wcwidth/blob/master/docs/intro.md Use iter_sequences() to split text into segments of plain text and escape sequences. Each tuple contains the segment string and a boolean indicating whether it is an escape sequence (True) or text (False). ```python >>> list(wcwidth.iter_sequences('hello')) [('hello', False)] ``` ```python >>> list(wcwidth.iter_sequences('\x1b[31mred\x1b[0m')) [('\x1b[31m', True), ('red', False), ('\x1b[0m', True)] ``` -------------------------------- ### Strict Control Code Handling with wcwidth() Source: https://github.com/jquast/wcwidth/blob/master/docs/intro.md Use `control_codes='strict'` to raise a ValueError for any control sequence that cannot be accurately parsed for horizontal measurement. This is useful when input is known to contain control sequences like SGR color, bold, hyperlinks, and cursor movement. ```python >>> wcwidth.width('\n', control_codes='strict') Traceback (most recent call last): ... ValueError: Vertical movement character 0xa at position 0 ``` ```python >>> wcwidth.width('\x1b[H\x1b[2J', control_codes='strict') Traceback (most recent call last): ... ValueError: Indeterminate cursor sequence at position 0, '\x1b[H' ``` ```python >>> wcwidth.width('a\x1b[5Da', control_codes='strict') Traceback (most recent call last): ... ValueError: Cursor left movement at position 1 would move 5 cells left from column 1, exceeding string start ``` -------------------------------- ### Correcting Width for VTE Terminals Source: https://github.com/jquast/wcwidth/blob/master/docs/intro.md Demonstrates how wcstwidth corrects the width of a trigram character for VTE terminals, which historically rendered them as narrow. ```python >>> wcwidth.wcswidth('\u2630') 2 >>> wcwidth.wcstwidth('\u2630', term_program='vte') 1 ``` -------------------------------- ### Detect Terminal Ambiguous Width Setting Source: https://github.com/jquast/wcwidth/blob/master/docs/intro.md Detect whether the attached terminal is configured to treat ambiguous characters as double-width. This uses the blessed library to query the terminal and then creates a partial function for wcwidth.width with the detected setting. ```python >>> import blessed, functools >>> # Detect terminal ambiguous width as wide (2) or narrow (1) >>> ambiguous_width = blessed.Terminal().detect_ambiguous_width() >>> # Define a new 'width' function with this argument >>> awidth = functools.partial(wcwidth.width, ambiguous_width=ambiguous_width) >>> # result depends on attached terminal mode >>> awidth('\u2460') 1 ``` -------------------------------- ### Replace str.rjust with wcwidth.rjust Source: https://github.com/jquast/wcwidth/blob/master/docs/intro.md Use wcwidth.rjust for correct right-justification of strings containing wide characters. ```python >>> 'コンニチハ'.rjust(11, '*') # don't do this '******コンニチハ' >>> wcwidth.rjust('コンニチハ', 11, '*') # do this! '*コンニチハ' ``` -------------------------------- ### Measure Width of a Single Unicode Codepoint Source: https://github.com/jquast/wcwidth/blob/master/docs/intro.md Use wcwidth() to determine the display width of a single Unicode codepoint. Returns -1 for control codes. ```python >>> # '♀' narrow emoji >>> wcwidth.wcwidth('\u2640') 1 ``` -------------------------------- ### Handle Ambiguous Character Width Source: https://github.com/jquast/wcwidth/blob/master/docs/intro.md Control how characters with ambiguous width are treated. By default, they are treated as narrow (width 1). Set ambiguous_width=2 to treat them as double-width, which is common in CJK environments. ```python >>> # CIRCLED DIGIT ONE - ambiguous width >>> wcwidth.width('\u2460') 1 >>> wcwidth.width('\u2460', ambiguous_width=2) 2 ``` -------------------------------- ### Measure Width of a String Source: https://github.com/jquast/wcwidth/blob/master/docs/intro.md Use wcswidth() to determine the display width of a string of Unicode characters. Returns -1 if control codes occur anywhere in the string. ```python >>> # '♀️' emoji w/vs-16 >>> wcwidth.wcswidth('\u2640\ufe0f') 2 ``` -------------------------------- ### Left Justify Strings with wcwidth Source: https://github.com/jquast/wcwidth/blob/master/docs/intro.md Use wcwidth.ljust() as a replacement for str.ljust() to correctly handle strings containing wide characters. Standard str.ljust() may not align strings properly when wide characters are present. ```python >>> 'コンニチハ'.ljust(11, '*') # don't do this 'コンニチハ******' ``` ```python >>> wcwidth.ljust('コンニチハ', 11, '*') # do this! 'コンニチハ*' ``` -------------------------------- ### Clearing TERM and TERM_PROGRAM for Tests Source: https://github.com/jquast/wcwidth/blob/master/docs/intro.md A pytest fixture to unset TERM and TERM_PROGRAM environment variables before tests, ensuring consistent behavior across different environments. ```python @pytest.fixture(autouse=True) def _clear_term_program(): """unset TERM/TERM_PROGRAM before each test.""" saved_term = os.environ.pop('TERM', None) saved_tprog = os.environ.pop('TERM_PROGRAM', None) yield if saved_term is not None: os.environ['TERM'] = saved_term if saved_tprog is not None: os.environ['TERM_PROGRAM'] = saved_tprog ``` -------------------------------- ### Iterate Grapheme Clusters with wcwidth Source: https://github.com/jquast/wcwidth/blob/master/docs/intro.md Use iter_graphemes() to iterate over grapheme clusters of a string, which represent what a user perceives as a single character. This function implements Unicode Standard Annex #29 grapheme cluster boundary rules. ```python >>> from wcwidth import iter_graphemes >>> # ok + Regional Indicator 'Z', 'W' (Zimbabwe) >>> list(wcwidth.iter_graphemes('ok\U0001F1FF\U0001F1FC')) ['o', 'k', '🇿🇼'] ``` ```python >>> # cafe + combining acute accent >>> list(wcwidth.iter_graphemes('cafe\u0301')) ['c', 'a', 'f', 'é'] ``` ```python >>> # ok + Emoji Man + ZWJ + Woman + ZWJ + Girl >>> list(wcwidth.iter_graphemes('ok\U0001F468\u200d\U0001F469\u200d\U0001F467')) ['o', 'k', '👨‍👩‍👧'] ``` -------------------------------- ### Remove Terminal Escape Sequences Source: https://github.com/jquast/wcwidth/blob/master/docs/intro.md Use strip_sequences() to remove all terminal escape sequences from text. This is useful for cleaning up strings before processing or display. ```python >>> from wcwidth import strip_sequences >>> strip_sequences('\x1b[31mred\x1b[0m') 'red' ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.