terminalCellWidth function
Returns the terminal cell width of a single grapheme cluster.
cluster should be a single grapheme cluster (as produced by
Characters(str).first); callers that pass arbitrary strings get the
width of the first cluster only.
Implementation
int terminalCellWidth(String cluster) {
if (cluster.isEmpty) return 0;
final iter = cluster.characters.iterator;
if (!iter.moveNext()) return 0;
final runes = iter.current.runes.iterator;
if (!runes.moveNext()) return 0;
final first = runes.current;
var width = _openTuiCodePointWidth(first);
var hasWidth = width > 0;
final regionalIndicatorPair = _isRegionalIndicator(first);
var hasIndicMark = false;
while (runes.moveNext()) {
final codePoint = runes.current;
final codePointWidth = _openTuiCodePointWidth(codePoint);
if (codePoint == 0xFE0F) {
if (hasWidth && width == 1) width = 2;
continue;
}
if (_isDevanagariNonspacingMark(codePoint)) {
hasIndicMark = true;
continue;
}
if (regionalIndicatorPair && _isRegionalIndicator(codePoint)) {
width += codePointWidth;
hasWidth = true;
} else if (!hasWidth && codePointWidth > 0) {
width = codePointWidth;
hasWidth = true;
} else if (hasWidth &&
hasIndicMark &&
_isDevanagariBase(codePoint) &&
codePointWidth > 0) {
if (codePoint != 0x0930) width += codePointWidth;
hasIndicMark = false;
}
}
return width;
}