ElixirOpentui.TextBuffer (ElixirOpentui v0.2.0)

Copy Markdown View Source

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

char_width(grapheme)

@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.

display_width(text)

@spec display_width(String.t()) :: non_neg_integer()

Get display width accounting for wide characters (CJK, emoji).

grapheme_at_column(text, column)

@spec grapheme_at_column(String.t(), integer()) :: String.t() | nil

Return the grapheme whose cell range covers the given display column.

grapheme_count(text)

@spec grapheme_count(String.t()) :: non_neg_integer()

Get grapheme count (visual character count).

grapheme_index_to_column(text, index)

@spec grapheme_index_to_column(String.t(), integer()) :: non_neg_integer()

Get the display column corresponding to a grapheme index.

pad_leading_columns(text, total_columns, pad_char \\ " ")

@spec pad_leading_columns(String.t(), non_neg_integer(), String.t()) :: String.t()

Pad a string on the left to the requested display width.

pad_trailing_columns(text, total_columns, pad_char \\ " ")

@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_columns(text, start_column, width)

@spec slice_columns(String.t(), integer(), integer()) :: String.t()

Slice a string by display columns, replacing partially clipped graphemes with spaces.

take_columns(text, max_columns)

@spec take_columns(String.t(), integer()) :: String.t()

Take the leading text that fits within the given display columns.