Unicode text measurement helpers: grapheme clusters, display width, and column-based slicing and padding.
Summary
Functions
Calculate the display width of a single grapheme. Wide chars (CJK, some emoji) take 2 columns; combining marks take 0.
Get display width accounting for wide characters (CJK, emoji).
Return the grapheme whose cell range covers the given display column.
Get grapheme count (visual character count).
Get the display column corresponding to a grapheme index.
Pad a string on the left to the requested display width.
Pad a string on the right to the requested display width.
Slice a string by display columns, replacing partially clipped graphemes with spaces.
Take the leading text that fits within the given display columns.
Functions
@spec char_width(String.t()) :: 0 | 1 | 2
Calculate the display width of a single grapheme. Wide chars (CJK, some emoji) take 2 columns; combining marks take 0.
@spec display_width(String.t()) :: non_neg_integer()
Get display width accounting for wide characters (CJK, emoji).
Return the grapheme whose cell range covers the given display column.
@spec grapheme_count(String.t()) :: non_neg_integer()
Get grapheme count (visual character count).
@spec grapheme_index_to_column(String.t(), integer()) :: non_neg_integer()
Get the display column corresponding to a grapheme index.
@spec pad_leading_columns(String.t(), non_neg_integer(), String.t()) :: String.t()
Pad a string on the left to the requested display width.
@spec pad_trailing_columns(String.t(), non_neg_integer(), String.t()) :: String.t()
Pad a string on the right to the requested display width.
Slice a string by display columns, replacing partially clipped graphemes with spaces.
Take the leading text that fits within the given display columns.