pub enum WidthMode {
Wc,
Grapheme,
}Expand description
How grapheme clusters are measured for terminal-cell layout.
This enum does not control segmentation: grapheme_cells always
segments by extended grapheme cluster. It controls only width calculation
for each cluster. The East-Asian Ambiguous policy is orthogonal and is
passed as a separate eaw_wide boolean to char_width and
grapheme_width.
Variants§
Wc
Measure each cluster by summing the widths of its code points.
This models a terminal that has no grapheme segmentation and advances
the cursor once per code point, which is what POSIX wcswidth
describes: the sum of wcwidth over the string. Combining marks,
joiners, and variation selectors are themselves zero-width, so they
add nothing and a cluster like e + combining acute still measures
one cell. A code point that owns cells does add, which is why a
regional-indicator pair measures two.
A long joined sequence measures the sum of its parts, so a four-person family emoji is eight columns and a skin-toned thumbs-up is four. Use this when matching older or simpler terminal width behavior is more important than cluster-aware emoji presentation.
Grapheme
Measure the whole grapheme cluster.
This mode accounts for variation selectors, regional indicators, zero-width-joiner sequences, and pictographic presentation when deciding whether a cluster occupies zero, one, or two cells. Use it for modern terminal rendering that treats emoji and joined clusters as a single displayed unit.
Implementations§
Source§impl WidthMode
impl WidthMode
Sourcepub fn grapheme_width(self, g: &str, eaw_wide: bool) -> u8
pub fn grapheme_width(self, g: &str, eaw_wide: bool) -> u8
Measure one extended grapheme cluster under this mode.
In WidthMode::Wc mode, the width is the sum of char_width over
every code point in g; an empty string is 0. In
WidthMode::Grapheme mode, the width is grapheme_width for the
whole cluster.
§Parameters
g— an extended grapheme cluster. Passing a longer string is accepted, and inWcmode every code point in it counts toward the sum.eaw_wide— East-Asian Ambiguous policy; seechar_width.
§Returns
The cluster width in terminal cells. WidthMode::Grapheme returns
0, 1, or 2; WidthMode::Wc sums code points and can return
more, saturating at u8::MAX.
§Errors and panics
This method does not fail or intentionally panic.