Skip to content

New: wrap()#169

Merged
jquast merged 46 commits intomasterfrom
jq/next-new-textwrap
Jan 17, 2026
Merged

New: wrap()#169
jquast merged 46 commits intomasterfrom
jq/next-new-textwrap

Conversation

@jquast
Copy link
Owner

@jquast jquast commented Jan 15, 2026

This PR builds on #168 and #165 combined

New wrap() function is an emoji, control and terminal sequence, wide, zero-width, and grapheme-aware version of textwrap.wrap().

>>> # Wrapping CJK text (each character is 2 cells wide)
>>> wrap('コンニチハ', 4)
['コン', 'ニチ', 'ハ']

>>> # Text with ANSI color sequences
>>> wrap('\x1b[31mhello world\x1b[0m', 5)
['\x1b[31mhello', 'world\x1b[0m']

jquast added 28 commits January 14, 2026 16:33
Implements Unicode Standard Annex #29 grapheme cluster boundaries.
Handles Hangul syllables, emoji ZWJ sequences, regional indicators,
combining characters, and Indic scripts.

New exports: iter_graphemes, _bisearch
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
We suggested to use ``wcwidth<2`` for years, when it should have been
``wcwidth<1``, I really hope somebody didn't copy & paste our
recommendation .. :(
its a private function, anyway, still ok.

Below the turtles, 0/1 is very much the definition of Falsey and Truthy.
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
Adds iter_graphemes() function for Unicode grapheme cluster iteration.
@jquast jquast changed the base branch from jq/merged-grapheme-and-width to wcwidth.merged-grapheme-ljust-center January 15, 2026 20:24
@jquast jquast force-pushed the jq/next-new-textwrap branch from 0126606 to 22e4624 Compare January 16, 2026 04:06
and, remove optional 'column=0', if you want to measure text starting at
a different column containing tabs .. then expand your own!
and, remove optional 'column=0', if you want to measure text starting at
a different column containing tabs .. then expand your own!
@jquast jquast changed the base branch from wcwidth.merged-grapheme-ljust-center to jq/next-new-grapheme January 16, 2026 05:07
@jquast jquast changed the base branch from jq/next-new-grapheme to wcwidth.merged-grapheme-ljust-center January 16, 2026 05:08
Text wrapping that properly handles:
- Terminal escape sequences
- Unicode grapheme clusters
- Hyphenation at proper boundaries
- Width calculation for CJK and emoji

New exports: wrap, SequenceTextWrapper
These files were leftovers from a previous iteration. The textwrap
module now uses escape_sequences.py instead.
especially regarding tabs. I've also decided to "take the easy way out"
and use str.expandtabs() instead of processing it ourselves and reduce
our complexity and need of additional column=1 argument
@jquast jquast force-pushed the jq/next-new-textwrap branch from ea6f1f1 to b055e8a Compare January 16, 2026 05:15
@jquast jquast changed the title Jq/next new textwrap New: wrap() Jan 16, 2026
@jquast jquast marked this pull request as ready for review January 16, 2026 05:23
@jquast jquast changed the base branch from wcwidth.merged-grapheme-ljust-center to master January 17, 2026 19:00
@jquast jquast merged commit c91edbb into master Jan 17, 2026
@jquast jquast deleted the jq/next-new-textwrap branch January 17, 2026 19:01
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