Terminal Unicode width is difficult to predict because emulators interpret emoji, grapheme clusters, variation selectors, regional indicators, and invisible format characters differently. Measurements across roughly 35 terminals show that support for grapheme clustering does not guarantee consistent widths: even terminals advertising the same feature disagree on particular characters and sequences. The central proposal is wcstwidth(), a Python function that adjusts standard wcwidth measurements using correction tables tailored to the active terminal. For example, a sequence estimated at two cells by the general algorithm may occupy eight in VTE. The author argues that such terminal-specific corrections are needed until common standards or sizing protocols are widely adopted.
Correction tables depend on identifying the terminal, using environment variables such as TERM or TERM_PROGRAM, or queries supported by some emulators; remote connections can make identification harder. Multiplexers are excluded because their reported cursor positions and displayed widths depend on the host terminal and can conflict. A complementary approach, the Kitty Text Sizing Protocol, lets applications mark text so terminals can present complex graphemes at a specified width. The work also expands Python tools for measuring, wrapping, clipping, aligning, and navigating text that includes graphemes and escape sequences. Finally, testing found that most grapheme-aware terminals keep a grapheme within two cells, prompting wcwidth to impose that limit, even though it can make long Indic-script conjuncts less legible.
Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.