fix: correct emoji width for table alignment#797
Open
zamderax wants to merge 1 commit intotuist:mainfrom
Open
Conversation
pepicrft
reviewed
Dec 26, 2025
| @@ -10,7 +10,10 @@ extension Character { | |||
| /// There is no standard for this, but it seems like most terminals treat | |||
| /// emojis and ideographs as double width. | |||
| public var displayWidth: Int { | |||
Contributor
There was a problem hiding this comment.
I'd suggest to adjust the implementation to use wcwidth, which is a POSIX C function that returns the number of columns a character occupies in a terminal. Terminal, shells, and CLI tools like ls & vim use it:
#if canImport(Darwin)
import Darwin
#elseif canImport(Glibc)
import Glibc
#endif
extension Character {
public var displayWidth: Int {
unicodeScalars.reduce(0) { total, scalar in
let w = wcwidth(wchar_t(scalar.value))
// wcwidth returns -1 for non-printable, treat as 0
return total + max(0, Int(w))
}
}
}
Contributor
Author
There was a problem hiding this comment.
Here's the problem I ran into with wcwidth
- Emoji sequences - Characters like 👨👩👦 are actually multiple Unicode scalars joined by Zero-Width Joiners (ZWJ):
👨 + ZWJ + 👩 + ZWJ + 👦 = 👨👩👦 wcwidthsees each piece separately and sums them (~8), but terminals render it as one 2-column character.- Variation selectors - ✓ (U+2713) vs ✓️ (U+2713 + U+FE0F). The second has a variation selector that tells the terminal "render this as emoji" (width 2), but wcwidth ignores it.
- I put a flag emoji - 🇺🇸 is two "regional indicator" characters. wcwidth returns -1 (unknown) for each, but terminals show a single 2-column flag.
What do you think we should do?
zamderax
added a commit
to wendylabsinc/Noora
that referenced
this pull request
Feb 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Emojis were treated as width of 1 but now they should be treated as width of 2. This incorrectly would shift a row in a table off by one space.