Skip to content

Conversation

@jquast
Copy link
Owner

@jquast jquast commented Jan 15, 2026

This PR builds upon both #165 and #166 to provide ljust, rjust, and center().

From docs/intro.rst, replaces standard functions:

>>> # result consumes 16 total cells, 11 expected,
>>> 'コンニチハ'.ljust(11, '*')
'コンニチハ******'
>>> # result consumes 16 total cells, 11 expected,
>>> 'コンニチハ'.rjust(11, '*')
'******コンニチハ'
>>> # result consumes 5 total cells, 6 expected,
>>> 'cafe\u0301'.center(6, '*')
'café*'

This is corrected with the proposed grapheme and sequence-aware corrected functions:

>>> wcwidth.ljust('コンニチハ', 11, '*')
'コンニチハ*'
>>> wcwidth.rjust('コンニチハ', 11, '*')
'*コンニチハ'
>>> wcwidth.center('cafe\u0301', 6, '*')
'*café*'

jquast added 17 commits January 14, 2026 16:01
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
String justification functions that properly handle:
- Terminal escape sequences
- Unicode grapheme clusters
- Multi-cell fillchar characters
- Wide CJK characters

New exports: iter_sequences, ljust, rjust, center
all the big functions go into wcwidth.py
remove carcass of unused _generated_caps and sequences.py
@jquast jquast changed the base branch from master to jq/merged-grapheme-and-width January 15, 2026 16:41
@jquast jquast closed this Jan 15, 2026
@jquast jquast deleted the jq/next-new-ljust-rjust-center branch January 15, 2026 22:54
@jquast jquast restored the jq/next-new-ljust-rjust-center branch January 17, 2026 19:00
@jquast jquast deleted the jq/next-new-ljust-rjust-center branch January 17, 2026 19:00
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