Skip to content

Conversation

@jquast
Copy link
Owner

@jquast jquast commented Jan 15, 2026

  • Add new width() function for measuring terminal-aware strings, with support for control codes, escape sequences (SGR, OSC, CSI), cursor movement, and tab stops.
  • Add iter_sequences() function to iterate with text containing escape sequences
  • New file, control_codes.py for control characters, categorized
  • New file escape_sequences.py for terminal sequence patterns, categorized
  • extract _bisearch , duplicates New: iter_graphemes() #165

A few examples from docs/intro.rst:

>>> wcwidth.width('\x1b[38;2;255;150;100mWARN\x1b[0m')
4

>>> list(wcwidth.iter_sequences('\x1b[31mred\x1b[0m'))
[('\x1b[31m', True), ('red', False), ('\x1b[0m', True)]

>>> wcwidth.width('\U0001F1FF\U0001F1FC')
2

jquast added 14 commits January 14, 2026 16:34
New width() function handles:
- Terminal escape sequences (SGR, OSC, CSI, etc.)
- Control codes with parse/strict/ignore modes
- Tab stops and cursor movement
- Always returns non-negative integer (never -1)

New exports: width
its a private function, anyway, still ok.

Below the turtles, 0/1 is very much the definition of Falsey and Truthy.
- Test lone ESC character handling in iter_sequences and width
- Test backspace at column 0 (no negative position)
- Test carriage return column reset
- Test tab with tabstop=0 in parse mode
- Test vertical control (LF) in parse mode
@jquast jquast changed the title Add width() function for terminal-aware string measurement New: width() terminal-aware string measurement Jan 15, 2026
@jquast jquast merged commit bbb0e47 into master Jan 17, 2026
36 checks passed
@jquast jquast deleted the jq/next-new-width branch January 17, 2026 18:08
jquast added a commit that referenced this pull request Jan 17, 2026
Justify text with control-code and sequence-awareness, requires #166 

Example,

        >>> wcwidth.center('hi', 6)
        '  hi  '
        >>> wcwidth.center('\\x1b[31mhi\\x1b[0m', 6)
        '  \\x1b[31mhi\\x1b[0m  '
        >>> wcwidth.center('\\U0001F468\\u200D\\U0001F469\\u200D\\U0001F467', 6)
        '  👨‍👩‍👧  '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant