unicode_width v1.0.1

Terminal cell widths of code points and text for Crystal, by kitty's algorithm: Unicode 18.0.0 grapheme clusters, escape sequences and streamed input, without allocations.

unicode_width

Terminal cell widths of code points and text: 0, 1 or 2 per cluster.

  • Unicode 18.0.0 tables, grapheme clusters per UAX #29 revision 49.
  • Cells placed by kitty's algorithm.
  • Escape sequences (styles, hyperlinks, ...) take no cells.
  • Input: code points or UTF-8 bytes, whole or streamed.
  • Helpers to truncate, pad and expand tabs; a per-code-point placer for terminal emulators.
  • No allocations, printable ASCII measured eight bytes at a time, tables embedded at compile time.

Installation

dependencies:
  unicode_width:
    github: asekii-org/unicode_width

Then shards install. Requires Crystal 1.22+; no dependencies.

Usage

require "unicode_width"

UnicodeWidth.of('漢')                             # => 2
UnicodeWidth.of("e\u{0301}")                     # => 1
UnicodeWidth.of("\u{1F469}\u{200D}\u{1F4BB} ok") # => 5  👩‍💻 ok
UnicodeWidth.of("\e[1mbold\e[0m")                # => 4

UnicodeWidth.each_cluster("a\u{1F44D}\u{1F3FD}") do |offset, size, width|
  # {0, 1, 1}, then {1, 8, 2}
end

UnicodeWidth.truncate("漢字漢字", 6) # => "漢字…"
UnicodeWidth.pad("漢", 4)           # => "漢  "

cjk = UnicodeWidth::Policy.new(ambiguous: 2)
UnicodeWidth.of("±", cjk) # => 2
API Does
of(codepoint), of(text) Cells of a code point or text
each_cluster(text) Yields {offset, size, width} of every cluster
fit, truncate, pad, offset_at Cut, pad and locate by columns
column, expand_tabs Cursor column; expand tabs to spaces
Policy Target terminal: ambiguous width, graphemes, escapes
Stream Measures text arriving in chunks or code points
Placer Places code points one at a time, for terminal emulators
UNICODE_VERSION Unicode version of the tables ("18.0.0")

Guides

License

MIT, see LICENSE. The Unicode data in .ucd/ and the tables generated from it are under the Unicode License v3.

Repository

unicode_width

Owner
Statistic
  • 0
  • 0
  • 0
  • 0
  • 0
  • about 3 hours ago
  • December 4, 2026
License

MIT License

Links
Synced at

Sat, 05 Dec 2026 03:04:27 GMT

Languages