telegram: tune table width with emoji-aware display width

This commit is contained in:
dj-oyu 2026-02-20 01:28:38 +09:00
parent 9c4b49d818
commit f283427480
2 changed files with 16 additions and 0 deletions

View file

@ -771,6 +771,8 @@ func displayWidth(s string) int {
continue
case unicode.Is(unicode.Mn, r):
continue
case isEmojiRune(r):
w += 3
case unicode.In(r,
unicode.Han,
unicode.Hiragana,
@ -787,6 +789,13 @@ func displayWidth(s string) int {
return w
}
func isEmojiRune(r rune) bool {
// Common emoji blocks + Dingbats/Stars used in rating tables.
return (r >= 0x1F300 && r <= 0x1FAFF) || // Misc emoji/pictographs/symbols
(r >= 0x2600 && r <= 0x27BF) || // Misc symbols + dingbats
r == 0x2B50 // WHITE MEDIUM STAR (⭐)
}
func wrapByDisplayWidth(s string, maxWidth int) []string {
if maxWidth <= 0 {
return []string{s}

View file

@ -79,3 +79,10 @@ func TestMarkdownToTelegramHTML_TableWrapsLongCell(t *testing.T) {
t.Fatalf("expected wrapped long cell content, got: %q", got)
}
}
func TestDisplayWidth_EmojiIsThree(t *testing.T) {
got := displayWidth("⭐⭐⭐⭐⭐")
if got != 15 {
t.Fatalf("displayWidth(stars) = %d, want 15", got)
}
}