Debian Codegraph @

comparison/COMPATIBILITY_ANALYSIS.md

raw symbols SCIP
1 # Compatibility Analysis: displaywidth, go-runewidth, and uniseg
2
3 > Generated by Cursor IDE using Claude Sonnet 4.5, and edited by @clipperhouse
4
5 This document summarizes the compatibility findings between three Go libraries for Unicode string width calculation:
6
7 - [clipperhouse/displaywidth](https://github.com/clipperhouse/displaywidth) (this package)
8 - [mattn/go-runewidth](https://github.com/mattn/go-runewidth)
9 - [rivo/uniseg](https://github.com/rivo/uniseg)
10
11 ## Basic Unicode Categories
12
13 Most Unicode categories show good compatibility.
14
15 | Category | displaywidth | go-runewidth | uniseg |
16 |----------|--------------|--------------|---------|
17 | ASCII | ✅ Compatible | ✅ Compatible | ✅ Compatible |
18 | Latin Extended | ✅ Compatible | ✅ Compatible | ✅ Compatible |
19 | CJK (Chinese/Japanese/Korean) | ✅ Compatible | ✅ Compatible | ✅ Compatible |
20 | Arabic | ✅ Compatible | ✅ Compatible | ✅ Compatible |
21 | Combining Marks | ✅ Compatible | ✅ Compatible | ✅ Compatible |
22 | Zero-Width Characters | ✅ Compatible | ✅ Compatible | ✅ Compatible |
23
24 ## Emojis
25
26 Regular emojis (😀, 🚀, 🎉, etc.) behave identically:
27
28 | Library | Regular Emoji Width |
29 |---------|---------------------|
30 | **displaywidth** | Always 2 |
31 | **go-runewidth** | Always 2 |
32 | **uniseg** | Always 2 |
33
34 ### Regional Indicator Pairs (Flags)
35
36 Regional indicator pairs (flags like 🇺🇸) are composed of two Regional Indicator symbols.
37
38 | Library | Behavior |
39 |--------|----------|
40 | **displaywidth** | Width 2 per flag |
41 | **go-runewidth** | Width 1 per flag |
42 | **uniseg** | Width 2 per flag |
43
44 **Example:** `🇺🇸🇯🇵🇬🇧` (3 flags)
45 - displaywidth: 6 columns (2+2+2)
46 - go-runewidth: 3 columns (1+1+1)
47 - uniseg: 6 columns (2+2+2)
48
49 I (@clipperhouse) believe that 2 is the correct width, they are emojis. Ghostty and iTerm display
50 regional flags as width 2, as does VS Code. Mac Terminal (Tahoe macOS 26) displays them as 1. Sigh.
51
52 To repro in your terminal of choice:
53
54 ```
55 echo "🇺🇸🇯🇵🇬🇧abc\n123456"
56 ```
57
58 I have considered detecting the terminal (like a user agent) and using width 1 for
59 Mac Terminal as a special case. I kinda hate that, because if Apple corrects it, then
60 the behavior changes. OTOH, I assume Mac Terminal is the most popular terminal and
61 so it might be better for end-users.
62
63 ## Variation Selectors
64
65 VS15 and VS16 from [Unicode TR51](https://unicode.org/reports/tr51/#Emoji_Variation_Sequences)
66
67 | Library | VS16 (U+FE0F) | VS15 (U+FE0E) |
68 |---------|---------------|---------------|
69 | **displaywidth** | Forces emoji presentation (width 2) | No effect, preserves base width |
70 | **go-runewidth** | Treated as separate character (width 1) | Treated as separate character (width 1) |
71 | **uniseg** | Treated as part of emoji (width 2) | Forces width 1 |
72
73 **Example:** `☺️⌛︎❤️` (3 emoji with variation selectors)
74 - displaywidth: 6 columns
75 - go-runewidth: 4 columns
76 - uniseg: 5 columns
77
78 I would appear to me (@clipperhouse) that the handling of VS15 is not widely
79 agreed upon. Some libraries and standards (such as wcwidth) interpret it as
80 "always narrow to width 1". Others (such as this library) interpret it as
81 "no effect on width, use the base character width".
82
83 Here is [a conversation on GitHub](https://github.com/contour-terminal/contour/discussions/1178#discussioncomment-6778716) and an [explanation from Grok](https://grok.com/share/bGVnYWN5LWNvcHk%3D_274f540c-c9a6-47c7-9d4f-47697ed20032).
84
85 ## Keycap Sequences
86
87 Keycap sequences like 1️⃣ are formed by: base character + variation selector (U+FE0F) + combining enclosing keycap (U+20E3).
88
89 | Library | Behavior | Width per Keycap |
90 |---------|----------|------------------|
91 | **displaywidth** | Treats as emoji | 2 columns |
92 | **go-runewidth** | Treats base character | 1 column |
93 | **uniseg** | Treats as base character | 1 column |
94
95 **Example:** `1️⃣#️⃣` (2 keycap sequences)
96 - displaywidth: 4 columns (2 per keycap)
97 - go-runewidth: 2 columns (1 per keycap)
98 - uniseg: 2 columns (1 per keycap)
99
100 ## East Asian Ambiguous Width
101
102 [East Asian Ambiguous characters](https://www.unicode.org/reports/tr11/#Ambiguous) (★, °, ±, etc.) can be rendered as either narrow (1 column) or wide (2 columns) depending on configuration.
103
104 | Library | Default | With EastAsianWidth=true |
105 |---------|---------|--------------------------|
106 | **displaywidth** | Width 1 | Width 2 |
107 | **go-runewidth** | Width 1 | Width 2 |
108 | **uniseg** | Width 1 | Width 2 |
109
110 **Example:** `★°±` (3 ambiguous characters)
111 - displaywidth default: 3 columns
112 - displaywidth with EastAsianWidth=true: 6 columns
113 - go-runewidth default: 3 columns
114 - go-runewidth with EastAsianWidth=true: 6 columns
115 - uniseg default: 3 columns
116 - uniseg with EastAsianAmbiguousWidth=2: 5 columns (usually)
117
118 ## Detailed Test Results
119
120 > Run `go test -v` in the `comparison/` directory to see comprehensive behavior comparisons between libraries.
121
122 ### Test Case: "Hello 世界! 😀🇺🇸"
123
124 **Breakdown:**
125 - "Hello " = 6 columns (ASCII)
126 - "世界" = 4 columns (CJK, 2 each)
127 - "! " = 2 columns (ASCII)
128 - "😀" = 2 columns (emoji)
129 - "🇺🇸" = 1-2 columns (flag, depends on library)
130
131 **Results:**
132 - displaywidth: 16 columns (flag = 2 columns)
133 - go-runewidth: 15 columns (flag = 1 column)
134 - uniseg: 16 columns (flag = 2 columns)
135
136 ### Test Case: "🚀🚀🚀" (3 rocket emoji)
137
138 **Results:**
139 - displaywidth: 6 columns (2 per emoji)
140 - go-runewidth: 6 columns (2 per emoji)
141 - uniseg: 6 columns (2 per emoji)
142
143 ## Further Reading
144
145 - [State of Terminal Emulation 2025](https://www.jeffquast.com/post/state-of-terminal-emulation-2025/)