Skip to content

Terminal software identity-assisted wcswidth()#220

Merged
jquast merged 68 commits into
masterfrom
jq/dynawidth
Jun 5, 2026
Merged

Terminal software identity-assisted wcswidth()#220
jquast merged 68 commits into
masterfrom
jq/dynawidth

Conversation

@jquast

@jquast jquast commented May 25, 2026

Copy link
Copy Markdown
Owner

VS-15 narrows width

Correction Tables

  • disabled by default (term_program=False),

    • although it would improve user experience for downstream dependencies like urwid or python-prompt-toolkit to set term_program=True, that they benefit when unique identifying TERM or TERM_PROGRAM
    • it is likely to cause downstream projects to inexplicably fail automatic tests,
    • and with varying results depending on the terminal used by the developer troubleshooting it.
  • Use environment values of TERM_PROGRAM and TERM, and suggest that calling programs to use XTVERSION or ENQ query result, to hint to width() and similar for wcswidth(term_program='xterm.js'),

    • When set, and it matches the automatic "overrides" tables, a correction is applied.
    • This is made possible by the data gathered by https://ucs-detect.readthedocs.io/ project.
    • Only terminals that can be automatically identified by TERM_PROGRAM, XTVERSION, ENQ, or unique TERM are offered
    • Small performance impact due to extra call for terminal resolution and whether any correction should be applied.
  • Correction tables for ~20+ modern terminals, ~14 unique fingerprints, shared by hashing:

    • alacritty
    • apple_terminal
    • bobcat
    • contour
    • extraterm
    • foot
    • ghostty
    • iterm2
    • kitty
    • konsole
    • mintty
    • mlterm
    • pterm
    • rio
    • st
    • terminology
    • urxvt
    • vte
    • warp
    • wezterm
    • xterm.js
    • zellij
  • And their aliases: hyper, iterm.app, putty, rxvt, rxvt-unicode-256color, screen, st-256color, tabby, tmux-256color, vscode, warpterminal, xterm-ghostty, xterm-kitty.

Closes #211.

Comment thread wcwidth/table_grapheme_overrides/_registry.py Fixed
@codecov

codecov Bot commented May 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (e4f76d5) to head (61b7877).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##            master      #220    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files           23        27     +4     
  Lines         1570      1924   +354     
  Branches       390       456    +66     
==========================================
+ Hits          1570      1924   +354     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread wcwidth/_wcswidth.py Fixed
Comment thread wcwidth/_width.py Fixed
@codspeed-hq

codspeed-hq Bot commented May 25, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 13.5%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

❌ 4 (👁 4) regressed benchmarks
✅ 84 untouched benchmarks
🆕 20 new benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
🆕 test_wcstwidth_emoji_term_program[ghostty] N/A 1.3 ms N/A
🆕 test_wcstwidth_emoji_term_program[xterm.js] N/A 547 µs N/A
🆕 test_wcstwidth_ri_term_program[ghostty] N/A 343.4 ms N/A
🆕 test_wcstwidth_ri_term_program[xterm.js] N/A 344.7 ms N/A
🆕 test_wcstwidth_term_program[ghostty] N/A 729.1 µs N/A
🆕 test_wcstwidth_term_program[xterm.js] N/A 874.7 µs N/A
🆕 test_wcstwidth_vs15_term_program[ghostty] N/A 680.2 µs N/A
🆕 test_wcstwidth_vs15_term_program[xterm.js] N/A 1 ms N/A
🆕 test_wcstwidth_vs16_term_program[ghostty] N/A 666.8 µs N/A
🆕 test_wcstwidth_vs16_term_program[xterm.js] N/A 760.3 µs N/A
🆕 test_wcstwidth_wide_term_program[ghostty] N/A 561.3 µs N/A
🆕 test_wcstwidth_wide_term_program[xterm.js] N/A 1.1 ms N/A
🆕 test_width_ri_term_program[ghostty] N/A 343.6 ms N/A
🆕 test_width_ri_term_program[xterm.js] N/A 344.9 ms N/A
🆕 test_width_term_program[ghostty] N/A 783 µs N/A
🆕 test_width_term_program[xterm.js] N/A 932.8 µs N/A
🆕 test_width_vs15_term_program[ghostty] N/A 735.1 µs N/A
🆕 test_width_vs15_term_program[xterm.js] N/A 1.1 ms N/A
🆕 test_width_vs16_term_program[ghostty] N/A 718.8 µs N/A
🆕 test_width_vs16_term_program[xterm.js] N/A 812.3 µs N/A
... ... ... ... ...

ℹ️ Only the first 20 benchmarks are displayed. Go to the app to view all benchmarks.


Comparing jq/dynawidth (61b7877) with master (e4f76d5)

Open in CodSpeed

Comment thread tests/test_term_overrides.py Fixed
@jquast jquast changed the title terminal software-assisted width Terminal software identity-assisted wcswidth() Jun 1, 2026
@jquast jquast marked this pull request as draft June 3, 2026 23:41
@jquast

jquast commented Jun 3, 2026

Copy link
Copy Markdown
Owner Author

I forgot to include language grapheme overrides, which is arguably the most useful -- but there is a lot of difficulty in doing this correctly, it might require that wcswidth() call iter_graphemes(), which is high performing for python 3.15 but it will definitely have some performance impact for earlier python

Comment thread wcwidth/_width.py Fixed
@jquast jquast marked this pull request as ready for review June 5, 2026 18:36
jquast added 4 commits June 5, 2026 14:50
leave wcswidth() as-is and make new wcstwidth() for logic containing
terminal-aware overrides, this should remove any performance loss for
the pre-0.8.0 use case of not specifying any 'term_program=True|str'
@jquast jquast merged commit 169c846 into master Jun 5, 2026
29 checks passed
@jquast jquast deleted the jq/dynawidth branch June 5, 2026 20:28
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.

Should wcswidth return Narrow (1) for Basic Emojis followed by VS-15, U+FE0E?

2 participants