stringWidth method

int stringWidth(
  1. String s
)

Calculates the display width of s using grapheme cluster iteration.

Each grapheme cluster is measured individually. Clusters containing U+FE0F (emoji presentation selector) are widened to emojiPresentationWidth when the base rune would otherwise be narrow.

Implementation

int stringWidth(String s) {
  var width = 0;
  // Count display width per grapheme cluster to avoid double-counting
  // multi-codepoint clusters (e.g. ZWJ emoji sequences).
  for (final g in uni.graphemes(s)) {
    var w = runeWidth(uni.firstCodePoint(g));
    // If the grapheme contains U+FE0F (variation selector 16 — emoji
    // presentation), terminals render the base character at emoji width
    // even if runeWidth() returned 1.
    if (w == 1 && g.length > 1 && g.contains('\uFE0F')) {
      w = emojiPresentationWidth;
    }
    width += w;
  }
  return width;
}