Skip to content

Instantly share code, notes, and snippets.

@tompng
Created August 20, 2024 16:44
Show Gist options
  • Save tompng/bb36d6020d0183e858742123b6cbfd78 to your computer and use it in GitHub Desktop.
Save tompng/bb36d6020d0183e858742123b6cbfd78 to your computer and use it in GitHub Desktop.
terminal emulator measure result
codepoints = File.read('EastAsianWidth.txt').scan(/^[0-9A-F]{4,6}/).map { _1.to_i(16) }
widths = codepoints.map do |cp|
c = cp.chr('utf-8') rescue (next nil)
print "\e[H\e[K#{c}\e[6n"
STDOUT.flush
/\e\[\d+;(\d+)R/ =~ STDIN.raw { STDIN.readpartial(1000) }
$1.to_i - 1
end
codepoints.zip(widths).reject {
!_2 || Reline::Unicode.get_mbchar_width(''<<_1) == _2 || ('a'<<_1).grapheme_clusters.size == 1
}.map{
[_1, _2, '[ '<<_1<<' ]', Reline::Unicode.get_mbchar_width(''<<_1)]
}

Output format

# amibuous_width = 1
[codepoint, actual_width, preview, reline_calculated_width][]

Mac Terminal.app

[[0, 0, "[  \u0000  ]", 2],
 [127, 0, "[  \u007F  ]", 2],
 [4448, 0, "[  ᅠ  ]", 1],
 [8294, 0, "[  ⁦  ]", 1],
 [12783, 1, "[  \u31EF  ]", 2]]

iTerm2

[[0, 0, "[  \u0000  ]", 2],
 [127, 1, "[  \u007F  ]", 2],
 [173, 0, "[    ]", 1],
 [1564, 0, "[    ]", 1],
 [4448, 0, "[    ]", 1],
 [6158, 0, "[    ]", 1],
 [8234, 0, "[    ]", 1],
 [8288, 0, "[    ]", 1],
 [8294, 0, "[    ]", 1],
 [12783, 1, "[  \u31EF  ]", 2],
 [65279, 0, "[    ]", 1],
 [65440, 0, "[    ]", 1],
 [113824, 0, "[    ]", 1],
 [119155, 0, "[    ]", 1],
 [127462, 2, "[  🇦  ]", 1],
 [917505, 0, "[    ]", 1]]

VSCode Terminal

[[0, 0, "[  \u0000  ]", 2],
 [127, 0, "[  \u007F  ]", 2],
 [128, 0, "[  \u0080  ]", 1],
 [12783, 1, "[  \u31EF  ]", 2],
 [65279, 0, "[    ]", 1],
 [101120, 1, "[  𘬀  ]", 2],
 [101632, 1, "[  𘴀  ]", 2],
 [110576, 1, "[  𚿰  ]", 2],
 [110581, 1, "[  𚿵  ]", 2],
 [110589, 1, "[  𚿽  ]", 2],
 [110898, 1, "[  𛄲  ]", 2],
 [110933, 1, "[  𛅕  ]", 2],
 [128732, 1, "[  🛜  ]", 2],
 [129008, 1, "[  🟰  ]", 2],
 [129292, 1, "[  🤌  ]", 2],
 [129339, 2, "[  🤻  ]", 1],
 [129350, 2, "[  🥆  ]", 1],
 [129727, 1, "[  🪿  ]", 2],
 [129742, 1, "[  🫎  ]", 2],
 [129760, 1, "[  🫠  ]", 2],
 [129776, 1, "[  🫰  ]", 2]]

Alacritty

[[0, 0, "[  \u0000  ]", 2],
 [127, 0, "[  \u007F  ]", 2],
 [128, 0, "[  \u0080  ]", 1],
 [1536, 0, "[  ؀  ]", 1],
 [1564, 0, "[  ؜  ]", 1],
 [1757, 0, "[  ۝  ]", 1],
 [1807, 0, "[  ܏  ]", 1],
 [2192, 0, "[  ࢐  ]", 1],
 [2274, 0, "[  ࣢  ]", 1],
 [4448, 0, "[  ᅠ  ]", 1],
 [6158, 0, "[  ᠎  ]", 1],
 [8203, 0, "[  ​  ]", 1],
 [8234, 0, "[  ‪  ]", 1],
 [8288, 0, "[  ⁠  ]", 1],
 [8294, 0, "[  ⁦  ]", 1],
 [12783, 1, "[  \u31EF  ]", 2],
 [65279, 0, "[    ]", 1],
 [65529, 0, "[    ]", 1],
 [69821, 0, "[  𑂽  ]", 1],
 [69837, 0, "[  𑃍  ]", 1],
 [78896, 0, "[  𓐰  ]", 1],
 [113824, 0, "[  𛲠  ]", 1],
 [119155, 0, "[  𝅳  ]", 1],
 [917505, 0, "[  󠀁  ]", 1]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment