telegram: tune table width with emoji-aware display width
This commit is contained in:
parent
f8ac77a2a3
commit
25f081da1d
2 changed files with 16 additions and 0 deletions
|
|
@ -771,6 +771,8 @@ func displayWidth(s string) int {
|
||||||
continue
|
continue
|
||||||
case unicode.Is(unicode.Mn, r):
|
case unicode.Is(unicode.Mn, r):
|
||||||
continue
|
continue
|
||||||
|
case isEmojiRune(r):
|
||||||
|
w += 3
|
||||||
case unicode.In(r,
|
case unicode.In(r,
|
||||||
unicode.Han,
|
unicode.Han,
|
||||||
unicode.Hiragana,
|
unicode.Hiragana,
|
||||||
|
|
@ -787,6 +789,13 @@ func displayWidth(s string) int {
|
||||||
return w
|
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 {
|
func wrapByDisplayWidth(s string, maxWidth int) []string {
|
||||||
if maxWidth <= 0 {
|
if maxWidth <= 0 {
|
||||||
return []string{s}
|
return []string{s}
|
||||||
|
|
|
||||||
|
|
@ -79,3 +79,10 @@ func TestMarkdownToTelegramHTML_TableWrapsLongCell(t *testing.T) {
|
||||||
t.Fatalf("expected wrapped long cell content, got: %q", got)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue